std::prev 返回一个迭代器,该迭代器在反向前进一定数量的位置后指向元素。它在头文件中定义 .
null
信息技术 返回参数的副本 按规定数量向后推进。如果它是一个随机访问迭代器,则该函数只使用一次运算符+或运算符–来前进。否则,该函数会在复制的迭代器上重复使用递增或递减运算符(运算符++或运算符–),直到n个元素被提升。
语法:
BidirectionalIterator prev (BidirectionalIterator it, typename iterator_traits::difference_type n = 1); it: Iterator to the base position. difference_type: It is the numerical type that represents distances between iterators of the BidirectionalIterator type. n: Total no. of positions by which the iterator has to be advanced. In the syntax, n is assigned a default value 1 so it will atleast advance by 1 position. Returns: It returns an iterator to the element n positions before it.
// C++ program to demonstrate std::prev #include <iostream> #include <iterator> #include <deque> #include <algorithm> using namespace std; int main() { // Declaring first container deque< int > v1 = { 1, 2, 3, 4, 5, 6, 7 }; // Declaring another container deque< int > v2 = { 8, 9, 10 }; // Declaring an iterator deque< int >::iterator i1; // i1 points to 1 i1 = v1.begin(); // Declaring another iterator to store return // value and using std::prev deque< int >::iterator i2; i2 = std::prev(v1.end(), 3); // Using std::copy std::copy(i1, i2, std::back_inserter(v2)); // Remember, i1 stills points to 1 // and i2 points to 5 // v2 now contains 8 9 10 1 2 3 4 // Displaying v1 and v2 cout << "v1 = " ; int i; for (i = 0; i < 7; ++i) { cout << v1[i] << " " ; } cout << "v2 = " ; for (i = 0; i < 7; ++i) { cout << v2[i] << " " ; } return 0; } |
输出:
v1 = 1 2 3 4 5 6 7 v2 = 8 9 10 1 2 3 4
这有什么帮助?
- 在列表中移动迭代器: 因为,列表支持 双向迭代器 ,只能使用++和––运算符递增。所以,如果我们想让迭代器前进一个以上的位置,那么 下一个 如果我们想减少迭代器,那么std::prev可能非常有用。
// C++ program to demonstrate std::prev
#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
using
namespace
std;
int
main()
{
// Declaring first container
list<
int
> v1 = { 1, 2, 3, 7, 8, 9 };
// Declaring second container
list<
int
> v2 = { 4, 5, 6 };
list<
int
>::iterator i1;
i1 = v1.begin();
// i1 points to 1 in v1
list<
int
>::iterator i2;
// i2 = v1.end() - 3;
// This cannot be used with lists
// so use std::prev for this
i2 = std::prev(v1.end(), 3);
// Using std::copy
std::copy(i1, i2, std::back_inserter(v2));
// v2 now contains 4 5 6 1 2 3
// Displaying v1 and v2
cout <<
"v1 = "
;
int
i;
for
(i1 = v1.begin(); i1 != v1.end(); ++i1) {
cout << *i1 <<
" "
;
}
cout <<
"v2 = "
;
for
(i1 = v2.begin(); i1 != v2.end(); ++i1) {
cout << *i1 <<
" "
;
}
return
0;
}
输出:
v1 = 1 2 3 7 8 9 v2 = 4 5 6 1 2 3
说明: 在这里,只要看看如果我们只想复制列表中选定的部分,那么我们可以使用std::prev,否则我们不能使用任何+=,-=运算符和列表支持的双向迭代器。因此,我们使用std::prev并直接将迭代器从末尾向后移动三个位置。
我们能用吗 下一个 取代std::prev?
std::prev可能出现的一个常见查询是,std::next也可以与否定参数一起使用,以向后移动迭代器。答案是 对 .
// C++ program to demonstrate std::next #include <iostream> #include <iterator> #include <deque> #include <algorithm> using namespace std; int main() { // Declaring first container deque< int > v1 = { 1, 2, 3, 4, 5, 6, 7 }; // Declaring another container deque< int > v2 = { 8, 9, 10 }; // Declaring an iterator deque< int >::iterator i1; // i1 points to 1 i1 = v1.begin(); // Declaring another iterator to store return // value and using std::next deque< int >::iterator i2; i2 = std::next(v1.end(), -3); // Using std::copy std::copy(i1, i2, std::back_inserter(v2)); // Remember, i1 stills points to 1 // and i2 points to 5 // v2 now contains 8 9 10 1 2 3 4 // Displaying v1 and v2 cout << "v1 = " ; int i; for (i = 0; i < 7; ++i) { cout << v1[i] << " " ; } cout << "v2 = " ; for (i = 0; i < 7; ++i) { cout << v2[i] << " " ; } return 0; } |
输出:
v1 = 1 2 3 4 5 6 7 v2 = 8 9 10 1 2 3 4
说明: 所以,我们刚刚用std::next代替std::prev,并将第二个参数从3改为-3,它仍然有相同的用途。
我们也可以使用std::next,但需要记住两件事:
- 因为std::next在其语法中有一个参数作为正向迭代器,所以如果我们想使用负数来推进该迭代器,那么它应该是 至少有一个双向迭代器 .
- 虽然也可以使用std::next,但是 std::prev()将更具可读性 当意图明确是向后移动时。
本文由 辛格先生 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END