How to write 'n' copies of a character to ostream like in python

后端 未结 4 946
半阙折子戏
半阙折子戏 2021-01-02 00:54

In python, the following instruction: print \'a\'*5 would output aaaaa. How would one write something similar in C++ in conjunction with std:

4条回答
  •  爱一瞬间的悲伤
    2021-01-02 01:32

    The obvious way would be with fill_n:

    std::fill_n(std::ostream_iterator(std::cout), 5, 'a');
    

    Another possibility would be be to just construct a string:

    std::cout << std::string(5, 'a');
    

提交回复
热议问题