Initializing a vector

M

Mats Weber

I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?
 
P

Pete C.

Mats said:
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?

const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete
 
P

Pete C.

Pete said:
Mats said:
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?

const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete

Oops, forgot a closing paren:
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin());

- Pete
 
P

Pete Becker

Pete C. said:
Mats said:
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?

const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete

Oops, forgot a closing paren:
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin());

This drops the last element of the array. It also hard-codes the type of
the array. Here's an improvement:

std::copy(a, a + sizeof(a)/sizeof(*a), v.begin());

Even better, use vector's default constructor, so you can change the
number of elements in the initializer without having to change the
constructor call:

vector<double> v;
std::copy(a, a + sizeof(a)/sizeof(*a), inserter(v, v.end());

Better still, use the vector constructor that handles ranges:

vector v(a, a + sizeof(a)/sizeof(*a));
 
G

Gianni Mariani

Mats said:
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?

Good answers from Pete :)

Another treatment from Leor:

InitUtil:
An STL Container Initialization Library
by Leor Zolman

http://www.bdsoft.com/tools/initutil.html
 
M

Mats Weber

Pete Becker said:
"Pete C." wrote:
Better still, use the vector constructor that handles ranges:

vector v(a, a + sizeof(a)/sizeof(*a));

Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?
 
P

Pete Becker

Mats said:
Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?

I've seen attempts at doing it. They're more trouble than they're worth.
sizeof does the job.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Mats said:
Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?

You can use this template:

template <class C, size_t N>
size_t dim_array (C (&) [N])
{ return N; }

vector v (a, a + dim_array (a) );
 
A

Alex Vinokur

Mats Weber said:
Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?

Why is sizeof not suited to solve your problem?
 
M

Mats Weber

Alex Vinokur said:
Why is sizeof not suited to solve your problem?

Because sizeof deals with the machine representation of the array, which
I do not care about. All I need to know is the number of elements and
their values, not how they are represented in memory. Suppose we are
dealing with arrays of short, and some implementation decides to align
arrays of short on 32-bit boundaries (i.e. addresses that are multiples
of 4). In that case, given

short a[] = {1, 2, 4};

sizeof(short) = 2, sizeof(a) = 12 and the sizeof trick fails.
 
K

Karl Heinz Buchegger

Mats said:
Because sizeof deals with the machine representation of the array, which
I do not care about. All I need to know is the number of elements and
their values, not how they are represented in memory. Suppose we are
dealing with arrays of short, and some implementation decides to align
arrays of short on 32-bit boundaries (i.e. addresses that are multiples
of 4).

That's not possible.
Padding bytes are not allowed in an array. All array elements have to be
contiguous, withoug padding bytes.

But of course it is allowed for the compiler to add padding bytes to
the data type you make an array from.

struct MyType
{
short i;
};

The compiler is allowed to add padding bytes to that struct to bring
it to a size of 4 (with your numbers from above). In this case
sizeof will correctly return 4 and the 'sizeof' trick for getting
the number of elements in an array will continue to work.
 
J

John Harrison

Mats Weber said:
Alex Vinokur said:
Why is sizeof not suited to solve your problem?

Because sizeof deals with the machine representation of the array, which
I do not care about. All I need to know is the number of elements and
their values, not how they are represented in memory. Suppose we are
dealing with arrays of short, and some implementation decides to align
arrays of short on 32-bit boundaries (i.e. addresses that are multiples
of 4). In that case, given

short a[] = {1, 2, 4};

sizeof(short) = 2, sizeof(a) = 12 and the sizeof trick fails.

Such an implementation would be non-conforming. sizeof must include any
bytes needed for alignment.

The sizeof trick always works, the C++ standard requires it to.

But if you don't like it use Julian's template.

john
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top