Question on Vectors

Hello, I just got into the topic on Vectors and now I have to modify a program I did with arrays into vectors instead. So it would be like:

1
2
const int SIZE = 7;
int userNum[SIZE] = {1, 2, 4, 5, 6, 7, 4};


into
1
2
const int SIZE = 7;
vector <int> userNum(SIZE) = {1, 2, 4, 5, 6, 7, 4};


But for some reason I can't initialize the numbers in the vector like I did with the arrays. Can someone please explain why?
Thank You
You can't initialize a vector that way. Here's an example from the vector page on the reference:

1
2
int myints[] = {16,2,77,29};
  vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
Is that how I could initialize the vector ints?
I'm not sure you're understanding fully.
1
2
vector<int> fifth;
vector/*Declare a vector*/<int>/*It's type is int*/ fifth/*It's name is fifth*/


If you want to use it, then you would simply use it's member functions, like shown above.
Last edited on
One way to initialize a vector has been posted. Other approaches will populate the container at runtime, for example:
1
2
3
4
vector< int > v;
v.push_back( 1 );
v.push_back( 2 );
v.push_back( 3 );


If I remember correctly, a static initialization syntax has been included in C++11.
Thanks for the help, I was able to finish my program using fafner's model.
Topic archived. No new replies allowed.