为容器编写打印函数的必要性? [英] Necessity of writing print functions for containers?
问题描述
我使用大约6个不同的C ++容器。我开始编写打印函数来输出容器内容。这是必要的吗?我认为这是C ++库的一部分?
I use about 6 different C++ containers. I started writing print functions to output the container contents. Is this necessary? I would think this is part of the C++ library?
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 << "] ";
}
推荐答案
问题有一个提示,为什么它不是标准库的一部分。您的代码使用方括号和不带空格的逗号表示地图中的对。其他人可能希望格式不同,因此标准委员会的选项是:
The code you give in your question has a hint as to why it is not part of the standard library. Your code uses square brackets and a comma with no spaces to show the pairs in the map. Other people may want it formatted differently so the options the standards committee had were:
- 提供大量格式化选项。
- 不提供任何格式设置选项,让每个不喜欢自己格式的人都可以自己设置。
- 不做任何事情, b
- Provide a lot of formatting options.
- Provide no formatting options and make everyone who doesn't like their formatting roll their own.
- Do nothing and make everyone roll their own
他们使用选项三,知道将开发满足人们特定需求的库。
They went with option three knowing that libraries would be developed that meet people's specific needs.
这篇关于为容器编写打印函数的必要性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!