读书人

超级新手初学者

发布时间: 2012-08-03 00:12:14 作者: rapoo

超级新手菜鸟求助
题目是 :
定义一个 vector 的容器,存储你在未来六个月里要阅
读的书,再定义一个 set,用于记录你已经看过的书名。
编写程序从 vector 中为你选择一本没有读过而现在要
读的书。当它为你返回选中的书名后,应该将该书名放
入记录已读书目的 set 中。如果实际上你把这本书放在
一边没有看,则本程序应该支持从已读书目的 set 中删
除该书的记录。在虚拟的六个月后,输出已读书目和还
没有读的书目。



我写的代码是:

#include <iostream>
#include <string>
#include <vector>
#include <set>
using namespace std;

int main()
{
vector<string>want_read;
set<string>have_read;
cout << "enter bookname you want read" << endl;
string name;
while (cin >> name)
want_read.push_back(name);
cout << "selece a book you want to read now" << endl;
while (cin >> name) {
for (vector<string>::iterator iter = want_read.begin();
iter != want_read.end(); ++iter) {
if (name != *iter)
cout << "this book was not you want to read" << endl;
else {
have_read.insert(iter);
want_read.erase(iter);
}
}
}
for (vector<string>::iterator it = want_read.begin();
it != want_read.end(); ++ it)
cout << *it << " ";
cout << endl;
for (set<string>::iterator it = have_read.begin();
it != have_read.end(); ++ it)
cout << *it << " ";
return 0;
}




错了 ,请问哪里错了,我不会改

[解决办法]

C/C++ code
 #include <iostream>#include <string>#include <vector>#include <set>using namespace std;int main(){vector<string>want_read;set<string>have_read;cout << "enter bookname you want read" << endl;string name;while (cin >> name)want_read.push_back(name);cout << "selece a book you want to read now" << endl;while (cin >> name) {for (vector<string>::iterator iter = want_read.begin();iter != want_read.end(); ++iter) {if (name != *iter)cout << "this book was not you want to read" << endl;else {have_read.insert(iter);want_read.erase(iter);}}}for (vector<string>::iterator it = want_read.begin();  it != want_read.end(); ++ it)cout << *it << " ";cout << endl;for (set<string>::iterator it = have_read.begin();it != have_read.end(); ++ it)cout << *it << " ";return 0;}
[解决办法]
程序修改如下:调用algorithm中的find函数查找
C/C++ code
#include <iostream>#include <string>#include <vector>#include <set>#include <algorithm>using namespace std;int main(){    vector<string>want_read;    set<string>have_read;    cout << "enter bookname you want read, input 'quit' to end:" << endl;    string name;    while (cin >> name)    {        if(name == "quit")            break;        want_read.push_back(name);        cout << "enter bookname you want read, input 'quit' to end:" << endl;    }    cout << "selece a book you want to read now, input 'quit' to end:" << endl;    while (cin >> name)     {        if(name == "quit")            break;        vector<string>::iterator it = find(want_read.begin(),                                want_read.end(),name);        if(it == want_read.end())            cout << "this book is not you want to read!" << endl;        else        {            want_read.erase(it);            have_read.insert(name);            cout<<"this book is your need, it has been read!"<<endl;        }        cout << "selece a book you want to read now, input 'quit' to end:" << endl;    }    for (vector<string>::iterator it = want_read.begin();                          it != want_read.end(); ++ it)        cout << *it << " ";    cout << endl;    for (set<string>::iterator it = have_read.begin();                        it != have_read.end(); ++ it)        cout << *it << " ";    system("pause");    return 0;} 


[解决办法]
for (vector<string>::iterator iter = want_read.begin();
iter != want_read.end();
++iter)
{
if (name != *iter)
cout << "this book was not you want to read" << endl;
else {
have_read.insert(iter);//应是*iter
want_read.erase(iter);
}
在for循环体中有erase操作,导致了iter失效,可以使用erase返回的迭代器赋给iter,不过这里要避开for循环体内的++iter

读书人网 >C++

热点推荐