Re: Data structure for sorted list

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




If add and remove operations are supported to your sorted list then
you need to use map or set.
If the keys are not unique then you need to use multimap or multiset.

Following is an excerpt from MSDN describing what is best for which
conditions

<MSDN Excerpt>
The map should be the associative container of choice when the
conditions associating the values with their keys are satisfied by the
application. A model for this type of structure is an ordered list of
uniquely occurring key words with associated string values providing,
say, definitions. If, instead, the words had more than one correct
definition, so that keys were not unique, then a multimap would be the
container of choice. If, on the other hand, just the list of words
were being stored, then a set would be the correct container. If
multiple occurrences of the words were allowed, then a multiset would
be the appropriate container structure.
</MSDN Excerpt>

If the data never changes after you built the sorted list and only
find operation is what you intend to use then I second Doug's comment.
Using std::vector with lower_bound on sorted vector performs well, and
uses less memory compared to map variants.




On Apr 26, 3:18 am, "Doug Harrison [MVP]" <d...@xxxxxxxx> wrote:
On Fri, 25 Apr 2008 11:21:33 -0700, fractal <frac...@xxxxxxxxxxx> wrote:
fractal wrote:

I'm looking for ideas on how to create a sorted list that executes in
the shortest possible time.

Facts about the app:
* Presently, it builds a vector from external data, then quicksorts it
* Each data node has several (potentially long) strings to sort on,
which makes makes comparisons quite slow
* The number of nodes can vary from several dozen to tens of thousands.
* It's compiled with VC6, though if needed, an upgrade to 2008 would be
considered

Would a binary tree be the way to go? stl::map? Would another
structure execute faster than a simple quicksort?

Any thoughts would be appreciated!

I should probably mention that the existing list is vector of pointers
and is discarded after one use.

If I understand you correctly, you fill the data structure with all the
data it will ever hold before using it, and you never update the data
structure after you begin using it. Using std::vector and one std::sort
call sounds ideal to me, though if you only look up a handful of items, it
might be worthwhile to investigate partial_sort. (If you sort it fully, use
lower_bound to find items and binary_search just to test if they're
present.)

--
Doug Harrison
Visual C++ MVP

.



Relevant Pages

  • Re: Clojure hash table accessors in CL
    ... for their hash map. ... So the predicate loops over container values. ... For looping over keys ...
    (comp.lang.lisp)
  • Re: Why doesnt CMap have a copy constructor?
    ... The CMap documentation implies that this operator *cannot* be used on the right side of an expression; what they mean is that it *should not* be used on the right side unless you know that the key is in the map. ... what I had in my mind was a mental comparison with .NET BCL Dictionary container class, the fact that e.g. std::map does not have a ContainsKeymethod; I know, we can use std::map::findand compare it with map::end, but it is not as easy and convenient as having an out of the box method like ContainsKey. ... Or std::map does not offer a method to get a list of all the contained keys, and I also don't like the 'first'/'second' naming choice, I would prefer a more natural 'key'/'value'. ... there isn't anything to stop you from iterating a map and retrieving all the keys. ...
    (microsoft.public.vc.mfc)
  • Re: How to compare hashes to find matching keys with conflicting values.
    ... I did read the perldoc info on map but I am> still confused on how it is being used here. ... I would have now way to link them back to the keys> from register as now they would be numerically ordered in the array instead> of being linked to the hostname key. ... The keys of %keys are sorted with it before it goes through the "map pipe". ...
    (perl.beginners)
  • Re: Frage zu Generics
    ... verletzen, denn dann würde jemand, der aus derselben Liste mit get ... some implementations prohibit null keys and ... als ein Objekt nach dem abholen aus dem Container ...
    (de.comp.lang.java)
  • RE: How to compare hashes to find matching keys with conflicting values.
    ... How to compare hashes to find matching keys with conflicting ... I see that we are creating an array called res however I ... I did read the perldoc info on map but I ... from register as now they would be numerically ordered in the array ...
    (perl.beginners)