473,394 Members | 1,785 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,394 software developers and data experts.

Print occurence list

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[i];

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
Jul 22 '05 #1
3 2087

"William Payne" <mi******************@student.liu.se> wrote in message
news:c1**********@news.island.liu.se...
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
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"William Payne" <mi******************@student.liu.se> wrote in message
news:c1**********@news.island.liu.se...
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


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
Jul 22 '05 #3
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

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 22 '05 #4

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

Similar topics

2
by: Tesla | last post by:
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :" Is there a function to find the position of the "I"th occurence of a character/string? Like lets say I want to find out...
1
by: Roland | last post by:
hi all having designed my schema to use repeating groups of elements, I found that some applications (eg. Microsoft InfoPath) refused to recognised that element as a repeating element. I...
0
by: Armin Wagenknecht | last post by:
Hello, i am modelling a TopicMap, and I have the following problem: I want to use two scopes in the topicmap for one occurence which has to satsify BOTH scopes!! Example: I have the...
10
by: Sean Berry | last post by:
I need to find the second to last occurence of a "." in a string. Basically I am taking a URL like http://this.is.mydomin.com/path/to/file.txt and want to extract /path/to/file.txt I...
6
by: utab | last post by:
Dear all, I would like to find the occurence of numbers in an array(I solved this problem before now I could not see the solution) but not with iterators or vectors I want to apply them later, I...
3
by: das | last post by:
Hi all, How can I get a row that has only one occurence in a table? Not through 'distinct' because this gets a single row that might have multiple occurences, I want to get only rows that have...
3
by: utab | last post by:
Dear all, I tried to solve the occurence problem: to find the distinct occurences of a word in an input. I know that I could use map and other STD lib functions. I tried to do it the hard way. I...
2
by: gauravguleria | last post by:
wht's the program which takes two strings using command line arguments and finds the occurence of second string in the first string.The program should return the starting position of first...
4
by: Caldas | last post by:
I'm trying to find files in the current directory that have the occurence of '<IWM_Datasource>NOUID</IWM_Datasource>' AND '<IWM_HasMetaData>true</IWM_HasMetaData>'. I was able to find just the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...

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.