Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL and sorted list

Reply
Thread Tools

STL and sorted list

 
 
Der Andere
Guest
Posts: n/a
 
      06-01-2004
I need to implement a sorted (ordered) list. STL has no sorted list type as
far as I know. Is there a (straight) way to implement a sorted list using
STL?
BTW: The type of items in the list will be a class. Is it necessary to
implement the > or < operators or to write a compare-function that returns
the larger or smaller of two classes? Ideally, the list begins with the
smallest element.

Cheers,
Matthias


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-01-2004
Der Andere wrote:

> I need to implement a sorted (ordered) list. STL has no sorted list
> type as far as I know.


It has std::set.

> Is there a (straight) way to implement a sorted list using STL?
> BTW: The type of items in the list will be a class. Is it necessary to
> implement the > or < operators or to write a compare-function that
> returns the larger or smaller of two classes?


Only operator<, AFAIK.

> Ideally, the list begins with the smallest element.


 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      06-01-2004
In article <c9hnnu$jeq$07$>,
"Der Andere" <> wrote:

>I need to implement a sorted (ordered) list. STL has no sorted list type as
>far as I know. Is there a (straight) way to implement a sorted list using
>STL?


Sure.

myList.sort();


>BTW: The type of items in the list will be a class. Is it necessary to
>implement the > or < operators or to write a compare-function that returns
>the larger or smaller of two classes?


operator< is generally implemented for sort, but any binary functor that
can compare two objects and return true for the smaller (ie left most)
one will work.
 
Reply With Quote
 
Der Andere
Guest
Posts: n/a
 
      06-01-2004
> >I need to implement a sorted (ordered) list. STL has no sorted list type
as
> >far as I know. Is there a (straight) way to implement a sorted list using
> >STL?

>
> Sure.
>
> myList.sort();


As the list would have to be sorted after every (non-sorting) insertion,
probably the multiset is the better alternative.

> >BTW: The type of items in the list will be a class. Is it necessary to
> >implement the > or < operators or to write a compare-function that

returns
> >the larger or smaller of two classes?

>
> operator< is generally implemented for sort, but any binary functor that
> can compare two objects and return true for the smaller (ie left most)
> one will work.



 
Reply With Quote
 
Der Andere
Guest
Posts: n/a
 
      06-01-2004
> > I need to implement a sorted (ordered) list. STL has no sorted list
> > type as far as I know.

>
> It has std::set.


You're right.
The problem that I had was that I also need to contain multiple occurences
of one element. But a different version of set (multiset) seems to be the
appropriate solution.


> > Is there a (straight) way to implement a sorted list using STL?
> > BTW: The type of items in the list will be a class. Is it necessary to
> > implement the > or < operators or to write a compare-function that
> > returns the larger or smaller of two classes?

>
> Only operator<, AFAIK.
>
> > Ideally, the list begins with the smallest element.

>



 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      06-02-2004
In article <c9ivhg$lb4$02$>,
"Der Andere" <> wrote:

>> >I need to implement a sorted (ordered) list. STL has no sorted list type

>as
>> >far as I know. Is there a (straight) way to implement a sorted list using
>> >STL?

>>
>> Sure.
>>
>> myList.sort();

>
>As the list would have to be sorted after every (non-sorting) insertion,
>probably the multiset is the better alternative.


True, however if it is a situation where the items are all added at
once, then not modified while the list is being iterated over, simply
calling list::sort() may be a more effecient alternative.
 
Reply With Quote
 
Der Andere
Guest
Posts: n/a
 
      06-02-2004
> >> >I need to implement a sorted (ordered) list. STL has no sorted list
type
> >as
> >> >far as I know. Is there a (straight) way to implement a sorted list

using
> >> >STL?
> >>
> >> Sure.
> >>
> >> myList.sort();

> >
> >As the list would have to be sorted after every (non-sorting) insertion,
> >probably the multiset is the better alternative.

>
> True, however if it is a situation where the items are all added at
> once, then not modified while the list is being iterated over, simply
> calling list::sort() may be a more effecient alternative.


Oh I see. However, items are added and deleted throughout the execution of
the program, so list::sort() would have to called many times.

Regards,
Matthias


 
Reply With Quote
 
Der Andere
Guest
Posts: n/a
 
      06-02-2004
> I need to implement a sorted (ordered) list. STL has no sorted list type
as
> far as I know. Is there a (straight) way to implement a sorted list using
> STL?
> BTW: The type of items in the list will be a class. Is it necessary to
> implement the > or < operators or to write a compare-function that returns
> the larger or smaller of two classes? Ideally, the list begins with the
> smallest element.


