943,794 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1028
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 12th, 2009
0

Sorting vectors

Expand Post »
How do i sort a vector based on the frequency of its elements

example

my vector contains 10,11,12,10,11,13,11

i need to get the sorted out put as
11,11,11,10,10,12,13

the normal sort is

sort(vec.begin(), vec.end());
how do i sort it in that way
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
newcook88 is offline Offline
27 posts
since May 2009
May 12th, 2009
1

Re: Sorting vectors

Here is what you are trying to do:
http://answers.yahoo.com/question/in...2195554AAANJgl
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
May 12th, 2009
0

Re: Sorting vectors

What is the ordering?
If you want to sort by the most frequent elements first, then you can use the link given to you.

If you are sorting by some other rule,
you should say what rule that is.
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
May 13th, 2009
0

Re: Sorting vectors

that link helps but how do i print the elements the the type of output vector in the code below
Quote ...
vector< pair<int, string> > output;
Reputation Points: 10
Solved Threads: 0
Light Poster
newcook88 is offline Offline
27 posts
since May 2009
May 13th, 2009
0

Re: Sorting vectors

Won't this be easy, just maintain seperate queue for each element and insert the element into the corresponding queue by dynamic memory allocation, then the queue for which front-reverse is the maximum will have maximum frequency
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
May 13th, 2009
0

Re: Sorting vectors

Click to Expand / Collapse  Quote originally posted by s_sridhar ...
Won't this be easy, just maintain seperate queue for each element and insert the element into the corresponding queue by dynamic memory allocation, then the queue for which front-reverse is the maximum will have maximum frequency
can you please show how its done
Reputation Points: 10
Solved Threads: 0
Light Poster
newcook88 is offline Offline
27 posts
since May 2009
May 13th, 2009
0

Re: Sorting vectors

or rather, may be you can use bucket sort, declare a two dimensional array provided you already know the range. just push the number into the corresponding bucket and based on the frequency you can then push it into the stack
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
May 13th, 2009
0

Re: Sorting vectors

i meant the same for that queue
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
May 13th, 2009
0

Re: Sorting vectors

Click to Expand / Collapse  Quote originally posted by s_sridhar ...
i meant the same for that queue
sir can you show me how to print the sorted values to screen in da example providen in the yahoo llink.. i am having trouble displaying the output
Reputation Points: 10
Solved Threads: 0
Light Poster
newcook88 is offline Offline
27 posts
since May 2009
May 13th, 2009
0

Re: Sorting vectors

Store the frequencies in a seperate array, sort them and display the contents of the buckets.
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Trouble with fstream
Next Thread in C++ Forum Timeline: making a graph in c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC