简体   繁体   English

如何在C ++中存储从Vector弹出的项目

[英]How to store the item popped off from Vector in C++

I am making a program where i have to incorporate the use of vectors. 我正在编写一个程序,其中必须结合使用向量。 But the problem is that i need to store the value of the popped off item from the vector. 但是问题是我需要存储从向量中弹出项目的值。 The vector's popback() doesn't really help with saving as it takes no arguments and has void return type. 向量的popback()并没有真正的帮助,因为它没有参数,返回类型为void。 Help would be highly appreciated. 帮助将不胜感激。

Try this example 试试这个例子

std::vector<int> v = { 1, 2, 3, 4, 5 };

while ( !v.empty() )
{
   int x = v.back();
   v.pop_back();
   std::cout << x << ' ';
}

std::cout << std::endl;

There are several ways to access elements of a vector as for example back(), front(), operator [], at(), *it where it is an iterator. 有多种方法可以访问向量的元素,例如back(),front(),operator [],at(),* it(它是迭代器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM