Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL Vector question?

Reply
Thread Tools

STL Vector question?

 
 
Rui Maciel
Guest
Posts: n/a
 
      12-18-2012
Ian Collins wrote:

> The evidence has led you the the correct conclusion! The [] operator on
> std::vector does not allocate storage.


Is there any assurance made by the standard? From what I've gathered, the
C++ standard only specifies that std::vector must provide a couple of
operator[] instances which returns references to members, and that storage
management is handled automatically by the class.


Rui Maciel
 
Reply With Quote
 
 
 
 
none
Guest
Posts: n/a
 
      12-20-2012
In article <(E-Mail Removed)>,
jski <(E-Mail Removed)> wrote:
>Try:
>
> struct data
> {
> int dof;
> int XYZ;
> double X, Y, Z;
> int node;
> vector<point *> pt;
>
> };
>
>AeroDOF[ dof[j] ].pt.push_back( new point( alpha[i], beta[i], Force[i]
>[j] ) );


Apart from creating a memory leak, this does nothing useful. Stick to
using values in the vector unless you have very good reasons not to.

Yannick

 
Reply With Quote
 
 
 
 
Calum
Guest
Posts: n/a
 
      12-20-2012
On Monday, 17 December 2012 19:55:59 UTC, Elzbieta Burnett wrote:
> I thought that vector '[]' would create an instance of 'point' (like
>
> map '[]'), call copy constructor and assign newly created entry to
>
> point( alpha[i], beta[i], Force[i][j] ) in AeroDOF.pt.
>
>
>
> Evidently this isn't true?


Exactly. std::vector:perator[] never creates new items. However std::map:perator[] behaves differently and does create new items.

You can use std::vector::at() which performs a bounds check on the vector and will throw an exception if the index is out of range. You must of course catch the exception.

You could also replace std::vector<point> with std::map<int,point>, so that points are allocated according to your expectations. However this is much less efficient.

Best to use std::vector:ush_back(), resize() or pass the desired size of the vector to the constructor.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem storing tvmet vector objects in an stl vector container alexjcollins@gmail.com C++ 2 09-08-2008 09:18 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector of STL maps versus Vector of objects amolpan@gmail.com C++ 2 07-26-2005 10:16 PM
Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*> CD C++ 2 10-05-2004 02:29 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments