|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|