By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,329 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,329 IT Pros & Developers. It's quick & easy.

std::vector

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


enzo wrote:

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;

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

P: n/a

"enzo" <pr*************@yahoo.com> wrote in message news:6c**************************@posting.google.c om...
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);
Jul 19 '05 #3

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"enzo" <pr*************@yahoo.com> wrote in message news:6c**************************@posting.google.c om...
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);

Jul 19 '05 #4

P: n/a
> > > 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().

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.