我正在使用以下代码来查找类型中的std::vector
字符串string
。但是如何返回特定元素的位置呢?
代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<string> vec;
vector<string>::iterator it;
vec.push_back("H");
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(),"r");
//it++;
if(it!=vec.end()){
cout<<"FOUND AT : "<<*it<<endl;
}
else{
cout<<"NOT FOUND"<<endl;
}
return 0;
}
输出:
FOUND AT : r
预期输出:
FOUND AT : 9