Necessity of writing print functions for containers?

后端 未结 3 761
刺人心
刺人心 2021-01-26 13:40

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?<

3条回答
  •  既然无缘
    2021-01-26 14:27

    It's not part of the library, but its easy to write with the tools provided:

    C c; // Where C is a container type
    std::copy(
        c.begin(), c.end()
      , std::ostream_iterator< C::value_type >( cout, " " )
    );
    

    For containers whose element is a pair (like map and I'd believe unordered_map) you'd need a custom Output Iterator to print the pair with the comma and brackets.

提交回复
热议问题