Search code examples
c++functionvectorpush-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 Version)

  • 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?;
  • I don't understand how to fill in the arguments (const value_type& val) & (value_type&& val)
  • 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
  • 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?

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

  • 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?

Solution

  • If you are a beginner, just read over the additional qualifiers like const, & and &&. The methods in the STL are implemented in a way, that they behave consistent over all overloads:

    I will give you a small example here:

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

    Now the more in depth part of the answer:

    First (const value_type& val). The & character signals, that we take the argument by reference, that means we don't copy the argument, but get a fancy pointer, that will behave like the object itself. You may not want, that your variable is changed, if you push it back to a vector. To get a promise, by the programmer of the STL, that he will not change your variable while pushing it back to the vector, he can add the const before the type.

    The reason it is implemented that way, is that it may prevent an unneeded copy. (First copy the argument onto the stack to call push_back and the second time copy it at the position in the vector. The first copy is unnecessary and saved by the const reference.)

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

    myvector.push_back(5);
    

    Since the 5 has no address, it can't be passed as a reference. The compiler can not use the first overload of the function. But the programmer also does not want to waste the time for the copy onto the stack. That is why C++11 added new semantic. A so called rvalue for such temporary objects. If you want to write a function to take such an rvalue, you can do so by using type&& rvalue_variable. The value in this case the 5 is moved onto the stack by using the move constructor of the type. For trivial types like int, this will be the same as the copy constructor. For complex types like std::vector there are shortcuts one can take if one is allowed to rip the temporary object apart. In case of the vector, it does not need to copy all the data in the vector to a new location, but can use the pointer of the old vector in the new object.

    Now we can 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.