question about std::set



 DEVELOP > c-Plus-Plus > question about std::set

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Anonymous"
Date: 16 Oct 2007 11:41:38 AM
Object: 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 helpful (i.e. no real examples),
and google is not coming up with anything too useful either
.

User: "Jim Langston"

Title: Re: question about std::set 16 Oct 2007 03:05:34 PM
"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 helpful (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.
.

User: "Victor Bazarov"

Title: Re: question about std::set 16 Oct 2007 11:52:23 AM
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
.

User: "terminator"

Title: Re: question about std::set 17 Oct 2007 06:43:34 AM
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 helpful (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.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER