Achayan
Achayan

Reputation: 5885

list Items from a map variable

I am just trying to get the values from a map variable. But for some reason its giving a error: no match for 'operator<<' in 'std::operator<< error. Here is a snippet of my code.

std::map <string, list<int> > gradeList;
....
gradeList.insert(std::make_pair("A", itemVal));

And after that I am trying to print it with a for loop

for(map<string, list<int> >::iterator it = gradeList.begin(); it != gradeList.end(); ++it) {
        cout << it->first << " " << it->second <<  "\n";
    }

Which is giving me error. Any idea what's going on ?

thanks

Upvotes: 0

Views: 58

Answers (3)

Anant Simran Singh
Anant Simran Singh

Reputation: 637

it->second has type std::list.

<< is not overloaded for this .Hence the error.

If you want to print this, you can use a function of your own.

void print_list(std::list<int> a){
    for(auto i:a)
        std::cout<<i<<std::endl;
}

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

Expression it->second has type std::list<int>. There is no operator << overloaded for an object of type std::list. You have at least also to use a loop that to output elements of a list.

For example

for(map<string, list<int> >::iterator it = gradeList.begin(); it != gradeList.end(); ++it) {
        cout << it->first << ":";
        for ( int x : it->second ) cout << ' ' << x;
        cout << endl;
    }

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409136

You get an error because it->second is a std::list which doesn't have an overloaded output operator for it.

Upvotes: 3

Related Questions