STL学习笔记----7.STL迭代器
2013-10-17 15:01
309 查看
STL迭代器
一. 迭代器类型
Input迭代器 istream
Output迭代器 ostream
Forward迭代器
Bidirectional迭代器 list, set, multiset, map,multimap
Random access迭代器 vector, deque, string, array
注:随机迭代器,提供”迭代器算术运算”,比如:
Iter
索引位置为n的元素
Iter+=n向前跳n个元素
Iter-=n 向后跳n个元素
Iter1 < iter2 判断iter1是否在iter2之前
[cpp]
view plaincopyprint?
for (int i=0;i<coll.size(); ++i) {
cout << coll.begin() [i]<< ' ';
// iter
}
for (pos =coll.begin(); pos < coll.end()-1; pos += 2) {
//注意,只有随机迭代器才能用 operator <
cout << *pos << ' ';
}
for (int i=0;i<coll.size(); ++i) { cout << coll.begin() [i]<< ' '; // iter } for (pos =coll.begin(); pos < coll.end()-1; pos += 2) { //注意,只有随机迭代器才能用 operator < cout << *pos << ' '; }
二. 迭代器相关辅助函数
1. advance(), 可将迭代器的位置增加或减少,增加的幅度由参数决定。
[cpp]
view plaincopyprint?
#include <iterator>
void advance (InputIterator& pos, Dist n)
#include <iterator> void advance (InputIterator& pos, Dist n)
注:n可以是负值,advance()不检查迭代器是否超过序列end()
[cpp]
view plaincopyprint?
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll;
//insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
list<int>::iterator pos = coll.begin();
//print actual element
cout << *pos << endl; // 1
//step three elements forward
advance (pos, 3);
//print actual element
cout << *pos << endl; // 4
//step three elements backward
advance (pos, -1);
//print actual element
cout << *pos << endl; // 3
}
#include <iostream> #include <list> #include <algorithm> using namespace std; int main() { list<int> coll; //insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } list<int>::iterator pos = coll.begin(); //print actual element cout << *pos << endl; // 1 //step three elements forward advance (pos, 3); //print actual element cout << *pos << endl; // 4 //step three elements backward advance (pos, -1); //print actual element cout << *pos << endl; // 3 }
2. distance(),用来处理两个迭代器之间的距离。
[cpp]
view plaincopyprint?
#include <iterator>
Dist distance (InputIterator pos1, InputIterator pos2)
#include <iterator> Dist distance (InputIterator pos1, InputIterator pos2)
注:两个迭代器必须指向同一个容器,如果不是随机迭代器,则pos2的位置必须与pos1相同或在其后。
[cpp]
view plaincopyprint?
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll;
//insert elements from -3 to 9
for (int i=-3; i<=9; ++i) {
coll.push_back(i);
}
//search element with value 5
list<int>::iterator pos;
pos = find (coll.begin(), coll.end(),
//range
5);
//value
if (pos != coll.end()) {
//process and print difference from the beginning
cout << "difference between beginning and 5: "
<< distance(coll.begin(),pos) << endl;
//8, 第一个位置的distance是0
}
else {
cout << "5 not found" << endl;
}
}
#include <iostream> #include <list> #include <algorithm> using namespace std; int main() { list<int> coll; //insert elements from -3 to 9 for (int i=-3; i<=9; ++i) { coll.push_back(i); } //search element with value 5 list<int>::iterator pos; pos = find (coll.begin(), coll.end(), //range 5); //value if (pos != coll.end()) { //process and print difference from the beginning cout << "difference between beginning and 5: " << distance(coll.begin(),pos) << endl; //8, 第一个位置的distance是0 } else { cout << "5 not found" << endl; } }
3. iter_swap(), 可交换两个迭代器所指的内容。
[cpp]
view plaincopyprint?
#include <algorithm>
void iter_swap (ForwardIterator1 pos1, ForwardIterator2 pos2)
#include <algorithm> void iter_swap (ForwardIterator1 pos1, ForwardIterator2 pos2)
注:交换迭代器pos1和pos2所指的值,迭代器类型不必相同,但其中所指的值必须可以相互赋值。
[cpp]
view plaincopyprint?
#include <iostream>
#include <list>
#include <algorithm>
#include "print.hpp"
using namespace std;
int main()
{
list<int> coll;
//insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
PRINT_ELEMENTS(coll); // 1 2 3 4 5 6 7 8 9
//swap first and second value
iter_swap (coll.begin(), ++coll.begin());
PRINT_ELEMENTS(coll); // 2 1 3 4 5 6 7 8 9
//swap first and last value
iter_swap (coll.begin(), --coll.end());
PRINT_ELEMENTS(coll); // 9 1 3 4 5 6 7 8 2
}
#include <iostream> #include <list> #include <algorithm> #include "print.hpp" using namespace std; int main() { list<int> coll; //insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } PRINT_ELEMENTS(coll); // 1 2 3 4 5 6 7 8 9 //swap first and second value iter_swap (coll.begin(), ++coll.begin()); PRINT_ELEMENTS(coll); // 2 1 3 4 5 6 7 8 9 //swap first and last value iter_swap (coll.begin(), --coll.end()); PRINT_ELEMENTS(coll); // 9 1 3 4 5 6 7 8 2 }
三. 迭代器配接器
1. Reverse(逆向)迭代器
2. Insert(插入)迭代器
3. Stream(流)迭代器
相关文章推荐
- STL学习笔记----7.STL迭代器
- C++ 学习笔记之 STL 队列
- SGI STL 学习笔记三 heap
- STL学习笔记11— —特殊容器stack
- C++ Primer 学习笔记_33_STL实践与分析(7) --容器适配器
- STL学习笔记--排序算法
- STL学习笔记1 List
- STL学习笔记2 List
- STL学习笔记(三):STL的可扩展性
- STL学习笔记-模板知识
- C++ STL学习笔记二 vector向量容器
- 极客班STL与泛型编程 第一周学习笔记
- STL学习笔记— —特殊容器stack
- STL学习笔记之next_permutation(下一个排列)
- STL学习笔记----2.容器的共通操作
- STL学习笔记-- list
- stl 学习笔记 8 续 (function object)
- 【学习笔记】《STL使用入门教程》第一讲:STL的string类型的使用方法
- C++ Primer 学习笔记_29_STL实践与分析(3) --操作步骤集装箱(下一个)
- STL 入门学习笔记二