Need help with vector array example

Hi, I'm trying to learn C++ and I am currently studying vector arrays. I am confused by which the book is teaching me how to work with vector arrays. In my book, it says to type these codes;

#include <vector>
#include <iostream>
using namespace std;

int main() {

vector <int> vec(3, 100);

vec.pop_back(); // Removes the final element
cout << "Vector size: " << vec.size() << endl;
cout << "Final element: " << vec.back() << endl;

vec.clear(); //Remove all elements
cout << "Vector size: " << vec.size() << endl;

vec.push_back(200); //Add an element
cout << "Vector size: " << vec.size() << endl;
cout << "First element: " << vec.front() << endl;

return 0;

}

So why is it that the output for this is Vector size: 2 and Final element: 100, when you remove the final element using pop_back();? Are all three of these values in this vector array 100?

Also, why is the out put for this Vector size: 0, when you use the clear()? doesn't this clear the values in this array and the size should remain the same (size 3)?

I apologize if these questions may seem ridiculous, but the book that I have doesn't do a good job in articulating the example they have me to do.
Are all three of these values in this vector array 100?
Exactly. Read this:

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

It is the fill constructor.

doesn't this clear the values in this array and the size should remain the same (size 3)?
No, it removes all elements:

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

More:

http://www.cplusplus.com/reference/vector/vector/?kw=vector
Topic archived. No new replies allowed.