21C++
程序清单15.1 在vector中查找元素及其位置
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int main(){
vector<int>a;
a.push_back(1);
a.push_back(3);
a.push_back(5);
a.push_back(7);
//用迭代器进行遍历
vector<int>::iterator walker = a.begin(); // 或 auto walker = a.begin();
while(walker != a.end()){
cout << *walker << endl;
walker++;
}
//迭代器
vector<int>::iterator finder = find(a.begin(), a.end(), 3);//或auto finder = find(a.begin(), a.end(), 3);
if(finder != a.end()){
int p = distance(a.begin(), finder);
cout << "position is " << p;
}
}