将字符乘以整数 (c++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2596953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:01:55  来源:igfitidea点击:

Multiply char by integer (c++)

c++stringcharmultiplying

提问by dubya

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

回答by janks

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.

在任何情况下,C++ 标准字符串类,named std::string,都有一个非常适合你的构造函数。

string ( size_t n, char c );

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

内容被初始化为由字符c,n次重复形成的字符串。

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, <string>.

确保包含相关的标题,<string>.

回答by Randolpho

the way you're doing it will do a numeric multiplication of the binary representation of the '*'character against the number 7 and output the resulting number.

你这样做的方式是将'*'字符的二进制表示与数字 7进行数字乘法,并输出结果数字。

What you want to do (based on your c++ code comment) is this:

你想要做的(基于你的 C++ 代码注释)是这样的:

char star = '*';
int num = 7;
for(int i=0; i<num; i++)
{
    cout << star;
}// outputs 7 stars. 

回答by SoapBox

GMan's over-eningeering of this problem inspired me to do some template meta-programming to further over-engineer it.

GMan 对这个问题的过度设计激发了我进行一些模板元编程以进一步对其进行过度设计。

#include <iostream>

template<int c, char ch>
class repeater {
  enum { Count = c, Char = ch };
  friend std::ostream &operator << (std::ostream &os, const repeater &r) {
    return os << (char)repeater::Char << repeater<repeater::Count-1,repeater::Char>();
  }
};

template<char ch>
class repeater<0, ch> {
  enum { Char = ch };
friend std::ostream &operator << (std::ostream &os, const repeater &r) {
    return os;
  }
};

main() {
    std::cout << "test" << std::endl;
    std::cout << "8 r = " << repeater<8,'r'>() << std::endl;
}

回答by Sean

You could do this:

你可以这样做:

std::cout << std::string(7, '*');

回答by webgenius

The statement should be:

声明应该是:

char star = "*";

(star * num) will multiply the ASCII value of '*' with the value stored in num

(star * num) 将 '*' 的 ASCII 值与存储在 num 中的值相乘

To output '*' n times, follow the ideas poured in by others.

要输出'*'n次,就按照别人的想法。

Hope this helps.

希望这可以帮助。

回答by Nullterminator

//include iostream and string libraries

using namespace std;

int main()
{

    for (int Count = 1; Count <= 10; Count++)
    {
        cout << string(Count, '+') << endl;
    }

    for (int Count = 10; Count >= 0; Count--)
    {
        cout << string(Count, '+') << endl;
    }


return 0;

}

}