advance 提供给所有的迭代器 Random Access 才有的功能,另迭代器前进,增加的幅度有参数决定。
//iter/advance1.cpp
//advance 迭代器前进
//-
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
typedef std::list<int> IntList;
typedef std::list<int>::iterator IntListIterator;
int main(int argc,char** argv)
{
IntList coll;
for(int i = 1;i <= 9; ++i)
{
coll.push_back(i);
}
IntListIterator pos = coll.begin();
cout << *pos << endl;
advance(pos,3);
cout << *pos << endl;
advance(pos,-1);
cout << *pos << endl;
system("pause");
return 0;
}
//-distance 可处理迭代器之间的距离。
//iter/distance()
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll;
for(int i = -3; i <= 9; ++i)
{
coll.push_back(i);
}
list<int>::iterator pos;
pos = find(coll.begin(), coll.end(), 5);
if(pos != coll.end())
{
cout << "distance between beginning and 5: "
<< distance(coll.begin(),pos)
<<endl;
}
else
{
cout << "5 not found" <<endl;
}
system("pause");
return 0;
}
//iter_swap 可以交换两个迭代器所指的内容
//-
//iter/swap1.cpp
//-
#include <iostream>
#include <list>
#include <algorithm>
#include "print.hpp"
using namespace std;
int main()
{
list<int> coll;
for(int i=1; i<=9; ++i)
{
coll.push_back(i);
}
PRINT_ELEMENTS(coll);
iter_swap(coll.begin(), ++ coll.begin());
PRINT_ELEMENTS(coll);
iter_swap(coll.begin(), -- coll.end());
PRINT_ELEMENTS(coll);
system("pause");
return 0;
}