473,406 Members | 2,312 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,406 software developers and data experts.

Sortable associative container?

Hi,

I have the following problem:
I want to store file objects in a container 8for now, it's not important how
they look like).
Now, on the one hand I need to be able to randomly pick files from the
container as fast as possible. So far I have been using std::map to store
the files, whereas the key was a unique file ID. That given, I could pick
random files from the container in O(logn).
On the other hand, I need functionality to sort the files, e.g. by date or
size. However, std::sort and std::stable_sort only seem to work on
non-associative containers. On top of that, std::map doesn't define an own
sorting operation, like std::list does.

My questions:

1) How come there's no sorting operation for std::map?

2) Which solutions are at hand? Is there such a thing as a sortable map at
all?

Thanks,
Matthias
Jul 22 '05 #1
7 1679
Matthias Käppler wrote:
Hi,

I have the following problem:
I want to store file objects in a container 8for now, it's not important
how they look like).
Now, on the one hand I need to be able to randomly pick files from the
container as fast as possible. So far I have been using std::map to store
the files, whereas the key was a unique file ID. That given, I could pick
random files from the container in O(logn).
On the other hand, I need functionality to sort the files, e.g. by date or
size. However, std::sort and std::stable_sort only seem to work on
non-associative containers. On top of that, std::map doesn't define an own
sorting operation, like std::list does.

My questions:

1) How come there's no sorting operation for std::map?
That's because the main reason why maps are so quick at looking for a
specific key because they store the elements ordered. If you could sort a
map, the lookup wouldn't work anymore.
2) Which solutions are at hand? Is there such a thing as a sortable map at
all?


No. At least not in standard C++.

Jul 22 '05 #2
Matthias Käppler wrote:
...
My questions:

1) How come there's no sorting operation for std::map?
Because associative containers are kept sorted at all times. Sorting is
performed in accordance with comparator supplied at construction time.
You cannot re-sort the container in accordance with any other
comparator. This is essential to proper operation of such containers.
2) Which solutions are at hand? Is there such a thing as a sortable map at
all?


You can take a "sortable" container (say, 'std::vector') and fill it
with, say, pointers to elements of your 'std::map'. Then sort it any way
you want. That would be an off-line solution.

A possible on-line solution would involve keeping the elements in
several associative containers at once, each sorted with its own
specific comparator.

Each solution has its pros and cons.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #3
Matthias Käppler wrote:
Hi,

I have the following problem:
I want to store file objects in a container 8for now, it's not important how they look like).
Now, on the one hand I need to be able to randomly pick files from the container as fast as possible. So far I have been using std::map to store the files, whereas the key was a unique file ID. That given, I could pick random files from the container in O(logn).
On the other hand, I need functionality to sort the files, e.g. by date or size. However, std::sort and std::stable_sort only seem to work on
non-associative containers. On top of that, std::map doesn't define an own sorting operation, like std::list does.

My questions:

1) How come there's no sorting operation for std::map?

2) Which solutions are at hand? Is there such a thing as a sortable map at all?

Thanks,
Matthias

Check out the Boost Multi-Index Library at
http://www.boost.org/libs/multi_index/doc/index.html

It lets you define multiple "views" of data. So one view could be
sorted by filename, another sorted by date, and another by size.
--
Martin Rennix

Jul 22 '05 #4
Martin Rennix wrote:
Check out the Boost Multi-Index Library at
http://www.boost.org/libs/multi_index/doc/index.html

It lets you define multiple "views" of data. So one view could be
sorted by filename, another sorted by date, and another by size.


Haven't read through the whole tutorial yet, but it looks like this is
*exactly* what I need. Thanks Martin.
Jul 22 '05 #5
> http://www.boost.org/libs/multi_index/doc/index.html

Hmm, is this a very recent addition to boost? Because after reading the docs
I wanted to give it a try and when he couldn't find the header I searched
for it by hand in /usr/include and it's not there. I'm running boost 1.31.0
(Debian Sarge).
Jul 22 '05 #6
Matthias Käppler wrote in news:co*************@news.t-online.com in
comp.lang.c++:
http://www.boost.org/libs/multi_index/doc/index.html


Hmm, is this a very recent addition to boost? Because after reading
the docs I wanted to give it a try and when he couldn't find the
header I searched for it by hand in /usr/include and it's not there.
I'm running boost 1.31.0 (Debian Sarge).


Its in the latest release 1.32.

FYI: Boost have usenet access to there user support mailing list:

news:gmane.comp.lib.boost.user

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7
Matthias Käppler <no****@digitalraid.com> wrote in message news:<co*************@news.t-online.com>...
Martin Rennix wrote:
Check out the Boost Multi-Index Library at
http://www.boost.org/libs/multi_index/doc/index.html

It lets you define multiple "views" of data. So one view could be
sorted by filename, another sorted by date, and another by size.


Haven't read through the whole tutorial yet, but it looks like this is
*exactly* what I need. Thanks Martin.


No worries. Check out the rest of Boost while you're there, it's a
veritable treasure chest of goodies.

--
Martin Rennix
Jul 22 '05 #8

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

Similar topics

2
by: Ney André de Mello Zunino | last post by:
Hello. A non-modifying algorithm I implemented uses two associative containers from STL: set and map. The elements on those containers are supposed to refer to actual elements which lie on...
2
by: T.Meitz | last post by:
Hi everybody, I'm looking for an associative container (like STL map), with a regex like "find" method. Any suggestions are welcome. greetings Thomas
3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
2
by: rusmo1 | last post by:
I have a GridvVew in which all of the columns are sortable. I want the first column to display the position of the row in the sorted order regardless of which column I've sorted or which direction...
21
by: aaragon | last post by:
Hello everyone, I would like to know if there is a way to use the std::map to store different types for one of its two types. That is, I'm trying to use it as: typedef...
1
by: yonil | last post by:
I hope this is the correct group for this... I'm currently implementing the TR1 associative containers according to specification found in...
5
by: desktop | last post by:
set, map, multiset and multimap are all associative containers. But what does the word "associative" have to do with these 4 containers?
3
by: subramanian100in | last post by:
I am copying the following lines as it is, from Stanley Lippman's C++ Primer 4th edition, page 418(First paragraph). It says: "Although the map and set types provide bidirectional iterators, we...
1
by: anonymous | last post by:
I have a function that returns a map<string, vector<int. Inside the function I load the map with this instruction: loc_map.push_back(1); I would like to remove the output data structure and to...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.