📜  C++中的std :: prev

📅  最后修改于: 2021-05-30 13:39:41             🧑  作者: Mango

std :: prev前进一定数目后,返回指向该元素的迭代器。方向相反的位置。它在头文件中定义

返回参数的副本,该参数在向后的方向上前进了指定的数量。如果它是随机访问迭代器,则该函数仅使用一次运算符+或运算符–进行前进。否则,该函数在复制的迭代器上重复使用递增或递减运算符(运算符++或运算符–-),直到已推进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 
#include 
#include 
#include 
using namespace std;
int main()
{
    // Declaring first container
    deque v1 = { 1, 2, 3, 4, 5, 6, 7 };
  
    // Declaring another container
    deque v2 = { 8, 9, 10 };
  
    // Declaring an iterator
    deque::iterator i1;
  
    // i1 points to 1
    i1 = v1.begin();
  
    // Declaring another iterator to store return
    // value and using std::prev
    deque::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 << "\nv2 = ";
    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可能非常有用。
    // C++ program to demonstrate std::prev
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        // Declaring first container
        list v1 = { 1, 2, 3, 7, 8, 9 };
      
        // Declaring second container
        list v2 = { 4, 5, 6 };
      
        list::iterator i1;
        i1 = v1.begin();
        // i1 points to 1 in v1
      
        list::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 << "\nv2 = ";
        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 :: next代替std :: prev吗?

std :: prev可能引起的一个常见查询是std :: next也可以与否定参数一起使用,以将迭代器向后移动。好吧,答案是肯定的

// C++ program to demonstrate std::next
#include 
#include 
#include 
#include 
using namespace std;
int main()
{
    // Declaring first container
    deque v1 = { 1, 2, 3, 4, 5, 6, 7 };
  
    // Declaring another container
    deque v2 = { 8, 9, 10 };
  
    // Declaring an iterator
    deque::iterator i1;
  
    // i1 points to 1
    i1 = v1.begin();
  
    // Declaring another iterator to store return
    // value and using std::next
    deque::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 << "\nv2 = ";
    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在其语法中有一个参数作为正向迭代器,因此如果我们要使用负数no。为了推进该迭代器,则它至少应为双向迭代器
  • 尽管也可以使用std :: next,但是当意图是向后移动时, std :: prev()会更具可读性
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”