STL学习笔记----7.STL迭代器

 

STL迭代器

一. 迭代器类型

Input迭代器                        istream

Output迭代器                     ostream

Forward迭代器

Bidirectional迭代器          list, set, multiset, map,multimap

Random access迭代器   vector, deque, string, array

注:随机迭代器,提供”迭代器算术运算”,比如:

Iter[n] 索引位置为n的元素

Iter+=n向前跳n个元素

Iter-=n  向后跳n个元素

Iter1 < iter2 判断iter1是否在iter2之前

  1. for (int i=0;i<coll.size(); ++i) { 
  2.    cout << coll.begin() [i]<< ' '// iter[n] 
  3.  
  4. for (pos =coll.begin(); pos < coll.end()-1; pos += 2) {  //注意,只有随机迭代器才能用 operator <  
  5.    cout << *pos << ' ';  
for (int i=0;i<coll.size(); ++i) {
   cout << coll.begin() [i]<< ' ';  // iter[n]
}

for (pos =coll.begin(); pos < coll.end()-1; pos += 2) {  //注意,只有随机迭代器才能用 operator < 
   cout << *pos << ' '; 
}

二. 迭代器相关辅助函数

1. advance(), 可将迭代器的位置增加或减少,增加的幅度由参数决定。

  1. #include <iterator> 
  2. void advance (InputIterator& pos, Dist n) 
#include <iterator>
void advance (InputIterator& pos, Dist n)
n可以是负值,advance() 不检查迭代器是否超过序列end()

  1. #include <iostream> 
  2. #include <list> 
  3. #include <algorithm> 
  4. using namespace std; 
  5.  
  6. int main() 
  7.        list<int> coll; 
  8.  
  9.        //insert elements from 1 to 9 
  10.        for (int i=1; i<=9; ++i) { 
  11.             coll.push_back(i); 
  12.        } 
  13.  
  14.        list<int>::iterator pos = coll.begin(); 
  15.  
  16.        //print actual element 
  17.        cout << *pos << endl;  // 1 
  18.  
  19.        //step three elements forward 
  20.        advance (pos, 3); 
  21.  
  22.        //print actual element 
  23.        cout << *pos << endl; // 4 
  24.  
  25.        //step three elements backward 
  26.        advance (pos, -1); 
  27.  
  28.        //print actual element 
  29.        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(),用来处理两个迭代器之间的距离。
  1. #include <iterator> 
  2. Dist distance (InputIterator pos1, InputIterator pos2) 
#include <iterator>
Dist distance (InputIterator pos1, InputIterator pos2)
注:两个迭代器必须 指向同一个容器,如果不是随机迭代器,则 pos2的位置必须与pos1相同或在其后
  1. #include <iostream> 
  2. #include <list> 
  3. #include <algorithm> 
  4. using namespace std; 
  5.  
  6. int main() 
  7.        list<int> coll; 
  8.  
  9.        //insert elements from -3 to 9 
  10.        for (int i=-3; i<=9; ++i) { 
  11.            coll.push_back(i);  
  12.        } 
  13.  
  14.        //search element with value 5 
  15.        list<int>::iterator pos; 
  16.        pos = find (coll.begin(), coll.end(),        //range 
  17.                    5);                              //value 
  18.  
  19.        if (pos != coll.end()) { 
  20.            //process and print difference from the beginning 
  21.            cout << "difference between beginning and 5: " 
  22.                 << distance(coll.begin(),pos) << endl; //8, 第一个位置的distance是0 
  23.        } 
  24.        else
  25.             cout << "5 not found" << endl;  
  26.        }  
#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(), 可交换两个迭代器所指的内容。
  1. #include <algorithm> 
  2. void iter_swap (ForwardIterator1 pos1, ForwardIterator2 pos2) 
#include <algorithm>
void iter_swap (ForwardIterator1 pos1, ForwardIterator2 pos2)
注:交换迭代器pos1和pos2 所指的值,迭代器类型不必相同,但其中所指的值必须可以相互赋值。

  1. #include <iostream> 
  2. #include <list> 
  3. #include <algorithm> 
  4. #include "print.hpp" 
  5. using namespace std; 
  6.  
  7. int main()  
  8.        list<int> coll; 
  9.  
  10.        //insert elements from 1 to 9 
  11.        for (int i=1; i<=9; ++i) { 
  12.            coll.push_back(i);  
  13.        } 
  14.  
  15.        PRINT_ELEMENTS(coll);  // 1 2 3 4 5 6 7 8 9 
  16.  
  17.        //swap first and second value 
  18.        iter_swap (coll.begin(), ++coll.begin()); 
  19.  
  20.  
  21.        PRINT_ELEMENTS(coll);  // 2 1 3 4 5 6 7 8 9 
  22.  
  23.  
  24.        //swap first and last value 
  25.        iter_swap (coll.begin(), --coll.end()); 
  26.  
  27.        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(流)迭代器

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值