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

发布于 2024-08-28 06:00:29 字数 188 浏览 5 评论 0原文

是否可以将 char 与 int 相乘?

例如,我正在尝试制作一个图表,每次出现数字时都用 * 表示。

就像这样,但这不起作用

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心碎的声音 2024-09-04 06:00:29

我不会将该操作称为“乘法”,这只是令人困惑。连接是一个更好的词。

无论如何,名为 std::string 的 C++ 标准字符串类有一个非常适合您的构造函数。

string ( size_t n, char c );

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

因此,您可以这样操作:

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

确保包含相关标头

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

紙鸢 2024-09-04 06:00:29

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

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

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

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.

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

char star = '*';
int num = 7;
for(int i=0; i<num; i++)
{
    cout << star;
}// outputs 7 stars. 
‘画卷フ 2024-09-04 06:00:29

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

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

#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;
}
懵少女 2024-09-04 06:00:29

你可以这样做:

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

You could do this:

std::cout << std::string(7, '*');
安人多梦 2024-09-04 06:00:29

语句应该是:

char star = "*";

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

要输出 '*' n 次,请遵循其他人灌输的想法。

希望这有帮助。

The statement should be:

char star = "*";

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

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

Hope this helps.

瞄了个咪的 2024-09-04 06:00:29
//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;

}

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文