Hey,new to stl, in c array, we can declare an array like

int a[]={2,4,5,6,7,7}

how can u do it for a vector without pushing back n times???

Recommended Answers

All 3 Replies

Make an algorithm.

You can do something like this:

int myints[] = {16,2,77,29};
  vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

Hey,new to stl, in c array, we can declare an array like

int a[]={2,4,5,6,7,7}

how can u do it for a vector without pushing back n times???

C++0x has extended initializer lists, but current standard C++ doesn't allow it for the vector class without tricks like siddhant3s'. Boost::Assign can make it shorter than a bunch of push_back calls and save you a dummy variable at the cost of Boost. ;)

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.