Print occurence list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Payne

    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::stringstre am;

    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_elem ent(the_map.beg in(), 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.coun t(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


  • John Harrison

    #2
    Re: Print occurence list


    "William Payne" <mikas493_no_s_ p_a_m_@student. liu.se> wrote in message
    news:c1037s$msu $1@news.island. liu.se...[color=blue]
    > Hello, I'm working on a program where to user supplies a number of[/color]
    integers[color=blue]
    > 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[/color]
    am[color=blue]
    > unsure about how to print a sorted list, though.[/color]

    Just iterate through the map, it is already sorted.

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

    john


    Comment

    • William Payne

      #3
      Re: Print occurence list


      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
      news:c103jo$1ce hr1$1@ID-196037.news.uni-berlin.de...[color=blue]
      >
      > "William Payne" <mikas493_no_s_ p_a_m_@student. liu.se> wrote in message
      > news:c1037s$msu $1@news.island. liu.se...[color=green]
      > > Hello, I'm working on a program where to user supplies a number of[/color]
      > integers[color=green]
      > > on the command line and then my program displays a list with all the
      > > integers, stating how many times each integer occured, sorted from[/color][/color]
      highest[color=blue][color=green]
      > > to lowest.
      > > I thought std::map<int, int> would be a good idea, where the key is an[/color][/color]
      int[color=blue][color=green]
      > > supplied on the command line and its value is how many times it occurs.[/color][/color]
      I[color=blue]
      > am[color=green]
      > > unsure about how to print a sorted list, though.[/color]
      >
      > Just iterate through the map, it is already sorted.
      >
      > for (map<int, int>::const_ite rator i = the_map.begin() ;
      > i != the_map.end(); ++i)
      > {
      > cout << i->first << ' ' << i->second; '\n';
      > }
      >
      > john
      >
      >[/color]

      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


      Comment

      • David Rubin

        #4
        Re: Print occurence list

        William Payne wrote:

        [snip - how do you sort a map?][color=blue]
        > 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.[/color]

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



        "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

        Comment

        Working...