简体   繁体   中英

Unable to print vector values from map in maps

I am trying to print out the whole contents of map of maps but keep encountering into issues. Here is my map initialisation map<int, map<int, vector<int>>> myMap;

I have tried the following code:

for( auto const & cit : myMap)
    {
        cout << cit.first << " : ";
        auto const & imap = cit.second;
        for( auto const & cit2 : imap )
        {
            cout << cit2.first << ":" << cit2.second << ","; // errors out
            //cout << cit2.first << ":"; // works, but it is not printing out the vector<int> portion
        }
        cout << endl;
    }

As mentioned above, as soon as cit2.second is used, I got the following error: error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'const std::vector<int>')|

can someone kindly give me some insights?

You need to do it like this.

for( auto const & cit : myMap)
    {
        cout << cit.first << " : ";
        auto const & imap = cit.second;
        for( auto const & cit2 : imap )
        {
            cout << cit2.first << ":";
            auto const &vec = cit2.second;
            for(auto const &i : vec)
            {
                cout<<i<<" ";
            }cout<<endl;   
            //cout << cit2.first << ":"; // works, but it is not printing out the vector<int> portion
        }
        cout << endl;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM