question about std::set

A

Anonymous

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
 
V

Victor Bazarov

Anonymous said:
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
 
J

Jim Langston

Anonymous said:
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.
 
T

terminator

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.
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top