473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

std::vector

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
4 12205


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

"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
"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
> > > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.