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 > question about std::set
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
question about std::set

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 17h41   #1
Anonymous
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut question about std::set

I am writing a date manipulation library. I need a container that
requires each element to be unique, and also a container that is
'sortable'. After inserting the various elements ino the class, I would
like top sort them in ascending order.

I am undecided as to whether to use std::vector (which checks to ensure
uniqueness) or to use std::set class - any ideas on pros and cons?

Incidentally, if anyone knows a good online tutorial on std::set, please
let me know - the SGI docs are not too ful (i.e. no real examples),
and google is not coming up with anything too useful either
  Réponse avec citation
Vieux 16/10/2007, 17h52   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question about std::set

Anonymous wrote:
> I am writing a date manipulation library. I need a container that
> requires each element to be unique, and also a container that is
> 'sortable'. After inserting the various elements ino the class, I
> would like top sort them in ascending order.
>
> I am undecided as to whether to use std::vector (which checks to
> ensure uniqueness) or to use std::set class - any ideas on pros and
> cons?


A vector that "checks" for uniqueness will have to keep itself
sorted (to make the checks quick) and thus will do a lot of extra
work to assign and reallocate itself (when new objects are inserted).
That's why 'std::set' with proper sorting (comparison) functor is
the best approach. Yes, it's a bit wasteful because each set node
has to keep some extra topology information, but it's usually well
worth it when it comes to performance.

Once you finish inserting, you can trasfer all data into a vector
by traversing the set from the beginning to the end. The elements
of the vector will be sorted and unique, and they won't take up
any extraneous storage (no topology is needed beyond the order
inferred by the storage itself).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 16/10/2007, 21h05   #3
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question about std::set


"Anonymous" <no.reply@here.com> wrote in message
news:ifmdnXsSLIXJd4nanZ2dnUVZ8tOmnZ2d@bt.com...
>I am writing a date manipulation library. I need a container that requires
>each element to be unique, and also a container that is 'sortable'. After
>inserting the various elements ino the class, I would like top sort them in
>ascending order.


This pretty much describes a std::set. Uniquie and sorted.

> I am undecided as to whether to use std::vector (which checks to ensure
> uniqueness) or to use std::set class - any ideas on pros and cons?


A std::vector does not guarantee uniqueness, nor is it sorted. You would
have to do those steps manually somehow. std::sort for sorting is easy, but
you are going to have to check to make sure the item is not in the vector
before you add it, and a vector does not have .find() so you'll either need
to iterate over every element each time, or sort the vector each time an
element is inserted. Since std::set does all this for you, why not use it?

> Incidentally, if anyone knows a good online tutorial on std::set, please
> let me know - the SGI docs are not too ful (i.e. no real examples),
> and google is not coming up with anything too useful either


A std::set is fairly simple, similar to a std::vector. Except instead of
doing a push_back() you do an .insert(). The iterator is just about the
same. Also, you will want an operator< for whatever is in the set for
sorting.


  Réponse avec citation
Vieux 17/10/2007, 12h43   #4
terminator
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question about std::set

On Oct 16, 7:41 pm, Anonymous <no.re...@here.com> wrote:
> I am writing a date manipulation library. I need a container that
> requires each element to be unique, and also a container that is
> 'sortable'. After inserting the various elements ino the class, I would
> like top sort them in ascending order.
>
> I am undecided as to whether to use std::vector (which checks to ensure
> uniqueness) or to use std::set class - any ideas on pros and cons?
>
> Incidentally, if anyone knows a good online tutorial on std::set, please
> let me know - the SGI docs are not too ful (i.e. no real examples),
> and google is not coming up with anything too useful either


if you just mean to store and sort and ** do not want to modify the
values ** std::set is best.

regards,
FM.

  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 23h10.


É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,09039 seconds with 12 queries