473,396 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STL and sorted list

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
Jul 22 '05 #1
10 15083
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.


Jul 22 '05 #2
In article <c9*************@news.t-online.com>,
"Der Andere" <ma**************@gmx.net> 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.
Jul 22 '05 #3
> >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 returnsthe 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.

Jul 22 '05 #4
> > 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.

Jul 22 '05 #5
In article <c9*************@news.t-online.com>,
"Der Andere" <ma**************@gmx.net> wrote:
>I need to implement a sorted (ordered) list. STL has no sorted list typeas >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.
Jul 22 '05 #6
> >> >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
Jul 22 '05 #7
> 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
Jul 22 '05 #8
"Der Andere" <ma**************@gmx.net> wrote in message news:<c9************@news.t-online.com>...
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::ostream& print(std::ostream &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
Jul 22 '05 #9
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
kb******@gascad.at
Jul 22 '05 #10
Dave Moore ha escrito:
[...]
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:


[...]

In case you're curious, the kind of constructs you describe can be
obtained in a very convenient manner with Boost.MultiIndex, which will
ship in the next release of Boost:

http://www.boost-consulting.com/boost/libs/multi_index/

[yes, this is self-publicizing :) ]

Regards,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniele Varrazzo | last post by:
If you need a container to look into, there is the sets module that provides a couple of them. If you need a sorted list, there is the bisect module. But i don't think it fits your need for a...
5
by: MackS | last post by:
Dear all, I've got several large sets in my program. After performing several operations on these I wish to present one set to the user sorted according to a certain criterion. Is there any...
4
by: Robert Zurer | last post by:
Assuming that I have created a strongly typed collection and overridden the appropriate methods, i.e. this, Add, Insert etc., so that a sort order is maintained, it's still very possible for a...
4
by: Cybertof | last post by:
Hi ! I would like to make an array of structure (collection ?) that would behave like an advanced sorted list : a sorted list with one key but with multiple values (sorted lists are key/value...
4
by: Olaf Baeyens | last post by:
I have to load about 10000 possible file names into a list and must have thems sorted alphabetically, including the file information. At this moment, in conventional C++, I use a sorted file strng...
1
by: J L | last post by:
I want to create a sorted list whose values are themselves sorted lists. I wrote the following simple test program but it does not behave as I would expect. What I wanted to do was have the...
4
by: shrishjain | last post by:
Hi All, I need a type where I can store my items in sorted order. And I want to keep adding items to it, and want it to remain sorted. Is there any type in .net which I can make use of. I see...
1
by: Khayrat | last post by:
Hi folks, please help I have a xml file with a list of items. The list is sorted during xslt processing. Based on this sorted list I want a navigation facility to walk through the sorted list...
9
by: ECUweb | last post by:
Hi, I need to sort out a list of records based on the field "Weight" and then allocate points (from 1 to 10) to each record (in another field "Points") depending on the position of the record in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.