3

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?
Gulp Dragondawn
  • 101
  • 3
  • 10
  • 1
    *"and which do you suggest I use?"* Generally, the compiler will pick the right one for you. – Baum mit Augen Mar 21 '17 at 14:39
  • 2
    [Here](http://en.cppreference.com/w/cpp/container/vector) is a better reference btw. – Baum mit Augen Mar 21 '17 at 14:40
  • 1
    You could've easily looked that up, there are plenty of resources. However, I guess it couldn't hurt to have a comprehensive answer to this on SO (unless it already exists?), so I'll give it an upvote. – domsson Mar 21 '17 at 14:42
  • Thanks for the up vote _domdom_. I know how to look these things up, but I have problems sorting through vast information. Too many different references plus too many different approaches times the vast examples of push_back() & pop_back use in actual code equals brain transplant. – Gulp Dragondawn Mar 22 '17 at 14:37

3 Answers3

3

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 iff the vector capacity will be exceeded by its size.

pop_back() doesn't take any parameters and removes the last element of the vector, effectively reducing the size by 1.

Update:

I'm trying to tackle your questions one by one, if there is anything unclear, let me know.

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

Prior to C++11, rvalue-references didn't exist. That's why push_back was implemented as vector.push_back(const value_type& val). If you have a compiler that supports C++11 or later, std::vector.push_back() will be overloaded for const lvalue references and rvalue references.

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

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

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 is equal to the type of vector that you declared. If a vector is declared with std::string, then 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.
ScY
  • 91
  • 1
  • 7
  • This is probably the exact answer I'm looking for except it lacks some things. 1st, _push_back()_ is in the code, _but pop_back()_ isn't; 2nd, what does the **3** in _vec.pushback(3);_ refer to/do; 3rd, can you explain how the push_back() argument structure _(const value_type& val)_ fits with a declared vector; 4th, can you make definite & complete _main()_, _push_back()_ and _pop_back()_ functions in the example (pseudocode is fine)? Any of these changes will be much appreciated! – Gulp Dragondawn Mar 22 '17 at 15:08
  • 1
    @GulpDragondawn For further reference [see](http://en.cppreference.com/w/cpp/container/vector) – ScY Mar 22 '17 at 21:55
  • ScY, I understand vectors. I understand using vectors. I **don't** understand the two functions **_push_back()_ & _pop_back()_**. The link you gave only tells me about vectors. I need alternative references to the same sites explanations of said function. If I'm missing something here please be patient and explain as I have Autism and have a hard time understanding conversations. – Gulp Dragondawn Mar 23 '17 at 14:34
  • Basically **ScY** I need a good pseudocode (bare bones) understanding of the two functions before understanding what type of meat covers it. – Gulp Dragondawn Mar 23 '17 at 14:53
3

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.
OutOfBound
  • 1,914
  • 14
  • 31
2

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&) takes an argument, that is then copied into the vector. This means that a new element is initialized as a copy of the passed argument, as defined by an appropriate allocator.

void push_back(value_type&&) takes an argument, that is then moved into the container (this type of expressions are called rvalue expressions).

The usage of either of two depends on the results you want to achieve.

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

In most cases you shouldn't think about which version to use, as compiler will take care of this for you. Second version will be called for any rvalue argument and the first one for the rest. In a rare case when you want to ensure the second overload is called you can use std::move to explicitly convert the argument expression into xvalue (which is a kind of rvalues).

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

The sentence in question is:

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).

This means that value_type is the same type as the type of vector's elements. E.g., if you have vector<int> then value_type is the same as int and for vector<string> the value_type is string.

Because vector is not an ordinary type, but a template, you must specify a type parameters (which goes into angle brackets <> after vector) when defining a variable. Inside the vector template specification this type parameter T is then aliased with 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?

The main thing you need to remember is that vector behaves like a simple array, but with dynamicly changeable size and some additional information, like its length. push_back is simply a function that adds a new element at the end of this pseudo-array. There is, of course, a lot of subtle details, but they are inconsequential in most of the cases.

The basic usage is like this:

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 using it yourself.

Gasper
  • 2,752
  • 1
  • 12
  • 16