|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
does the STL Class vector can hold any user define type?
#include <vector> vector< MyCar> cars( 7 ); above OK? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"Jim Johnson" <aopiyy001@yahoo.com> wrote in message > does the STL Class vector can hold any user define type? > > #include <vector> > > vector< MyCar> cars( 7 ); Yes. Did you try using it? Sharad |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Apr 3, 8:34 pm, Jim Johnson <aopiyy...@yahoo.com> wrote:
> 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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Jim Johnson wrote:
> 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 2008-04-03 21:34:54 -0400, Jim Johnson <aopiyy001@yahoo.com> said:
> does the STL Class vector can hold any user define type? > vector is not a class. It is a template. If you don't make that distinction you'll end up thoroughly confused. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
kasthurirangan.balaji@gmail.com wrote:
> On Apr 3, 8:34 pm, Jim Johnson <aopiyy...@yahoo.com> wrote: >> 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. > PLUS the type can not be a local or unnamed type or derived therefrom. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Ron Natalie wrote:
> kasthurirangan.balaji@gmail.com wrote: >> On Apr 3, 8:34 pm, Jim Johnson <aopiyy...@yahoo.com> wrote: >>> 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. >> > 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? |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Rolf Magnus wrote:
> 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? > Yep, but I wanted to head off the next question when he tries do do something like: int foo() { struct x { int a; }; vector<x> v; } Derived, may be not, but the standard uses compounded from rather than derived...and that you can do. |
|
![]() |
| Outils de la discussion | |
|
|