我究竟如何使用函数 push_back 和 pop_back()?我在下面的链接中查找了它们,但仍然不明白

How exactly do I use the functions push_back and pop_back()? I looked them up in the following liks but still don't understand

http://www.cplusplus.com/reference/vector/vector/push_back/(C++11 版本)

http://www.cplusplus.com/reference/vector/vector/pop_back/

这应该向您展示如何同时使用它们。

push_back():

std::vector<int> vec = { 0, 1, 2 };
vec.push_back(3);

pop_back():

vec.pop_back();
vec.pop_back();

如果您需要更多说明:

push_back(const T& val) 将其参数添加到向量的末尾,有效地将大小增加 1,前提是向量容量将超过其大小。

pop_back() 不接受任何参数并删除向量的最后一个元素,有效地将大小减小 1。

更新:

我正在一一解答你的问题,有什么不明白的地方,请告诉我。

What is the difference and/or advantages of void push_back (const value_type& val); & void push_back (value_type&& val) and which do you suggest I use?;

在 C++11 之前,右值引用不存在。这就是 push_back 被实现为 vector.push_back(const value_type& val) 的原因。如果您有支持 C++11 或更高版本的编译器,std::vector.push_back() 将针对 const 左值引用和右值引用进行重载。

I don't understand how to fill in the arguments (const value_type& val) & (value_type&& val)

作为程序员,您不会选择如何将参数传递给push_back(),在大多数情况下,编译器会自动为您完成。

I don't understand the second sentence under the parameter section. (It's a bit too wordy for me to get). I do understand what val is though

value_type 等于您声明的 vector 的 type。如果用 std::string 声明向量,则它只能容纳 std::string

std::vector<std::string> vec;
vec.push_back("str"); // Ok. "str" is allowed.
vec.push_back(12);    // Compile-time error. 12 is not allowed.

如果您是初学者,只需阅读额外的限定符,如 const、& 和 &&。 STL 中的方法以某种方式实现,它们在所有重载上表现一致:

这里给大家举个小例子:

std::vector<int> myvector;
myvector.push_back(5);
int five = 5;
myvector.push_back(five);

现在回答更深入的部分:

首先(const value_type& val)。 & 字符表示我们通过引用获取参数,这意味着我们不复制参数,而是获得一个奇特的指针,其行为类似于对象本身。 如果将变量推回向量,您可能不希望更改变量。为了得到 STL 程序员的承诺,他不会在将变量推回向量时更改您的变量,他可以在类型前添加 const

之所以以这种方式实施,是因为它可以防止不必要的复制。 (第一次将参数复制到堆栈上以调用 push_back,第二次将其复制到向量中的位置。第一次复制是不必要的,由 const 引用保存。)

这一切都很好也很简单,但在某些情况下,不允许编译器获取值的引用并将其传递给函数。在临时值的情况下,没有引用可取,因为内存中没有变量。以下面这行为例。

myvector.push_back(5);

由于 5 没有地址,因此不能作为参考传递。编译器不能使用函数的第一个重载。但是程序员也不希望把时间浪费在复制到栈上。这就是 C++11 添加新语义的原因。此类临时对象的所谓右值。如果您想编写一个函数来获取这样的右值,可以使用 type&& rvalue_variable 来实现。本例中的值 5 通过使用该类型的移动构造函数移动到堆栈上。对于像 int 这样的普通类型,这将与复制构造函数相同。对于像 std::vector 这样的复杂类型,如果允许将临时对象分开,则可以采取一些捷径。在vector的情况下,不需要将vector中的所有数据复制到新位置,而是可以在新对象中使用旧vector的指针。

现在我们可以再看一下这个例子:

std::vector<int> myvector;
myvector.push_back(5); // push_back(const int&) can't be applied. The compiler chooses push_back(int&&) for us
int five = 5;
myvector.push_back(five); // push_back(const int&) can be applied and is used by the compiler
// The resulting vector after this has the two values [5, 5]
// and we see, that we don't need to care about it.

What is the difference and/or advantages of void push_back (const value_type& val); & void push_back (value_type&& val) and which do you suggest I use?

void push_back(const value_type&) 接受一个参数,然后将其复制到 vector 中。这意味着新元素被初始化为传递参数的副本,由适当的分配器定义。

void push_back(value_type&&) 接受一个参数,然后将其移入容器(这种类型的表达式称为 右值表达式)。

两者的使用取决于您想要达到的结果。

I don't understand how to fill in the arguments (const value_type& val) & (value_type&& val)

在大多数情况下,您不应该考虑使用哪个版本,因为编译器会为您解决这个问题。将为任何右值参数调用第二个版本,为其余参数调用第一个版本。在极少数情况下,当您想确保调用第二个重载时,您可以使用 std::move 将参数表达式显式转换为 xvalue(一种右值)。

I don't understand the second sentence under the parameter section. (It's a bit too wordy for me to get). I do understand what val is though

有问题的句子是:

Member type value_type is the type of the elements in the container, defined in vector as an alias of its first template parameter (T).

这意味着 value_typevector 的元素类型相同。例如,如果您有 vector<int>,则 value_typeint 相同,对于 vector<string>value_typestring

因为vector不是普通类型,而是模板,所以在定义变量时必须指定一个类型参数(在vector之后的尖括号<>中)。在 vector 模板规范中,此类型参数 T 的别名是 value_type:

typedef T value_type; 

It doesn't give an example I can understand real well. Can I get other examples using vectors or some video links that explain the use of the function in practice better?

您需要记住的主要事情是 vector 表现得像一个简单的数组,但具有动态可变的大小和一些附加信息,例如它的长度。 push_back 只是一个函数,它在这个伪数组的末尾添加一个新元素。当然,有很多细微的细节,但在大多数情况下都是无关紧要的。

基本用法是这样的:

vector<int> v; // v is empty
v.push_back(1); // v now contains one element

vector<float> v2 { 1.0, 2.0 }; // v2 is now a vector with two elements
float f = v2.pop_back(); // v2 now has one element, and f is now equals 2.0

了解其工作原理的最好方法是亲自尝试使用它。