我使用 istream_iterator 从输入中读取整数(直到 eof)并将它们存储到向量中
之后我想读取一个整数(或者可能是另一种类型的值,例如字符串)。我怎样才能做到这一点?
- 我应该使用 cin (也许以某种方式“清除”它)?我应该使用已经创建的 istream_iterator 吗?
“有问题的”代码如下。它不使用 cin 读取值。
#include<iostream>
#include<iterator>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
// creates two iterators to the begin end end of std input
istream_iterator<int> int_it(cin), eof;
vector<int> int_vec(int_it,eof);
// prints the vector using iterators
cout<<"You gave me the vector: ";
copy(int_vec.begin(),int_vec.end(),ostream_iterator<int>(cout," "));
cout<<endl;
int value;
cout<<"Give me the value you want to search for: ";
cin>>value;
int x=count(int_vec.begin(),int_vec.end(),value);
cout<<"Value "<<value<<" is found "<<x<<" times\n";
}