例1:
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
//所有的迭代器接口都是一样的
//c++经常使用迭代器
//vector和deque有额外的功能
//是迭代器操作失效
//顺序容器和关联容器
using namespace std;
//容器都在std名称空间里
bool findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel);
int main()
{
vector<int> a;
deque<int> b;
list<int> c;
a.push_back(0);
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
auto iter1 = a.begin();//或者是vector<int>::iterator iter1=a.begin();
cout << *iter1 << endl;
auto iter2 = a.end();//end指向最后一个的下一个,特殊的位置,标记,范围是不包括end的
//auto m = iter2+ a.size / 2;//指向中间的那个数
if (findInt(iter1, iter2, 3))
cout << "find ok" << endl;
else
cout << "find false" << endl;
system("pause");
return 0;
}
bool findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel)//不检查end
{
while (beg != end)
{
if(*beg == ivel)
break;
else
++beg;
}
if (beg != end)
return true;
else
return false;//做查找的时候是前包后不包的
}
例2:
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
//所有的迭代器接口都是一样的
//c++经常使用迭代器
//vector和deque有额外的功能
//是迭代器操作失效
//顺序容器和关联容器
using namespace std;
//容器都在std名称空间里
vector<int>::iterator findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel)//返回的结果是一个迭代器
{
while (beg != end)
{
if (*beg == ivel)
break;
else
++beg;
}
return beg;//beg不是end就是找到了
}
int main()
{
//用数组创建向量
int a1[] = {0, 1, 2, 3, 4, 5, 6};
vector<int> ivec(a1, a1 + 7);//把数组的名称传过去就成了迭代器,写多一个
vector<int>::iterator ans = findInt(ivec.begin(), ivec.end(), 55);
if (ans == ivec.end())
cout << "error" << endl;
else
cout << "OK" << endl;
vector<int> a;
deque<int> b;
list<int> c;
a.push_back(0);
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
auto iter1 = a.begin();//或者是vector<int>::iterator iter1=a.begin();
cout << *iter1 << endl;
auto iter2 = a.end();//end指向最后一个的下一个,特殊的位置,标记,范围是不包括end的
//auto m = iter2+ a.size / 2;//指向中间的那个数
system("pause");
return 0;
}
例3:
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
//所有的迭代器接口都是一样的
//c++经常使用迭代器
//vector和deque有额外的功能
//是迭代器操作失效
//顺序容器和关联容器
using namespace std;
//容器都在std名称空间里
vector<int>::iterator findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel)//返回的结果是一个迭代器
{
while (beg != end)
{
if (*beg == ivel)
break;
else
++beg;
}
return beg;//beg不是end就是找到了
}
int main()
{
//用数组创建向量
int a1[] = {0, 1, 2, 3, 4, 5, 6};
vector<int> ivec(a1, a1 + 7);//把数组的名称传过去就成了迭代器,写多一个
vector<int>::iterator ans = findInt(ivec.begin(), ivec.end(), 55);
if (ans == ivec.end())
cout << "error" << endl;
else
cout << "OK" << endl;
vector<int> a;
deque<int> b;
list<int> c;
vector<string> svec;
string str;
while (cin >> str)
svec.push_back(str);
for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
{
cout << *iter << endl;
}
//auto iter1 = a.begin();//或者是vector<int>::iterator iter1=a.begin();
//cout << *iter1 << endl;
//auto iter2 = a.end();//end指向最后一个的下一个,特殊的位置,标记,范围是不包括end的
auto m = iter2+ a.size / 2;//指向中间的那个数
system("pause");
return 0;
}
4,
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
using namespace std;
vector<int>::iterator findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel)//返回的结果是一个迭代器
{
while (beg != end)
{
if (*beg == ivel)
break;
else
++beg;
}
return beg;//beg不是end就是找到了
}
int main()
{
list<string> slst;
string str;
while (cin >> str)
slst.push_back(str);
cout << "now list:" << endl;
for (list<string>::iterator iter = slst.begin(); iter != slst.end(); ++iter)
{
cout << *iter << endl;
}
system("pause");
return 0;
}