473,908 Members | 5,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector

hi all,

i don't understand what's wrong:
1)
std::vector<dou ble[3]> p(10);

doesn't compile:

/usr/include/c++/3.2.2/bits/stl_vector.h: In constructor
`std::vector<_T p, _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:34 2: ISO C++ forbids casting
to an array type `double[3]'

2)
std::vector<dou ble[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:70 3: ISO C++ forbids casting
to an array type `double[3]'

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

it's ok.

i'm using g++ 3.2.2
Jul 19 '05 #1
4 12274


enzo wrote:

hi all,

i don't understand what's wrong:

1)
std::vector<dou ble[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.goo gle.com...
hi all,

i don't understand what's wrong:
1)
std::vector<dou ble[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.newshost ing.com>...
"enzo" <pr************ *@yahoo.com> wrote in message news:6c******** *************** ***@posting.goo gle.com...
hi all,

i don't understand what's wrong:
1)
std::vector<dou ble[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<dou ble[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<dou ble[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<dou ble[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::re size() and the vector constructor
versions in your original post don't compile is that those try to
initialize the elements. std::vector::re serve() doesn't initialize
elements, it is just a hint so the vector can manage memory more
effectively. In other words std::vector::re serve() is not an alternative
for std::vector::re size().

--
Peter van Merkerk
peter.van.merke rk(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
6032
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
2898
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 contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
20
17888
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
17
3375
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 this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
8
5129
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
32
69735
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: std::vector<int> fun(){ //build the vector v; return v; }
56
5863
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
8902
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; Any ideas why is tihs happening? The code is as follows: template <class T> struct StdVectorStorage { std::vector<T>* _storage;
6
3273
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 <vector> #include <algorithm> template <typename T> struct delete_ptr {
13
2972
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 {
0
10031
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9875
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10913
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7246
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5930
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4770
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.