1

I have a problem with outputting the elements from a map. My code:

#include <iostream>
#include <map>
#include <string>

int main()
{

  int t,n;
  std::string cont;

  std::cin >> t;
  while(t--)
  {
    std::cin >> n;
    std::map<std::string,int> citire;
    std::map<std::string,int>::iterator it;
    for(int i=0; i<n; i++)
    {
      std::getline(std::cin,cont);
      citire[cont]++;
    }

    for(it=citire.begin(); it!=citire.end(); ++it)
      std::cout << it->first << " " << it->second << '\n';

    std::cout << '\n';
  }

  return 0;
}

For the input:

2
6
03 10103538 2222 1233 6160 0142 
03 10103538 2222 1233 6160 0141 
30 10103538 2222 1233 6160 0141 
30 10103538 2222 1233 6160 0142 
30 10103538 2222 1233 6160 0141 
30 10103538 2222 1233 6160 0142 

5
30 10103538 2222 1233 6160 0144 
30 10103538 2222 1233 6160 0142 
30 10103538 2222 1233 6160 0145 
30 10103538 2222 1233 6160 0146 
30 10103538 2222 1233 6160 0143 

, I have used one line as a key in map. All I have to do is to output the key and its value is decreasing order by the key.

Program's Output:

1
03 10103538 2222 1233 6160 0141  1
03 10103538 2222 1233 6160 0142  1
30 10103538 2222 1233 6160 0141  2
30 10103538 2222 1233 6160 0142  1

 1
 10103538 2222 1233 6160 0142  1
30 10103538 2222 1233 6160 0142  1
30 10103538 2222 1233 6160 0143  23
30 10103538 2222 1233 6160 0144  1
30 10103538 2222 1233 6160 0145  1
30 10103538 2222 1233 6160 0146  1
5 1

Correct Output:

03 10103538 2222 1233 6160 0141 1
03 10103538 2222 1233 6160 0142 1
30 10103538 2222 1233 6160 0141 2
30 10103538 2222 1233 6160 0142 2

30 10103538 2222 1233 6160 0142 1
30 10103538 2222 1233 6160 0143 1
30 10103538 2222 1233 6160 0144 1
30 10103538 2222 1233 6160 0145 1
30 10103538 2222 1233 6160 0146 1

Am I doing something wrong?

m.s.
  • 16,063
  • 7
  • 53
  • 88

0 Answers0