0
// Map created std::map<int, std::vector<int>> _map; // Key/Data inserted _map.insert(std::pair<int, std::vector<int> >(0, { i })); // Display values [ERROR] for (const auto &p : _map) { std::cout << "m[" << p.first << "] = " << p.second << '\n'; } 

It is a very simple program of creating a map, inserting values and display both key/pair by iterating over the entire map. I am able to display map key (p.first) but I am not able to display the value of data (p.second).

Error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::vector>' (or there is no acceptable conversion)

3
  • 3
    What do you think p.second returns? It returns a vector of ints so you need to write an inner for loop to go through the vector and print the ints there. A vector does not define the << operator. Also you are using the new standard so instead of making a temporary pair to the insert function you can use brace initialisation._map.insert({0, {i}}) Commented Jul 10, 2015 at 5:55
  • I can't access the p.second value, then how would I be able to use it to create nested loop? can you please give a small example? thank you Commented Jul 10, 2015 at 5:58
  • You can access it. std::cout does not have operator << which could take a vector as parameter. Add some inner loop like that: for(auto &pp : p.second) {std::cout<<pp; } Commented Jul 10, 2015 at 6:01

3 Answers 3

4

p.second in the std::vector<int> and the Standard Library does not overload operator<< for vectors, as stated by the error message. So you'll have to add an inner loop to go through the vector and print the values it contains.

Instead of...

std::cout << "m[" << p.first << "] = " << p.second << '\n'; 

...try...

std::cout << "m[" << p.first << "] = {"; for (int n : p.second) std::cout << ' ' << n; std::cout << " }\n"; 
Sign up to request clarification or add additional context in comments.

Comments

2

Alternatively, you can print the content of p.second using std::copy

std::copy(p.second.begin(), p.second.end(), std::ostream_iterator<int>(std::cout, " ")); 

For a more general solution, I suggest using the great C++ container pretty print library. That library makes your code work without further alterations:

std::cout << p.second << std::endl; 

1 Comment

I like the fact that it is a one-line instruction. Let me remove the comment on the convenience of this approach so as to present it in a more neutral way.
0

You'll have to define the operator >> for std::vector yourself

template <typename T> std::ostream & operator >>(std::ostream & out, const std::vector<T> & vec) { std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(out, " ")); return out; } 

Your code will work as is after you add this definition.

for (const auto & p : _map) { std::cout << "m[" << p.first << "] = " << p.second << '\n'; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.