1. 程式人生 > 其它 >C++ STL prev()和next()函式(深入瞭解,一文學會)

C++ STL prev()和next()函式(深入瞭解,一文學會)

轉自:https://blog.csdn.net/qq_37529913/article/details/120008481

C++ STL advance()函式(深入瞭解,一文學會)》一節中,詳細講解了 advance() 函式的功能,其可以將指定迭代器前移或後移 n 個位置的距離。

C++ STL prev()和next()函式(深入瞭解,一文學會)目錄

1 advance() 函式移動的是源迭代器

2 prev()函式

3 next()函式

1 advance() 函式移動的是源迭代器
但值得一提的是,advance() 函式移動的是源迭代器,舉個例子:

#include <iostream>     //
std::cout #include <iterator> // std::advance #include <vector> using namespace std; //建立一個 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;

通過程式的執行結果不難看出,advance() 函式沒有任何返回值,其移動的是 it 迭代器本身。

這就產生一個問題,若我們不想移動 it 迭代器本身,而僅僅是想在 it 迭代器的基礎上,得到一個移動指定位置的新迭代器,顯然 advance() 函式是不合適的,這時就可以使用 C++ STL 標準庫提供的另外 2 個函式,即 prev() 和 next() 函式。

2 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>     // cout
#include <iterator>     // next
#include <list>         // list
using namespace std;
 
//建立並初始化一個 list 容器
list<int> mylist{ 1,2,3,4,5 };
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;

可以看到,當 it 指向 mylist 容器最後一個元素之後的位置時,通過 prev(it, 2) 可以獲得一個新迭代器 newit,其指向的是距離 it 左側 2 個元素的位置(其儲存的是元素 4);當 it 指向 mylist 容器中首個元素時,通過 prev(it, -2) 可以獲得一個指向距離 it 右側 2 個位置處的新迭代器。

注意,prev() 函式自身不會檢驗新迭代器的指向是否合理,需要我們自己來保證其合理性。

3 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;
 
//建立並初始化一個 list 容器
list<int> mylist{ 1,2,3,4,5 };
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;

可以看到,和 prev() 函式恰好相反,當 n 值為 2 時,next(it, 2) 函式獲得的新迭代器位於 it 迭代器的右側,距離 2 個元素;反之,當 n 值為 -2 時,新迭代器位於 it 迭代器的左側,距離 2 個元素。

注意,和 prev() 函式一樣,next() 函式自身也不會檢查新迭代器指向的有效性,需要我們自己來保證。