943,704 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 35429
  • C++ RSS
Aug 20th, 2004
1

sorting stl::map

Expand Post »
Does anyone know of a good way to sort an stl map?

I have something like the following example:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <map>
  3. #include <algorithm>
  4.  
  5. int main()
  6. {
  7. map<int, float> map_to_sort;
  8.  
  9. // Populate the map with between 20 - 2000 elements
  10.  
  11. map_to_sort.sort();
  12. }

Obviously I am aware that map<> doesn't have a sort function :op so does anyone have a quick method of sorting through a map?

From the best I can see I am going to have to manually iterate through all elements of the map and compare and move to a seperate map, I am hoping there is a better solution than this since that seems like a wasteful use of CPU time, this is for a searchengine project I am working on and as can be expected time is critical in this application, especially considering I actually need to sort through 3 seperate maps.

Any help would be really appreciated.

Best regards

Ben
Similar Threads
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 21st, 2004
0

Re: sorting stl::map

Hi Dave

I kinda figured there wasn't a sort() algorithm on map<> but I was hoping that maybe someone had written some efficient code to do a sort, and if so if they could give a few pointers on how it is achieved.

Thanks

Ben
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
Apr 14th, 2010
-1
Re: sorting stl::map
Hi,
C++ Maps are sorted associative containers that contain unique key/value pairs. Maps are sorted by their keys.When you insert new data into the map,Map will sort automatically.So you no need separate function to sort the data in Map. Forgive me if you are searching any other kind of sort.
Last edited by mn.balakumar; Apr 14th, 2010 at 2:29 pm.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
mn.balakumar is offline Offline
10 posts
since Jan 2010
Apr 14th, 2010
0
Re: sorting stl::map
FYI, mn.balakumar, you missed the boat by almost six years. You're welcome to questions, but keep in mind that for each old thread you resurrect, a recent thread goes ignored by falling to the second page.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Bank System Project
Next Thread in C++ Forum Timeline: Headcracking runtime errors std::map





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC