std::vector

E

enzo

hi all,

i don't understand what's wrong:


1)
std::vector<double[3]> p(10);

doesn't compile:

/usr/include/c++/3.2.2/bits/stl_vector.h: In constructor
`std::vector<_Tp, _Alloc>::vector(unsigned int) [with _Tp = double[3],
_Alloc = std::allocator<double[3]>]':
main.cpp:9: instantiated from here
/usr/include/c++/3.2.2/bits/stl_vector.h:342: ISO C++ forbids casting
to an array type `double[3]'

2)
std::vector<double[3] p;
p.resize(10);

doesn't compile:

/usr/include/c++/3.2.2/bits/stl_vector.h: In member function `void
std::vector<_Tp, _Alloc>::resize(unsigned int) [with _Tp = double[3],
_Alloc = std::allocator<double[3]>]':
main.cpp:10: instantiated from here
/usr/include/c++/3.2.2/bits/stl_vector.h:703: ISO C++ forbids casting
to an array type `double[3]'

3)
std::vector<double[3]> p;
p.reserve(10);

it's ok.

i'm using g++ 3.2.2
 
K

Karl Heinz Buchegger

enzo said:
hi all,

i don't understand what's wrong:

1)
std::vector<double[3]> p(10);

You can't put arrays into vectors. Arrays don't fullfil the properties
needed for std::vector: They are not copyable and not assignable.

Why not:

struct Point
{
double x;
double y;
double z;
};

std::vector< Point > p;
 
R

Ron Natalie

enzo said:
hi all,

i don't understand what's wrong:


1)
std::vector<double[3]> p(10);
The type used in a vector (or other standard container) must be copy constructable
and assignable. Plain arrays do not meet this requirement (you can't assign them).
Your choice is to wrap a class around the vector:"

struct tripple { double d[3]; }
vector<triple> p(10);
 
E

enzo

Ron Natalie said:
enzo said:
hi all,

i don't understand what's wrong:


1)
std::vector<double[3]> p(10);
The type used in a vector (or other standard container) must be copy constructable
and assignable.

ok
so why this works? but mostly how can work?

3)
std::vector<double[3]> p;
p.reserve(10);
p[1][2] = 3.0;
std::cout << p[1][2] << std::endl;


Plain arrays do not meet this requirement (you can't assign them).
Your choice is to wrap a class around the vector:"

struct tripple { double d[3]; }
vector<triple> p(10);
 
P

Peter van Merkerk

i don't understand what's wrong:
1)
std::vector<double[3]> p(10);
The type used in a vector (or other standard container) must be copy constructable
and assignable.

ok
so why this works? but mostly how can work?

3)
std::vector<double[3]> p;
p.reserve(10);
p[1][2] = 3.0;
std::cout << p[1][2] << std::endl;

It may work with the implementation of the standard library you are
using, but it is not guaranteed to work with other standard library
implementations. The reason why it works in your case is that reserve
immediately allocates the requested space and you have a POD type which
does not have to be initialized before use.

The reason that std::vector::resize() and the vector constructor
versions in your original post don't compile is that those try to
initialize the elements. std::vector::reserve() doesn't initialize
elements, it is just a hint so the vector can manage memory more
effectively. In other words std::vector::reserve() is not an alternative
for std::vector::resize().
 

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,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top