#include<iostream> #include<iterator> #include<cstring> #include<string> #include<vector> using namespace std; int main() { ? ? string a[5] = { "Jack_Ma","HuaTeng_Ma","QiangDong_Liu","YanHong_Li","Lei_Ding" }; ? ? vector <string> v(a, a + 5);//用数组初始化vector
? ? vector <string> ::iterator i;//迭代器 ? ? i = find(v.begin(), v.end(),"Lei_Ding"); ? ? if (i == v.end()) cout << "没找到" << endl; ? ? else cout << "找到了" << endl; ? ? cout << *i << " "; //可用指针 ? ? cout << *v.begin() << " "; ? ? cout << i - v.begin() << endl; //索引位置
//下面的和上面效果一样 // ? string * pStr=0; int i=-1; // ? for (auto & item:v) //c++11语法 // ? ? {i++;if (item=="Lei_Ding") pStr=&item;} // ? if(pStr==0) cout << "没找到" << endl; // ? else ?cout << "找到了" << endl; // ? cout<<*pStr<< " "; // ? cout << *v.begin() << " "; // ? cout << i ?<< endl; //索引位置
? ? system("pause"); ? ? return 0; }
|