473,396 Members | 1,689 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,396 software developers and data experts.

vector of structs

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
7 16104
are sure that's ok,i found no add() in the monber function of vector of
stl

Dec 11 '05 #2
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

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

Similar topics

2
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
6
by: Simon Elliott | last post by:
I have ome new code which has to work with some legacy code which does a lot of memset's and memcmp's on structs of PODs. This leads me to want to do stuff like this: struct foo { unsigned...
5
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is...
8
by: Joe Van Dyk | last post by:
I have a vector of structs. Say I get a new struct that's supposed to replace one of the structs in the vector. I believe I could use replace_if() to do this, but I'm not sure what the syntax...
8
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
1
by: sam961 | last post by:
Hi, I am a beginner in C++ so I hope you can be patient on me since my purpose is to learn. I am now involved in a relatively complicated Data Analysis exercise. I have a series of frames...
1
by: themadme | last post by:
im running code thats using threads, everytime i get this debug assertion failed box poping up. It involves with my stl vector. its saying that vector iterators incompatible. the vector...
4
by: engggirl3000 | last post by:
I was told to write a function that will read data, in a user defined text file, into a vector of structs. I am not sure where to start with this. Could some one tell me what is a vector of...
2
by: creativeinspiration | last post by:
Hi Everybody, I have a question. I have an STL vector of structs such as: struct sname { bool member1; bool member2; . . .
11
by: Matthew Page | last post by:
I need to pass an STL Vector to a Visual Basic program using COM. This has to be a vector of a user defined structure and it has to also be within a structure itself. As near as I can tell, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.