我正在使用以下代碼在std::string
的std::vector
中查找字符串。但是如何返回特定元素的所有位置?如何使用std :: find查找元素的所有位置?
我只是使用std::find
,但我只能返回第一個位置。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<string> vec;
vector<string>::iterator it;
vec.push_back("a");
vec.push_back("i");
vec.push_back("g");
vec.push_back("h");
vec.push_back("l");
vec.push_back("a");
vec.push_back("n");
vec.push_back("d");
vec.push_back("e");
vec.push_back("r");
it=find(vec.begin(),vec.end(),"a");
int pos = distance(vec.begin(), it);
if(it!=vec.end()){
cout<<"FOUND AT : "<<pos<<endl;
}
else{
cout<<"NOT FOUND"<<endl;
}
return 0;
}
我只能得到0,我怎麼能得到5呢?
[查找向量中所有元素出現的索引]的可能副本(http://stackoverflow.com/questions/25846235/finding-the-indexes-of-all-occurrences-of-an-element -in-a-vector) –
std :: find(it + 1,vec.end(),「a」)? – stijn