C++ STL prev()和next()函数用法详解

 
C++ STL advance()函数》一节中,详细讲解了 advance() 函数的功能,其可以将指定迭代器前移或后移 n 个位置的距离。

但值得一提的是,advance() 函数移动的是源迭代器,举个例子:
#include <iostream>     // std::cout
#include <iterator>     // std::advance
#include <vector>
using namespace std;
int main() {
    //创建一个 vector 容器
    vector<int> myvector{ 1,2,3,4 };
    //it为随机访问迭代器,其指向 myvector 容器中第一个元素
    vector<int>::iterator it = myvector.begin();
    //输出 it 迭代器指向的数据
    cout << "移动前的 *it = " << *it << endl;

    //借助 advance() 函数将 it 迭代器前进 2 个位置
    advance(it, 2);
    cout << "移动后的 *it = " << *it << endl;
    return 0;
}
程序执行结果为:

移动前的 *it = 1
移动后的 *it = 3

通过程序的运行结果不难看出,advance() 函数没有任何返回值,其移动的是 it 迭代器本身。

这就产生一个问题,若我们不想移动 it 迭代器本身,而仅仅是想在 it 迭代器的基础上,得到一个移动指定位置的新迭代器,显然 advance() 函数是不合适的,这时就可以使用 C++ STL 标准库提供的另外 2 个函数,即 prev() 和 next() 函数。

C++ STL prev()函数

prev 原意为“上一个”,但 prev() 的功能远比它的本意大得多,该函数可用来获取一个距离指定迭代器 n 个元素的迭代器。

prev() 函数的语法格式如下:

template <class BidirectionalIterator>
    BidirectionalIterator prev (BidirectionalIterator it, typename iterator_traits<BidirectionalIterator>::difference_type n = 1);

其中,it 为源迭代器,其类型只能为双向迭代器或者随机访问迭代器;n 为指定新迭代器距离 it 的距离,默认值为 1。该函数会返回一个距离 it 迭代器 n 个元素的新迭代器。

注意,当 n 为正数时,其返回的迭代器将位于 it 左侧;反之,当 n 为负数时,其返回的迭代器位于 it 右侧。

举个例子:
#include <iostream>     // std::cout
#include <iterator>     // std::next
#include <list>         // std::list
using namespace std;
int main() {
    //创建并初始化一个 list 容器
    std::list<int> mylist{ 1,2,3,4,5 };
    std::list<int>::iterator it = mylist.end();
    //获取一个距离 it 迭代器 2 个元素的迭代器,由于 2 为正数,newit 位于 it 左侧
    auto newit = prev(it, 2);
    cout << "prev(it, 2) = " << *newit << endl;
   
    //n为负数,newit 位于 it 右侧
    it = mylist.begin();
    newit = prev(it, -2);
    cout << "prev(it, -2) = " << *newit;
    return 0;
}
程序执行结果为:

prev(it, 2) = 4
prev(it, -2) = 3

可以看到,当 it 指向 mylist 容器最后一个元素之后的位置时,通过 prev(it, 2) 可以获得一个新迭代器 newit,其指向的是距离 it 左侧 2 个元素的位置(其存储的是元素 4);当 it 指向 mylist 容器中首个元素时,通过 prev(it, -2) 可以获得一个指向距离 it 右侧 2 个位置处的新迭代器。

注意,prev() 函数自身不会检验新迭代器的指向是否合理,需要我们自己来保证其合理性。

C++ STL next()函数

和 prev 相反,next 原意为“下一个”,但其功能和 prev() 函数类似,即用来获取一个距离指定迭代器 n 个元素的迭代器。

next() 函数的语法格式如下:

template <class ForwardIterator>
    ForwardIterator next (ForwardIterator it, typename iterator_traits<ForwardIterator>::difference_type n = 1);

其中 it 为源迭代器,其类似可以为前向迭代器、双向迭代器以及随机访问迭代器;n 为指定新迭代器距离 it 的距离,默认值为 1。该函数会返回一个距离 it 迭代器 n 个元素的新迭代器。

需要注意的是,当 it 为前向迭代器时,n 只能为正数,该函数最终得到的新迭代器位于 it 右侧;当 it 为双向迭代器或者随机访问迭代器时,若 n 为正数,则得到的新迭代器位于 it 右侧,反之位于 it 左侧。

举个例子:
#include <iostream>     // std::cout
#include <iterator>     // std::next
#include <list>         // std::list
using namespace std;
int main() {
    //创建并初始化一个 list 容器
    std::list<int> mylist{ 1,2,3,4,5 };
    std::list<int>::iterator it = mylist.begin();
    //获取一个距离 it 迭代器 2 个元素的迭代器,由于 2 为正数,newit 位于 it 右侧
    auto newit = next(it, 2);
    cout << "next(it, 2) = " << *newit << endl;
   
    //n为负数,newit 位于 it 左侧
    it = mylist.end();
    newit = next(it, -2);
    cout << "next(it, -2) = " << *newit;
    return 0;
}
程序执行结果为:

next(it, 2) = 3
next(it, -2) = 4

可以看到,和 prev() 函数恰好相反,当 n 值为 2 时,next(it, 2) 函数获得的新迭代器位于 it 迭代器的右侧,距离 2 个元素;反之,当 n 值为 -2 时,新迭代器位于 it 迭代器的左侧,距离 2 个元素。

注意,和 prev() 函数一样,next() 函数自身也不会检查新迭代器指向的有效性,需要我们自己来保证。