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

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

  • What is the difference and / or advantages void push_back (const value_type & val); and void push_back (value_type & val) and which do you suggest I am using ?;
  • I don't understand how to fill in the arguments (const value_type & val) and (value_type & val)
  • I don't understand the second sentence in the options section. (This is too wordy for me.) I understand that val though
  • This does not provide an example that I can understand well. Can I get other examples using vectors or some video links that better explain the use of the function in practice?

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

  • This does not provide an example that I can understand well. Can I get other examples using vectors or some video links that better explain the use of the function in practice?
+3


source to share


3 answers


If you are a beginner, just read additional qualifiers like const, and & &. Methods in STL are implemented in such a way that they behave consistently across all overloads:

I'll give a small example here:

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

      

Now, the deeper part of the answer:

First (const value_type& val)

. The symbol and symbol signal that we are accepting an argument by reference, which means that we are not copying the argument, but rather a fancy pointer that will behave like the object itself. You might not want your variable to be changed if you push it back to the vector. To get the promise of the STL programmer that he will not modify your variable and return it back to the vector, he can add const

before the type.

The reason this is implemented is because it can prevent unnecessary copy. (First copy the argument onto the stack to be called, push_back

and a second time copy it to a position in the vector. The first copy is not needed and is stored by reference const

.)



This is all good and simple, but there are times when the compiler is not allowed to reference a value and pass it to a function. In the case of temporary values, references are not accepted because there is no variable in memory. Take the following line for example.

myvector.push_back(5);

      

Since it 5

does not have an address, it cannot be passed as a link. The compiler cannot use the first function overload. But the programmer also doesn't want to waste time copying onto the stack. This is why C ++ 11 added new semantics. The so-called rvalue for such temporary objects. If you want to write a function to get such an rvalue, you can do so using type&& rvalue_variable

. The value in this case5

is pushed onto the stack using the type's move constructor. For trivial types like int, this will be the same as the copy constructor. For complex types like std :: vector, there are shortcuts to take if it is allowed to break the temporary object. In the case of a vector, it does not need to copy all the data in the vector to the new location, but can use the pointer of the old vector in the new object.

Now we'll look at the example again:

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.

      

+3


source


This should show you how you can use both of them.

push_back ():

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

      

pop_back ():

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

      

If you need more clarification:

push_back(const T& val)

adds its parameter to the end of the vector, effectively increasing the size by 1 if the vector capacity is exceeded.

pop_back()

takes no parameters and removes the last element of the vector, effectively reducing the size by 1.

Update



I am trying to solve your questions one by one, if there is anything unclear, let me know.

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

Prior to C ++ 11, rvalue references did not exist. That is why it push_back

was implemented as vector.push_back(const value_type& val)

. If you have a compiler that supports C ++ 11 or later, it std::vector.push_back()

will be overloaded for constant lvalue references and rvalue references.

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

You, as a programmer, do NOT choose how to pass arguments to push_back (), the compiler does this for you automatically in most cases.

I don't understand the second sentence in the options section. (This is too wordy for me.) I do understand what val is, though

value_type

is equal to the type of the vector you declared. If a vector is declared with std::string

, it can only hold 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.

      

+3


source


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

void push_back(const value_type&)

takes an argument, which is then copied to vector

. This means that the new element is initialized as a copy of the passed argument, as defined by the appropriate allocator.

void push_back(value_type&&)

takes an argument, which is then moved to a container (this type of expression is called rvalue expressions).

Using one of the two depends on the results you want to achieve.

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

Most of the time, you shouldn't have to think about which version to use as the compiler will take care of that for you. The second version will be called for any rvalue argument, and the first for the rest. In the rare case where you want to provide a second overload, you can use std::move

to explicitly convert an argument expression to an xvalue (which is a kind of rvalues).

I don't understand the second sentence in the options section. (This is too wordy for me.) I do understand what val is, though

Proposed proposal:

The value_type member type is the type of items in the container, defined in the vector as an alias for its first template parameter (T).

This means that value_type

is the same type as the element type vector

. For example, if you have vector<int>

, then value_type

matches with int

, and for vector<string>

value_type

- string

.

Since a vector is not a regular type, but a template, you must specify the type parameters (which comes in angle brackets <>

after vector

) when you define the variable. Inside the template, vector

this parameter parameter is T

then flattened with value_type

:

typedef T value_type; 

      

This does not provide an example that I understand well. Can I get other examples using vectors or some video links that better explain the use of the function in practice?

The main thing to remember is something that vector

behaves like a simple array, but dynamically resizable and some additional information such as its length. push_back

is just a function that adds a new element to the end of this pseudo-array. There are, of course, many subtle details, but in most cases they are irrelevant.

Primary use:

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

      

The best way to understand how it works is to try it yourself.

+2


source







All Articles