为集合重载运算符<<
overloading operator<< for set
我有一组整数对,我想打印它,所以我为集合和对 类 重载了 << 运算符作为 :
template<typename T, typename U>
inline ostream& operator<<(ostream& os, pair<T,U> &p){
os<<"("<<p.first<<","<<p.second<<")";
return os;
}
template<typename T>
inline ostream& operator<<(ostream& os, set<T> &s){
os<<"{";
for(auto it = s.begin() ; it != s.end() ; it++){
if(it != s.begin())
os<<",";
os<<*it;
}
os<<"}";
return os;
}
当我创建一个集合并像这样输出它时
set<pair<int,int>> s;
cout<<s<<endl;
它给出了错误:
cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
os<<*it;
和
initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::pair<int, int>]’
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
不知道是什么问题,错误很隐晦。此外,如果我创建一组整数并打印它,它工作正常。
流运算符并不意味着修改它们传递的值。因此,您应该接受 const 引用而不是可变引用:
template<typename T, typename U>
inline ostream& operator<<(ostream& os, const pair<T,U> &p){
os<<"("<<p.first<<","<<p.second<<")";
return os;
}
template<typename T>
inline ostream& operator<<(ostream& os, const set<T> &s){
os<<"{";
for(auto it = s.begin() ; it != s.end() ; it++){
if(it != s.begin())
os<<",";
os<<*it;
}
os<<"}";
return os;
}
auto it = s.begin()
中it
的类型是const_iterator
(source)。因此,当您调用 os<<*it;
时,您需要一个可以接受 const
的函数
一对。如果您将代码更改为此,它将起作用:
#include <iostream>
#include <set>
#include <utility>
using namespace std;
template<typename T, typename U>
inline ostream& operator<<(ostream& os, const pair<T,U> &p){
os<<"("<<p.first<<","<<p.second<<")";
return os;
}
template<typename T>
inline ostream& operator<<(ostream& os, const set<T> &s){
os<<"{";
for(auto it = s.begin() ; it != s.end() ; it++){
if(it != s.begin())
os<<",";
os<<*it;
}
os<<"}";
return os;
}
int main()
{
set<pair<int,int>> s {{1,2}};
cout<<s<<endl;
}
我还建议您始终将第二个参数作为 const &
for
您可以将临时绑定到 const &
(前函数 returns)
你不应该在输出期间修改容器,所以这使用 C++ 类型系统来强制执行。
我有一组整数对,我想打印它,所以我为集合和对 类 重载了 << 运算符作为 :
template<typename T, typename U>
inline ostream& operator<<(ostream& os, pair<T,U> &p){
os<<"("<<p.first<<","<<p.second<<")";
return os;
}
template<typename T>
inline ostream& operator<<(ostream& os, set<T> &s){
os<<"{";
for(auto it = s.begin() ; it != s.end() ; it++){
if(it != s.begin())
os<<",";
os<<*it;
}
os<<"}";
return os;
}
当我创建一个集合并像这样输出它时
set<pair<int,int>> s;
cout<<s<<endl;
它给出了错误:
cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
os<<*it;
和
initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::pair<int, int>]’
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
不知道是什么问题,错误很隐晦。此外,如果我创建一组整数并打印它,它工作正常。
流运算符并不意味着修改它们传递的值。因此,您应该接受 const 引用而不是可变引用:
template<typename T, typename U>
inline ostream& operator<<(ostream& os, const pair<T,U> &p){
os<<"("<<p.first<<","<<p.second<<")";
return os;
}
template<typename T>
inline ostream& operator<<(ostream& os, const set<T> &s){
os<<"{";
for(auto it = s.begin() ; it != s.end() ; it++){
if(it != s.begin())
os<<",";
os<<*it;
}
os<<"}";
return os;
}
auto it = s.begin()
中it
的类型是const_iterator
(source)。因此,当您调用 os<<*it;
时,您需要一个可以接受 const
的函数
一对。如果您将代码更改为此,它将起作用:
#include <iostream>
#include <set>
#include <utility>
using namespace std;
template<typename T, typename U>
inline ostream& operator<<(ostream& os, const pair<T,U> &p){
os<<"("<<p.first<<","<<p.second<<")";
return os;
}
template<typename T>
inline ostream& operator<<(ostream& os, const set<T> &s){
os<<"{";
for(auto it = s.begin() ; it != s.end() ; it++){
if(it != s.begin())
os<<",";
os<<*it;
}
os<<"}";
return os;
}
int main()
{
set<pair<int,int>> s {{1,2}};
cout<<s<<endl;
}
我还建议您始终将第二个参数作为 const &
for
您可以将临时绑定到
const &
(前函数 returns)你不应该在输出期间修改容器,所以这使用 C++ 类型系统来强制执行。