我使用大约 6 个不同的 C++ 容器。我开始编写打印函数来输出容器内容。有这个必要吗?我认为这是 C++ 库的一部分?
void print_list(const list<int>& list_int)
{
for (list<int>::const_iterator it = list_int.begin(); it != list_int.end(); it++) cout << *it << " ";
}
void print_um(const unordered_map<int, double>& map_int_d)
{
for(unordered_map<int, double>::const_iterator it = map_int_d.begin(); it != map_int_d.end(); ++it)cout << "[" << it->first << "," << it->second << "] ";
}
它不是库的一部分,但使用提供的工具可以轻松编写:
C c; // Where C is a container type
std::copy(
c.begin(), c.end()
, std::ostream_iterator< C::value_type >( cout, " " )
);
对于元素为 a 的容器pair
(like map
我相信unordered_map
)你需要一个自定义输出迭代器来打印pair
与逗号和括号。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)