3
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by Narue
0

You can't use initializers with vectors. You need to use something like

vector<int> ray;
ray.reserve(7); // Not necessary -- this just prevents extra reallocations.
ray.push_back(3);
ray.push_back(431);
ray.push_back(4);
ray.push_back(123);
ray.push_back(4);
ray.push_back(52);
ray.push_back(52);

Another option is to make an array with the initializer and use some kind of loop to push back elements (if you don't like writing all these push_backs). Another option is to wait for a later version of C++, which should allow this kind of thing.

0

This is a quick and easy way that uses an intermediate step of initializing an array, then using the array to initialize the vector.

int array[20] = {3,431,4,123,4,52,52};
std::vector<int> v ( array, array + 7 );

Alternatively, Boost has an assignment library that does just what you want:

using namespace boost::assign;

std::vector<int> v; 
v += 3,431,4,123,4,52,52;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.