q1 迭代器报错
程序员文章站
2022-04-25 15:15:46
...
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string>s;
int num = 0;
auto c = s.begin();
string t;
while (cin >> t)
s.push_back(t);
for(;c!=s.end();++c)
{
if (*c == "now")
{
++num;
}
}
cout << num << endl;
return 0;
}
报错
然而把声明的迭代器放到for循环里就无事发生
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string>s;
int num = 0;
string t;
while (cin >> t)
s.push_back(t);
for(auto c = s.begin();c!=s.end();++c)
{
if (*c == "now")
{
++num;
}
}
cout << num << endl;
return 0;
}
上一篇: 金蝶容器运维指南