3
    vector <map<string,string>> dictionary;
    map <string, string> word1;
    map <string, string> word2;

    word1.insert(pair<string, string>("UNREAL","Abc"));
    word2.insert(pair<string, string>("PROPS","Efg"));

    dictionary.push_back(word1);
    dictionary.push_back(word2);

    vector<map<string, string>>::iterator it;
    it = dictionary.begin();

    for( it; it != dictionary.end(); it++)
    {
                cout << it << " " << it << endl; //ERROR
    }

I want to display the data stored in vector. Please suggest me how to display output from vector dictionary?

Sunil Singh
  • 214
  • 1
  • 3
  • 15

4 Answers4

7

In order to solve your problem as it is, you should do this:

for(it = dictionary.begin(); it != dictionary.end(); it++){
    for(auto it1=it->begin();it1!=it->end();++it1){
        cout << it1->first << " " << it->second << endl; 
    }
}

However, I think there is something wrong with the design. In your case you do not need vector of maps... you need vector of pairs or just a map.

vector of pairs:

std::vector <std::pair<string,string>> dictionary;
dictionary.emplace_back("UNREAL","Abc");
dictionary.emplace_back("PROPS","Efg");
for(auto const& item:dictionary){
    std::cout << item.first << " " << item.second;
}

map:

 std::map<string,string> dictionary;
 dictionary.insert("UNREAL","Abc");//also :  dictionary["UNREAL"]="Abc";
 dictionary.insert("PROPS","Efg");//also :  dictionary["PROPS"]="Efg";
 for(auto const& item:dictionary){
     std::cout << item.first << " " << item.second;
 }

Because map is not just a pair of something, it is a set of pairs (kind of not accurate).

Humam Helfawi
  • 17,706
  • 12
  • 64
  • 134
5
// i is each map in your vector
for (auto i : dictionary) {
    // j is each std::pair<string,string> in each map
    for (auto j : i) {
      // these are the two strings in each pair
      j.first; j.second;
  } 
}

This answer requires c++11, but pretty much everything supports that these days.

xaxxon
  • 17,640
  • 4
  • 42
  • 70
1

vector and map are two containers, so you need two nested loops. In the nested loop over the map, your iterator refers to std::pairs so you access the key with .first and the value with .second:

vector <map<string,string> > dictionary;
map <string, string> word1;
map <string, string> word2;

word1.insert(pair<string, string>("UNREAL","Abc"));
word2.insert(pair<string, string>("PROPS","Efg"));

dictionary.push_back(word1);
dictionary.push_back(word2);

vector<map<string, string> >::iterator it;
for( it = dictionary.begin(); it != dictionary.end(); ++it)
{
    map<string, string>::iterator it;
    for( nested = it.begin(); nested != it.end(); ++nested)
    {
        cout << it->first << " " << it->second << endl; //ERROR
    }
}

If you use C++11, you can make this shorter with Range-based for-loops:

vector <map<string,string>> dictionary;
map <string, string> word1;
map <string, string> word2;

word1.insert(pair<string, string>("UNREAL","Abc"));
word2.insert(pair<string, string>("PROPS","Efg"));

dictionary.push_back(word1);
dictionary.push_back(word2);

for(const map<string, string> &outer : dictionary)
{
    for(const pair<string, string> & inner : outer)
    {
        cout << inner.first << " " << inner.second << endl; //ERROR
    }
}
Marcel Krüger
  • 745
  • 8
  • 13
  • use `auto &` or `const auto &` instead of `const map` and `const pair`. – Johan Lundberg Jan 12 '16 at 07:35
  • 1
    //This also worked. for(it = dictionary.begin(); it != dictionary.end(); it++) { cout << it->begin()->first << " " << it->begin()->second << endl; } – Sunil Singh Jan 12 '16 at 08:48
  • @JohanLundberg I think in this case with nested containers writing the actual types makes it easier for someone without much C++11 experience to see what's actually going on. – Marcel Krüger Jan 12 '16 at 08:54
1

There is an answer on similar question here. The code will allow you to print any stl container with any template parameters using << operator.

Community
  • 1
  • 1
ivaigult
  • 4,619
  • 5
  • 27
  • 48