Sorry, I was wrong there: The items in the list will be *pointers* to
instances of a class. If I use multiset::insert() then the items will be
ordered according to their (address) value, not according to the value of
the objects they point to. This is exactly what I do _not_ want ...

Regards,
Matthias


 
Reply With Quote
 
Dave Moore
Guest
Posts: n/a
 
      06-02-2004
"Der Andere" <> wrote in message news:<c9k75u$fh$04$>...
> > I need to implement a sorted (ordered) list. STL has no sorted list type

> as
> > far as I know. Is there a (straight) way to implement a sorted list using
> > STL?
> > BTW: The type of items in the list will be a class. Is it necessary to
> > implement the > or < operators or to write a compare-function that returns
> > the larger or smaller of two classes? Ideally, the list begins with the
> > smallest element.

>
> Sorry, I was wrong there: The items in the list will be *pointers* to
> instances of a class. If I use multiset::insert() then the items will be
> ordered according to their (address) value, not according to the value of
> the objects they point to. This is exactly what I do _not_ want ...
>

In that case you will have to use std::multiset's big brother,
std::multimap, where you duplicate the value from you structure that
you want to sort by as the key field.

For example:
#include <string>
#include <map>
#include <list>
#include <iostream>
#include <ostream>

using std::string;
using std::cout;
using std::endl;

struct mydata {
int m_ID;
string m_name;
mydata(const string& name, int ID) : m_ID(ID), m_name(name) {}
std:stream& print(std:stream &s) {
return s << m_ID << '\t' << m_name << '\n';
}
};

typedef std::multimap< int, mydata*> map_by_ID;
typedef std::multimap<string, mydata*> map_by_name;

typedef std::list<mydata> datastore;

int main () {
datastore data;

data.push_back(mydata("Alice", 4));
data.push_back(mydata("Alice", 671)); // just coz its a multimap
data.push_back(mydata("Bob", 3));
data.push_back(mydata("Chuck", 2));
data.push_back(mydata("Zelda", 2)); // just coz its a multimap
data.push_back(mydata("Dave", 1));

map_by_name namelist;
map_by_ID IDlist;
datastore::iterator di=data.begin();
for ( ; di!=data.end(); ++di) {
IDlist.insert( std::make_pair( di->m_ID, &(*di)));
namelist.insert(std::make_pair(di->m_name, &(*di)));
}

cout << "Data sorted by name:" << endl;
map_by_name::const_iterator CIN=namelist.begin();
for ( ; CIN!=namelist.end(); ++CIN)
CIN->second->print(cout);
cout << endl;

cout << "Data sorted by ID:" << endl;
map_by_ID::const_iterator CIID=IDlist.begin();
for ( ; CIID!=IDlist.end(); ++CIID)
CIID->second->print(cout);
cout << endl;
}

The above example could be cleaned up a bit but I hope you get the
gist. This is often a convenient way to implement a simplistic
database ... store the objects unsorted in some container, and then
create multiple sorted lists of pointers to allow fast searching
according to different criteria. Note that if items will be added to
the database, then you should *not* use std::vector or std::deque as
the unsorted container, because as they grow/shrink, then
pointers/iterators will eventually become invalidated. This is
guaranteed not to happen with std::list.

HTH, Dave Moore
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-02-2004
Der Andere wrote:
>
> > I need to implement a sorted (ordered) list. STL has no sorted list type

> as
> > far as I know. Is there a (straight) way to implement a sorted list using
> > STL?
> > BTW: The type of items in the list will be a class. Is it necessary to
> > implement the > or < operators or to write a compare-function that returns
> > the larger or smaller of two classes? Ideally, the list begins with the
> > smallest element.

>
> Sorry, I was wrong there: The items in the list will be *pointers* to
> instances of a class. If I use multiset::insert() then the items will be
> ordered according to their (address) value, not according to the value of
> the objects they point to. This is exactly what I do _not_ want ...


Not if you provide the multiset with a predicate which tells it in which
way you want the ordering to be done.

--
Karl Heinz Buchegger

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a list depending of the indexes of another sorted list Santiago Romero Python 10 01-21-2008 03:10 PM
Convert (sorted) list of dics to nested list ? shearichard@gmail.com Python 2 03-22-2006 09:34 PM
STL set members sorted? chenboston@gmail.com C++ 1 03-17-2006 05:22 PM
STL :: Set operations on sorted structures Generic Usenet Account C++ 8 12-07-2005 03:26 AM
STL Data Structures, Sorted Insertion? Kushal C++ 2 01-30-2004 04:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57