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

vector of structs

P: n/a
Hi everybody,

the "insert" function of a vector copies the param? or just refers to
the structure?

for example:

typedef struct {
int a;
...
} structType;

void f ( vector <structType> & v){
stryctType xx;
xx.a = 10;
v.add (xx); // it really *copies* xx in v... isn't it?
}

int main(){

f (vect);
cout << vect[0].a;
}
it works, but... is it correct?

thanks,
Major

Dec 11 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
are sure that's ok,i found no add() in the monber function of vector of
stl

Dec 11 '05 #2

P: n/a
oc****@despammed.com wrote:
Hi everybody,

the "insert" function of a vector copies the param? or just refers to
the structure?


Copy of structure will be inserted.
I'd recommend you to read this great book by Nicolai M. Josuttis:
http://www.josuttis.com/libbook/index.html

Recommended chapter with explanation is 6.1 "Common Container Abilities
and Operations".

Shortly, ALL STL containers are based on semantic of value not
reference, so they operate on copies internally.

--
Mateusz Loskot
http://mateusz.loskot.net

Dec 11 '05 #3

P: n/a

<oc****@despammed.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi everybody,

the "insert" function of a vector copies the param? or just refers to
the structure?

for example:

typedef struct {
int a;
...
} structType;

void f ( vector <structType> & v){
stryctType xx;
xx.a = 10;
v.add (xx); // it really *copies* xx in v... isn't it?
}

int main(){

f (vect);
cout << vect[0].a;
}
it works, but... is it correct?


Sorry, but the program above neither works/compiles nor is it correct. Among
other things there is no member function add for the class vector, so I
guess you meant something like insert or push_back. These functions do
create a copy which is placed in the container and thus, there is the
requirement that those objects stored in a standard container must be copy
constructible.

HTH
Chris
Dec 11 '05 #4

P: n/a
Mateusz Loskot wrote:
Copy of structure will be inserted. [cut] Shortly, ALL STL containers are based on semantic of value not
reference, so they operate on copies internally.


cheers!
I have "Borland C++: The Complete Reference" by Schildt, but it isn't
really clear on this point...

Dec 11 '05 #5

P: n/a
Chris Theis wrote:
Sorry, but the program above neither works/compiles nor is it correct. Among
other things there is no member function add for the class vector, so I
guess you meant something like insert or push_back. These functions do
create a copy which is placed in the container and thus, there is the
requirement that those objects stored in a standard container must be copy
constructible.


sorry, my fault (yes, I meant something like insert or push_back)...
when I wrote I was far from my compiler :-(

thanks a lot,

Major

Dec 11 '05 #6

P: n/a
us******@gmail.com wrote:
are sure that's ok,i found no add() in the monber function of vector of
stl


my mistake, sorry!

Dec 11 '05 #7

P: n/a
> cheers!
I have "Borland C++: The Complete Reference" by Schildt, but it isn't
really clear on this point...


Schildt seems to be an unfortunate choice of author, although that
particular title isn't reviewed

http://www.accu.org/bookreviews/publ...iews/0au/s.htm

Get yourself a copy of Accelerated C++

http://www.acceleratedcpp.com/

Gavin Deane

Dec 11 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.