Print occurence list

W

William Payne

Hello, I'm working on a program where to user supplies a number of integers
on the command line and then my program displays a list with all the
integers, stating how many times each integer occured, sorted from highest
to lowest.
I thought std::map<int, int> would be a good idea, where the key is an int
supplied on the command line and its value is how many times it occurs. I am
unsure about how to print a sorted list, though. I tried to use
max_element() to see if I can at least get the highest one and work from
there, but it gives me a three page compilation error that I don't intend to
duplicate here. The code I have so far is:

#include <iostream>
#include <map>
#include <sstream>
#include <algorithm>
#include <functional>
using std::map;
using std::cout;
using std::endl;
using std::stringstream;

void count(map<int, int>& the_map, int argc, const char* const argv[]);

int main(int argc, char* argv[])
{
map<int, int> the_map;

count(the_map, argc, argv);

cout << *(std::max_element(the_map.begin(), the_map.end())) << endl;

return 0;
}

void count(map<int, int>& the_map, int argc, const char* const argv[])
{
for(int i = 1; i < argc; ++i)
{
stringstream ss;

ss << argv;

int n = 0;

if(ss >> n)
{
if(the_map.count(n))
{
++the_map[n];
}
else
{
++the_map[n] = 1;
}
}
}
}

If you have any ideas on how to solve this, I'd appreciate it alot if you'd
share them!

/ WP
 
J

John Harrison

William Payne said:
Hello, I'm working on a program where to user supplies a number of integers
on the command line and then my program displays a list with all the
integers, stating how many times each integer occured, sorted from highest
to lowest.
I thought std::map<int, int> would be a good idea, where the key is an int
supplied on the command line and its value is how many times it occurs. I am
unsure about how to print a sorted list, though.

Just iterate through the map, it is already sorted.

for (map<int, int>::const_iterator i = the_map.begin();
i != the_map.end(); ++i)
{
cout << i->first << ' ' << i->second; '\n';
}

john
 
W

William Payne

John Harrison said:
I

Just iterate through the map, it is already sorted.

for (map<int, int>::const_iterator i = the_map.begin();
i != the_map.end(); ++i)
{
cout << i->first << ' ' << i->second; '\n';
}

john

Thanks alot, John. Silly me that I didn't try to that to see the order...I
was thinking it would be random, lol. But now I know better and I know to
try things out next time.
Thanks again!

/ WP
 
D

David Rubin

William Payne wrote:

[snip - how do you sort a map?]
Thanks alot, John. Silly me that I didn't try to that to see the order...I
was thinking it would be random, lol. But now I know better and I know to
try things out next time.

Or try reading the documentation. It's almost ubiquitous on the Web. For example,

http://www.cs.vassar.edu/~cs203/stl/Map.html

"Sorted" is the fourth word of the description...

/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,428
Latest member
RosalieQui

Latest Threads

Top