为容器编写打印函数的必要性? [英] Necessity of writing print functions for containers?

查看:93
本文介绍了为容器编写打印函数的必要性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用大约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:


  1. 提供大量格式化选项。

  2. 不提供任何格式设置选项,让每个不喜欢自己格式的人都可以自己设置。

  3. 不做任何事情, b
  1. Provide a lot of formatting options.
  2. Provide no formatting options and make everyone who doesn't like their formatting roll their own.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