473,695 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15124
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::inser t() 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::inser t() 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::ostr eam &s) {
return s << m_ID << '\t' << m_name << '\n';
}
};

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

typedef std::list<mydat a> 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::iter ator 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::co nst_iterator CIN=namelist.be gin();
for ( ; CIN!=namelist.e nd(); ++CIN)
CIN->second->print(cout);
cout << endl;

cout << "Data sorted by ID:" << endl;
map_by_ID::cons t_iterator CIID=IDlist.beg in();
for ( ; CIID!=IDlist.en d(); ++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::inser t() 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

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

Similar topics

0
1577
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 quick lookup: it's a python module so probably it's slower. Furthermore you can __hash__ almost everything, but you can __cmp__ in a consistent way much less stuff. Take definitely a look at sets.
5
52112
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 direct way to do so? Or must I list = for item in set1:
4
2515
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 property of the 'ListMemberObject' which is instrumental in the sort to be modified unbeknownst to the list thereby defeating the sort. One way to keep the list sorted is to add a 'ParentList' property to the ListMemberObject and notify that...
4
5149
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 items). - indexed by value index or by key (2 indexers) - sortable by key or a secondary key - supporting foreach loops
4
3925
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 list with a binary search to add the file name at the correct location, this way it is very fast. I but I created my own string list. So here is my question: Does C# or the .NET framework have such a sorted list that I can add the items during...
1
1636
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 doorConflictList be keyed on a door ID and contain a sorted list called conFlictList. I wrote this expecting the following doorConflictList should end up with 3 items. Item 1 would be a sorted list with 1 item
4
1850
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 there is SortedList<key, value> for hash tables, but could find anything for a sorted list. Currently I am using List<string> and whenever I add an item, I need to
1
1587
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 starting from a choosen item via <back> and <next>. Say this is the list: <a>
9
2208
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 sorted list. Sorting out the list is not the problem for me but allocating the points is. The other problem is that when for example two records have the same weight, the allocated points should be the same, even when in the sorted list one of the...
0
8565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9113
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8977
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8841
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7658
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2269
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.