Multiply char by integer (c++)

后端 未结 6 661
甜味超标
甜味超标 2020-12-31 16:03

Is it possible to multiply a char by an int?

For example, I am trying to make a graph, with *\'s for each time a number occurs.

So something like, but this d

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 16:23

    I wouldn't call that operation "multiplication", that's just confusing. Concatenation is a better word.

    In any case, the C++ standard string class, named std::string, has a constructor that's perfect for you.

    string ( size_t n, char c );
    

    Content is initialized as a string formed by a repetition of character c, n times.

    So you can go like this:

    char star = '*';  
    int num = 7;
    std::cout << std::string(num, star) << std::endl;  
    

    Make sure to include the relevant header, .

提交回复
热议问题