#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
template <class BidirectionalIterator>
BidirectionalIterator prev (BidirectionalIterator it, typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
#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
template <class ForwardIterator>
ForwardIterator next (ForwardIterator it, typename iterator_traits<ForwardIterator>::difference_type n = 1);
#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
本文链接:http://task.lmcjl.com/news/14977.html