hi,

how can i do it:

int array[20]={3,431,4,123,4,52,52};

using vectors??

Recommended Answers

All 2 Replies

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.

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.