d05hjkl
d05hjkl

Reputation: 39

How to cout list's values with iterator?

The question is in the title, I have tried this as I used it in the past but it doesnt work now:

for (list<string>::iterator it = list1.begin(); it != list1.end(); it++){
    cout << *it;
}

The error is the following:

"no operator "<<" matches these operands operand types are: std::ostream << std::string "

I havent used c++ like for 2 years and now Im stucked here, used google but didnt find any good answer.

Thanks in advance for your answer!

Upvotes: 2

Views: 1010

Answers (2)

Baum mit Augen
Baum mit Augen

Reputation: 50081

You forgot to include a header, most likely <string> (or, more unlikely, <iostream> or <list>). All three of those are required, you may not rely on any standard library header including another.

Upvotes: 3

user5840769
user5840769

Reputation: 11

The operator<< overloads for std::string are actually not found in <iostream> but in <string>. The standard does not restrict a library implementation from including arbitrary headers, so most implementations will include <string> somewhere up the chain. In the event that yours doesn't, you need to include <string> manually.

Upvotes: 1

Related Questions