C++ STL一一迭代器相关辅助函数(advance()、distance()、iter_swap())

一、advance()可令迭代器前进

#include <iterator>
void advance(InputIterator& pos,Dist n);
说明:

(1).使名为pos的Input迭代器步进(或步退)n个元素

(2).对bidirectional迭代器和Random Access迭代器而言,n可为负值,表示向后退。

(3).Dist是个template型别,通常是个整数型别。

(4).advance()并不检查迭代器是否超过序列的end(),所有,对序列尾端调用operator++是一种未定义的操作行为。

示例:

#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;

    // step three elements forward
    advance (pos, 3);

    // print actual element
    cout << *pos << endl;

    // step one element backward
    advance (pos, -1);

    // print actual element
    cout << *pos << endl;
}
程序输出结果:

1
4
3

二、distance()可处理迭代器之间的距离

#include <iterator>
Dist distance(InputIterator pos1, InputIterator pos2);
说明:

(1).传回两个Input迭代器pos1,pos2之间的距离

(2).两个敌当前必须指向同一容器

(3).如果不是随机存取迭代器,则从pos1开始往前走必须能够到达pos2,亦即pos2的位置必须与pos1相同或在其后。

(4).回返值Dist的型别由迭代器决定

iterator_traits<InputIterator>::difference_type
示例:

#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;
    }
    else {
        cout << "5 not found" << endl;
    }
}
运行结果:

difference between beginning and 5: 8


三、iter_swap()可交换两个迭代器所指内容(表示所指元素的内容)

#include <algorithm>
void iter_swap(ForwardIterator1 pos1,ForwardIterator2 pos2);
说明:

(1).交换迭代器pos1和pos2所指的值

(2).迭代器的型别不必相同,但其所指的两个值必须可以相互赋值。

示例:

//print.hpp
#include <iostream>

/* PRINT_ELEMENTS()
 * - prints optional C-string optcstr followed by
 * - all elements of the collection coll
 * - separated by spaces
 */
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}
#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);

    // swap first and second value
    iter_swap (coll.begin(), ++coll.begin());

    PRINT_ELEMENTS(coll);

    // swap first and last value
    iter_swap (coll.begin(), --coll.end());

    PRINT_ELEMENTS(coll);
}

运行结果:

1 2 3 4 5 6 7 8 9
2 1 3 4 5 6 7 8 9
9 1 3 4 5 6 7 8 2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值