Sunil Singh
Sunil Singh

Reputation: 324

How to use vector of map

    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?

Upvotes: 3

Views: 21880

Answers (4)

ivaigult
ivaigult

Reputation: 6667

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

Upvotes: 1

Marcel Kr&#252;ger
Marcel Kr&#252;ger

Reputation: 909

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
    }
}

Upvotes: 1

Humam Helfawi
Humam Helfawi

Reputation: 20264

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).

Upvotes: 7

xaxxon
xaxxon

Reputation: 19761

// 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.

Upvotes: 5

Related Questions