0

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘std::pair<const std::__cxx11::basic_string, std::vector >’)

i want to same key and mutiple values, for example key is 10 values are 2,3,4 but "*iter" is wrong.. how to cout map,vector in c++?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
pythonyyyy
  • 13
  • 4

3 Answers3

2

In your code snippet the value of the expression *iter is an object of the type std::pair<std::string, std::vector<int>> for which the operator << is not defined.

And the error message

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and
 ‘std::pair<const std::__cxx11::basic_string, std::vector >’)

says about this.

The simplest way is to use the range-based for loop.

Here is a demonstrative program.

#include <iostream>
#include <string>
#include <vector>
#include <map>

int main() 
{
    std::map<std::string, std::vector<int>> m;
    
    m["10"].assign( { 2, 3, 4 } );
    
    for ( const auto &p : m )
    {
        std::cout << p.first << ": ";

        for ( const auto &item : p.second )
        {
            std::cout << item << ' ';
        }

        std::cout << '\n';
    }
    return 0;
}

The program output is

10: 2 3 4 

If you want to write ordinary for-loops using iterators then the loops can look the following way.

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iterator>

int main() 
{
    std::map<std::string, std::vector<int>> m;
    
    m["10"].assign( { 2, 3, 4 } );
    
    for ( auto outer_it = std::begin( m ); outer_it != std::end( m ); ++outer_it )
    {
        std::cout << outer_it->first << ": ";

        for ( auto inner_it = std::begin( outer_it->second ); 
             inner_it != std::end( outer_it->second );
             ++inner_it )
        {
            std::cout << *inner_it << ' ';
        }

        std::cout << '\n';
    }
    return 0;
}

Again the program output is

10: 2 3 4 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

I suggest using structured bindings and range-based for loops:

std::map<std::string,std::vector<int>> m;

for (auto&[str, vec] : m) { // bind str to "first" in the pair and vec to "second"
    std::cout << str << ':';
    for(auto lineno : vec) std::cout << ' ' << lineno;
    std::cout << '\n';
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

You can define how to print things via std::ostream like this:

#include <iostream>
#include <map>
#include <vector>
#include <string>

// define how to print std::pair<std::string, std::vector<int>>
std::ostream& operator<<(std::ostream& stream, const std::pair<std::string, std::vector<int>>& pair) {
    stream << "(" << pair.first << ", {";
    bool first = true;
    for (int e : pair.second) {
        if (!first) stream << ", ";
        stream << e;
        first = false;
    }
    stream << "})";
    return stream;
}

int main(void) {
    std::string yytext = "hoge";
    int lineno = 42;

    // below is copied from the question

    std::map<std::string,std::vector<int>> m; 
    m[yytext].push_back(lineno);

    std::map<std::string,std::vector<int>>::iterator iter;

    for (iter=m.begin(); iter!=m.end(); iter++){
        std::cout<<iter->first<<":"<<*iter<<std::endl;}
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70