std::vector construction

A

Alf P. Steinbach

* mlt:
Is there someway to create a std::vector from a sequence of numbers? On this
page:

http://www.cppreference.com/wiki/stl/vector/vector_constructors

there are 4 different constructors, but none of them supports something
like:

std::vector<int> v = {1,2,3};

Its only possible to do it like:

std::vector<int> v;

v.push_back(1,2,3);

The usual is to declare the static data as static data, e.g.

static int data[] = {1, 2, 3};
static size_t const n = sizeof( data )/sizeof( *data );

std:vector<int> v( data, data + n );

Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* mlt:
Is there someway to create a std::vector from a sequence of numbers?
On this page:

http://www.cppreference.com/wiki/stl/vector/vector_constructors

there are 4 different constructors, but none of them supports
something like:

std::vector<int> v = {1,2,3};

Its only possible to do it like:

std::vector<int> v;

v.push_back(1,2,3);

The usual is to declare the static data as static data, e.g.

static int data[] = {1, 2, 3};
static size_t const n = sizeof( data )/sizeof( *data );

std:vector<int> v( data, data + n );

Add a 'const' in the 'data' declaration.


Cheers & sorry for typo,

- Alf, on Friday 13th, no coffee
 
Z

ZikO

mlt said:
Is there someway to create a std::vector from a sequence of numbers? On this
page:

http://www.cppreference.com/wiki/stl/vector/vector_constructors

there are 4 different constructors, but none of them supports something
like:

std::vector<int> v = {1,2,3};

Its only possible to do it like:

std::vector<int> v;

v.push_back(1,2,3);
AFAIK you can't do that by constructor. You could however use generate()
or generate_n() algorythm in <algorithm> head file and fill empty vector
with values generated by Generator. You however, need, generator :p


Something like this

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

class Gen { // generator
private:
int value;
int step;
public:
Gen(int bb = 0, int ss = 1) : value(bb), step(ss) {}
int operator()() {
int old = value;
value += step;
return old;
}
};

int main() {
vector<int> v1;
generate_n(back_inserter(v1), 50, Gen(1,1));

vector<int> v2(50);
generate(v2.begin(), v2.end(), Gen(0,5));

for(int i=0; i<v1.size(); i++) {
cout << v1 << " ";
}
cout << endl;

for(int i=0; i<v2.size(); i++) {
cout << v2 << " ";
}
cout << endl;
}

Hopefuly it helped.
 
J

James Kanze

Is there someway to create a std::vector from a sequence of
numbers? On this page:

there are 4 different constructors, but none of them supports
something like:
std::vector<int> v = {1,2,3};
Its only possible to do it like:
std::vector<int> v;
v.push_back(1,2,3);

There are several solutions. As Alf has pointed out, you can
always copy a staticly initialized C-style array (although it's
more idiomatic---and less error prone---to use template
functions to get the start and end addresses). For specific
instances where the sequence can be calculated, however, I'd
look into Boosts iterator adapters (to be used with the same
constructor---the template one with two iterators).
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top