#include <iostream> #include <string> #include <vector> #include <map> int main(){ freopen("input.txt","r+",stdin); freopen("output.txt","w",stdout); int nAccounts; std::cin >> nAccounts; std::string accountName; std::getline(std::cin, accountName); std::map<std::string, std::vector<std::string> > account; for (int i = 0; i <nAccounts; i++) { std::string accountName; std::getline(std::cin, accountName); account[accountName].push_back(accountName); } return 0; } 

How to go through the vectors in the map and, if the length of the vector is greater than 1, display the key?

 Пример входных\выходных данных: 5 abc bdf abc f f Выходные данные: abc f или f abc т.е. порядок вывода не важен. 
  • And what's the problem? for (std :: map <std :: string, std :: vector <std :: string>> :: iterator it = account.begin (); it! = account.end (); it ++) if (it-> second.size ()> 1) std :: cout << it-> first << std :: endl; - alexlz
  • @alexlz: you would use auto , otherwise it would completely scare the TSL from STL :) - VladD
  • @alexlz: 1. Well, the less low-level constructions, the fewer bugs 2. Yeah, C ++ 11, so 2013 is the year! It's time to use it at all. - VladD
  • @VladD 1. Then, can it be better to immediately abandon C ++? 2. This is for ubuntu maintenors. I have a 12.04 with gcc-4.6.3. And putting yourself - well, honestly - is reluctant. - alexlz
  • one
    @alexlz Already figured out with auto, really convenient :) instead of: std :: map <std :: string, std :: vector <std :: string>> :: iterator it = account.begin () just: auto it = account.begin () - arukasa

1 answer 1

In C ++, you can bypass any container using the construct

 for (auto& val : container) { //... } 

For std::map val will be a pair of key (string) and value (vector):

 for (auto& kv : container) { auto& key = kv.first; auto& vec = kv.second; //... } 

How to test the length of the vector and derive the key, I think, no need to explain.