/* 迭代器转逆向迭代器事例程序
* - 程序输出:
1 2 3 4 5 6 7 8 9
pos: 5
rpos: 4
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> coll;
for (int i = 1; i <= 9; ++i)
{
coll.push_back(i);
}
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
cout << endl;
vector<int>::iterator pos;
pos = find(coll.begin(), coll.end(), 5);
cout << "pos: " << *pos << endl;
vector<int>::reverse_iterator rpos(pos);
cout << "rpos: " << *rpos << endl;
getchar();
}
/* 安插型迭代器事例程序
* - 程序输出:
1 2 3
1 2 3 44 55
1 2 3 44 55
* - copy(coll.begin(), coll.end(), back_inserter(coll)); 语句无法执行,
* - 考虑是三参数中返回的迭代器可能有失效的,测试后
* - copy(coll.begin(), find(coll.begin(), coll.end(), 3), back_inserter(coll)); //可以执行
* - 输出:1 2 3 44 55 1 2
* - copy(coll.begin(), --coll.end(), back_inserter(coll)); //可以执行
* - 输出:1 2 3 44 55 1 2 3 44
* - copy(coll.begin(), (2+coll.begin()), find(coll.begin(), coll.end(), 55)); //可以执行
* - 输出:1 2 3 44 1
* - copy(coll.begin(), (2+coll.begin()), find(coll.begin(), coll.end(), 55)); //无法执行
* - 第二次复制时55已被覆盖, 且find返回的只是一个节点,不是范围。
* - 请教后知道copy(coll.begin(), coll.end(), back_inserter(coll));复制过程中
* - 由于coll中有新复制的元素加人,导致coll.end()返回的迭代器已经无效,
* - 当此迭代器未使用时,没有问题。但复制最后一个元素时会使用此迭代器,
* - 导致程序报错。
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include "print.hpp"
using namespace std;
int main()
{
vector<int> coll;
back_insert_iterator<vector<int> > iter(coll);
*iter = 1;
iter++;
*iter = 2;
iter++;
*iter = 3;
PRINT_ELEMENTS(coll);
back_inserter(coll) = 44;
back_inserter(coll) = 55;
PRINT_ELEMENTS(coll);
coll.reserve(2 * coll.capacity()); //避免内存重新分配,导致迭代器失效
//copy(coll.begin(), --coll.end(), back_inserter(coll));
PRINT_ELEMENTS(coll);
getchar();
}
/* 安插型迭代器事例程序
* - 程序输出:
1 2 3
1 2 3 44 55
55 44 3 2 1 1 2 3 44 55
*/
#include <iostream>
#include <list>
#include <algorithm>
#include "print.hpp"
using namespace std;
int main()
{
list<int> coll;
back_insert_iterator<list<int> > iter(coll);
*iter = 1;
iter++;
*iter = 2;
iter++;
*iter = 3;
PRINT_ELEMENTS(coll);
back_inserter(coll) = 44;
back_inserter(coll) = 55;
PRINT_ELEMENTS(coll);
copy(coll.begin(), coll.end(), front_inserter(coll));
PRINT_ELEMENTS(coll);
getchar();
}
/* 安插型迭代器事例程序
* - 程序输出:
set: 1 2 3
set: 1 2 3 44 55
list: 1 2 3 44 55
list: 1 1 2 3 44 55 2 3 44 55
*/
#include <iostream>
#include <set>
#include <list>
#include <algorithm>
#include "print.hpp"
using namespace std;
int main()
{
set<int> coll;
insert_iterator<set<int> > iter(coll, coll.begin());
*iter = 1;
iter++;
*iter = 2;
iter++;
*iter = 3;
PRINT_ELEMENTS(coll, "set: ");
inserter(coll, coll.end()) = 44;
inserter(coll, coll.end()) = 55;
PRINT_ELEMENTS(coll, "set: ");
list<int> coll2;
copy(coll.begin(), coll.end(), inserter(coll2, coll2.begin()));
PRINT_ELEMENTS(coll2, "list: ");
copy(coll.begin(), coll.end(), inserter(coll2, ++coll2.begin()));
PRINT_ELEMENTS(coll2, "list: ");
getchar();
}
/* istream迭代器事例程序
* - 程序输入ctrl+z退出
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
istream_iterator<string> cinPos(cin);
ostream_iterator<string> coutPos(cout, " ");
while (cinPos != istream_iterator<string>())
{
advance(cinPos, 2); //ignore the following two strings.
if(cinPos != istream_iterator<string>()) //read and write the third string.
{
*coutPos++ = *cinPos++;
}
}
cout << endl;
getchar();
}