how to initialize std::vector?

T

Tony Young

Hi,

I wonder if there is a way to declare and initialize a vector in one
line as one does for array (shown below):

int n[4] = {1, 2, 3, 4};

The one line should be equivalent to the following code:
vector<int> n;
n.resize(4);
for (int i=0; i<4; i++)
n = i+1;

Any help is appreciated.

JD
 
V

Victor Bazarov

Tony said:
I wonder if there is a way to declare and initialize a vector in one
line as one does for array (shown below):

int n[4] = {1, 2, 3, 4};

The one line should be equivalent to the following code:
vector<int> n;
n.resize(4);
for (int i=0; i<4; i++)
n = i+1;


I think I heard/read about 'counting_iterator' from Boost. Check
it out.

V
 
W

WittyGuy

I wonder if there is a way to declare and initialize a vector in one
line as one does for array (shown below):

int n[4] = {1, 2, 3, 4};

The one line should be equivalent to the following code:
vector<int> n;
n.resize(4);
for (int i=0; i<4; i++)
n = i+1;


I don't know the one line equivalent, but the following might be
helpful to you:
int n[4] = {1, 2, 3, 4};
vector <int> vecN (n, n+4);

-
Sukumar R
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top