乘以整数(c ++) [英] Multiply char by integer (c++)

查看:328
本文介绍了乘以整数(c ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用一个int乘以一个char?

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 doesn't work

char star = "*";
int num = 7;

cout << star * num //to output 7 stars


推荐答案

I wouldn 't调用操作乘法,这只是混乱。连接是一个更好的词。

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

在任何情况下,名为 std :: string 的C ++标准字符串类都有一个构造函数,

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 );

内容被初始化为由字符 c n 次。

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

所以你可以这样走:

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

确保包含相关标题< string>

这篇关于乘以整数(c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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