小编Rog*_*ido的帖子

从`std :: ostream_iterator`调用时找不到`operator <<`的重载?

// main.cpp
Run Code Online (Sandbox Code Playgroud)

#include <iostream>
#include <utility>
#include <algorithm>
#include <iterator>
#include <map>

template<typename t1, typename t2>
std::ostream& operator<<(std::ostream& os, const std::pair<t1, t2>& pair)
{
  return os << "< " << pair.first << " , " << pair.second << " >";
}

int main() 
{
  std::map<int, int> map = { { 1, 2 }, { 2, 3 } };
  std::cout << *map.begin() << std::endl;//This works

  std::copy(
    map.begin(),
    map.end(),
    std::ostream_iterator<std::pair<int,int> >(std::cout, " ")
  ); //this doesn't work
}
Run Code Online (Sandbox Code Playgroud)

no match for ‘operator<<’ …

c++ c++11

5
推荐指数
1
解决办法
3256
查看次数

标签 统计

c++ ×1

c++11 ×1