Vector

could someone explain what is avec.pop_back() ? i'm confuse on this code.
or explain what going on would be great?

the output is:
10
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
**********
#include <vector>
#include <iostream>
#include<fstream>
using namespace std;

int main()
{

  vector<float> avec( 10,3.14159);

  cout << avec.size() << endl;
  for(int i= 0;i< 4;i++) avec.pop_back();
  cout << avec.size() << endl;




  return 0;
}
http://www.cplusplus.com/reference/vector/vector/pop_back/

Removes the last element in the vector, effectively reducing the container size by one.

This destroys the removed element.

Your code calls pop_back in a loop that executes 4 times. Therefore, you remove the last element 4 times. This changes the size of the vector from 10 to 6.
Last edited on
Topic archived. No new replies allowed.