*** [算法]find, for_each及reverse_iterator的应用

#include <iostream>
#include <algorithm>
#include <deque>
using namespace std;

/*
    demonstrate algorithm: find, for_each
    demonstrate reverse_iterator
*/

void print (int x)
{
    cout << x << " ";
}

int main()
{
    deque<int> d;
    for (int i=1; i<10; i++)
    {
        d.push_back(i);
    }
    
    deque<int>::iterator pos1 = find(d.begin(), d.end(), 3);
    deque<int>::iterator pos2 = find(d.begin(), d.end(), 7);
    for_each(pos1, pos2, print);
    cout << endl;
    
    // when rpos1++, it moves backward instead of forward
    deque<int>::reverse_iterator rpos1(pos1);    
    deque<int>::reverse_iterator rpos2;
    rpos2 = (deque<int>::reverse_iterator)pos2;
    for_each(rpos2, rpos1, print); //左闭右开
    cout << endl;

    // 当为rpos1赋值为pos1时,rpos1实际指向了pos1的前一个元素
    cout << "pos1= " << *pos1 << ", rpos1: " << *rpos1 << endl;
    cout << "pos2= " << *pos2 << ", rpos2: " << *rpos2 << endl;
    
    return 0;
}

 

posted @ 2019-01-04 20:37  super行者  阅读(190)  评论(0编辑  收藏  举报