PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > <vector>
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
<vector>

Réponse
 
LinkBack Outils de la discussion
Vieux 04/04/2008, 02h34   #1
Jim Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut <vector>

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

#include <vector>

vector< MyCar> cars( 7 );

above OK?
  Réponse avec citation
Vieux 04/04/2008, 03h03   #2
Sharad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: <vector>


"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


  Réponse avec citation
Vieux 04/04/2008, 03h04   #3
kasthurirangan.balaji@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: <vector>

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.
  Réponse avec citation
Vieux 04/04/2008, 04h49   #4
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: <vector>

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
  Réponse avec citation
Vieux 04/04/2008, 12h11   #5
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: <vector>

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)

  Réponse avec citation
Vieux 04/04/2008, 13h43   #6
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: <vector>

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.
  Réponse avec citation
Vieux 04/04/2008, 17h23   #7
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: <vector>

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?

  Réponse avec citation
Vieux 04/04/2008, 18h05   #8
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: <vector>

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h49.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14410 seconds with 16 queries