<vector>

J

Jim Johnson

does the STL Class vector can hold any user define type?

#include <vector>

vector< MyCar> cars( 7 );

above OK?
 
S

Sharad

Jim Johnson said:
does the STL Class vector can hold any user define type?

#include <vector>

vector< MyCar> cars( 7 );

Yes. Did you try using it?

Sharad
 
K

kasthurirangan.balaji

does the STL Class vector can hold any user define type?

#include <vector>

vector< MyCar> cars( 7 );

above OK?

yes, ensure the user defined type is copy constructable and
assignable.

Thanks,
Balaji.
 
K

Kai-Uwe Bux

Jim said:
does the STL Class vector can hold any user define type?

No. As all containers, the value types need to satisfy the
copy-constructible and assignable conceptual requirements. Using a vector
on a type that does not is undefined behavior.

Also: some functions may require default constructability.

#include <vector>

vector< MyCar> cars( 7 );

above OK?

Depends on MyCar. The above will require MyCar to be copy-constructible,
assignable, and default-constructible.


Guessing from the name "MyCar", the class could be a class for which
copy-construction and assignment doesn't make sense (since cars are
entities and don't get copied and assigned in the real-world domain where
one deals with them). In that case, you might consider vector< MyCar* > or
some smart-pointer variant thereof. (This also takes care of the possible
need for polymorphic cars in the vector.)


Best

Kai-Uwe Bux
 
R

Rolf Magnus

Ron said:
PLUS the type can not be a local or unnamed type or derived
therefrom.

That's generally true for any type used as template argument. BTW:
Can you have a type that is derived from a local or unnamed type, but isn't
itself one?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top