Print occurence list

Discussion in 'C++' started by William Payne, Feb 18, 2004.

  1. 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
     
    William Payne, Feb 18, 2004
    #1
    1. Advertising

  2. "William Payne" <> wrote in message
    news:c1037s$msu$...
    > 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
     
    John Harrison, Feb 18, 2004
    #2
    1. Advertising

  3. "John Harrison" <> wrote in message
    news:c103jo$1cehr1$-berlin.de...
    >
    > "William Payne" <> wrote in message
    > news:c1037s$msu$...
    > > 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
     
    William Payne, Feb 18, 2004
    #3
  4. William Payne

    David Rubin Guest

    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
     
    David Rubin, Feb 18, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dustin D.
    Replies:
    1
    Views:
    11,275
  2. Mr Ideas Man

    Occurence of Char!

    Mr Ideas Man, Feb 10, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    492
    Terry Burns
    Feb 10, 2006
  3. natG
    Replies:
    4
    Views:
    9,695
  4. Roland
    Replies:
    1
    Views:
    1,556
    Oleg Tkachenko
    Sep 25, 2003
  5. Xavier Noria

    print LIST vs print join "", LIST

    Xavier Noria, Apr 16, 2004, in forum: Perl Misc
    Replies:
    14
    Views:
    200
    Uri Guttman
    Apr 19, 2004
Loading...

Share This Page