通过模板,我重载了运算符<<这样它就输出了容器的所有元素:
template<typename T, template<typename> typename C>
ostream& operator<<(ostream& o, const C<T>& con) { for (const T& e : con) o << e; return o; }
它适用于std::vector
,但当我尝试将其应用于std::list
时会产生错误消息:
错误:'operator&lt;&lt;'不匹配(操作数类型为'std :: ostream {aka std :: basic_ostream}'和'std :: __ cxx11 :: list')cout&lt;&lt; 立;
这是我的代码摘录(在GCC 5.2.1,Ubuntu 15.10上编译):
#include "../Qualquer/std_lib_facilities.h"
struct Item {
string name;
int id;
double value;
Item(){};
Item(string n, int i, double v):
name{n}, id{i}, value{v} {}
};
istream& operator>>(istream& is, Item& i) { return is >> i.name >> i.id >> i.value; }
ostream& operator<<(ostream& o, const Item& it) { return o << it.name << '\t' << it.id << '\t' << it.value << '\n'; }
template<typename T, template<typename> typename C>
ostream& operator<<(ostream& o, const C<T>& con) { for (const T& e : con) o << e; return o; }
int main()
{
ifstream inp {"../Qualquer/items.txt"};
if(!inp) error("No connection to the items.txt file\n");
istream_iterator<Item> ii {inp};
istream_iterator<Item> end;
vector<Item>vi {ii, end};
//cout << vi;//this works OK
list<Item>li {ii, end};
cout << li;//this causes the error
}
但是,当我专门为std::list
编写模板时,它可以正常工作:
template<typename T>
ostream& operator<<(ostream& o, const list<T>& con) { for (auto first = con.begin(); first != con.end(); ++first) o << *first; return o; }
为什么ostream& operator<<(ostream& o, const C<T>& con)
模板不适用于std::list
?
答案 0 :(得分:2)
template<typename T, template<typename> typename C>
ostream& operator<<(ostream& o, const C<T>& con) { for (const T& e : con) o << e; return o; }
为什么这么复杂?您只需要在for循环中使用类型名称T
。您也可以通过C::value_type
或仅使用auto
关键字:
template<typename C>
ostream& operator<<(ostream& o, const C& con)
{
for (const typename C::value_type& e : con) o << e; return o;
}
template<typename C>
ostream& operator<<(ostream& o, const C& con)
{
for (auto& e : con) o << e; return o;
}