C++进阶:STL迭代器总结

迭代器的重要作用就是让容器和算法解耦合, 或者说让数据和操作解耦合, 算法利用迭代器作为输入, 从而摆脱对容器具体数据的访问. 容器生成的迭代器用于遍历容器中的每个元素, 同时避免暴露容器的内部数据结构和实现细节.

1. 迭代器分类

No. 类别(Category) 举例
1 输入迭代器(Input Iterator) istream
2 输出迭代器(Output Iterator) ostream
3 前向迭代器(Forward Iterator) forward_listunordered_setunordered_multisetunordered_mapunordered_multimap
4 双向迭代器(Bidirectional Iterator) listmapsetmultisetmultimap
5 随机访问迭代器(Random-Access Iterator) vectorarraydequestring、内置数组

2. 迭代器操作

  • 访问
    解引用*->
    下标[]
  • 移动
函数 作用
void advance (InputIterator& it, Distance n) 迭代器向后移动n个元素
distance (InputIterator first, InputIterator last) lastfirst间隔元素个数

3. 迭代器适配器

迭代器适配器主要分三类:

  • 插入适配器
  • 流迭代器适配器
    • 输出流迭代器适配器ostreambuf_iterator
    • 输入流迭代器适配器istreambuf_iterator
  • 逆向迭代器适配器reverse_iterator

3.1 插入适配器

函数 作用
insert_iterator inserter (Container& x, Iterator it) 创建插入迭代器,必须是包含成员函数insert()的容器,比如:顺序容器(vectorlistdeque)、关联容器(mapset)
front_insert_iterator front_inserter (Container& x) 创建前向插入迭代器,必须是包含成员函数push_front()的容器,比如:listdeque
back_insert_iterator back_inserter (Container& x) 创建后向插入迭代器,必须是包含成员函数push_back()的容器,比如:vectorlistdeque
  • inserter.cpp
#include 
#include 
#include 
using namespace std;
int main(){
    int arr[] = {1,2,3,4,5,6};
    vector vec(arr,arr+6);
    vector:: iterator it = vec.begin();
    advance(it,3);
    copy(arr,arr+3,it);
    copy(vec.begin(),vec.end(),ostream_iterator(cout,","));
    cout << endl;
    copy(arr+3,arr+6,inserter(vec,it));
    copy(vec.begin(),vec.end(),ostream_iterator(cout,","));
}
  • front_inserter.cpp
#include 
#include 
#include 
using namespace std;
int main(){
    int arr[] = {1,2,3,4,5,7};
    list vec(5);
    ostream_iterator os_it(cout,",");
    copy(arr,arr+3,front_inserter(vec));
    copy(vec.begin(),vec.end(),os_it);
}
  • back_inserter.cpp
#include 
#include 
#include 
using namespace std;
int main(){
    int arr[] = {1,2,3,4,5,7};
    list vec(5);
    ostream_iterator os_it(cout,",");
    copy(arr+3,arr+6,back_inserter(vec));
    copy(vec.begin(),vec.end(),os_it);
}

3.2 流迭代器适配器

3.2.1 输出流迭代器适配器ostreambuf_iterator

  • ostreambuf_iterator.cpp
#include 
#include 
#include 
using namespace std;
int main(){
    int arr[] = {1,2,3,4,5,6};
    ostream_iterator os_it(cout," ");
    copy(arr,arr+6,os_it);
    vector vec(arr,arr+6);
    ostream_iterator os_it2(cout," ");
    copy(vec.begin(),vec.end(),os_it2);
}

3.2.2 输入流迭代器适配器istream_iterator

  • istream_iterator.cpp
#include 
#include 
#include 
using namespace std;
int main(){
    istream_iterator eos;
    istream_iterator is_it(cin);
    ostream_iterator os_it(cout,",");
    int arr[10];
    copy(is_it,eos,arr);
    copy(arr,arr+10,os_it);
    cout << endl;
    cin.clear();
    vector vec;    
    copy(istream_iterator(cin),eos,back_inserter(vec));
    copy(vec.begin(),vec.end(),os_it);
}

3.3 逆向迭代器适配器reverse_iterator

逆向迭代器适配器:template class reverse_iterator;

  • reverse_iterator.cpp
#include 
#include 
#include 
using namespace std;
int main(){
    int arr[] = {1,2,3,4,5,6};
    vector vec(arr,arr+6);
    copy(vec.begin(),vec.end(),ostream_iterator(cout,","));
    cout << endl;
    copy(
        reverse_iterator::iterator>(vec.end()),
        reverse_iterator::iterator>(vec.begin()),
        ostream_iterator(cout,","));
    cout << endl;
}

4. 迭代器表达式对比

C++进阶:STL迭代器总结_第1张图片

扩展阅读

  • 《Accelerated C++》
  • 《C++程序设计语言(第4部分:标准库)》第33章 STL迭代器

你可能感兴趣的:(C++进阶:STL迭代器总结)