gpt4 book ai didi

C++:在没有显式循环的情况下重复输出 i 次

转载 作者:太空宇宙 更新时间:2023-11-04 14:45:04 26 4
gpt4 key购买 nike

有没有办法做到这一点 没有循环 编写尽可能少的代码:

for (int i = 0; i < 10; i++) std::cout << 'x';

就像在 python 中一样:

print 'x' * 10

最佳答案

使用 std::string(size_t, char) constructor :

std::cout << std::string(10, 'x');

请注意,与 Python 不同,这仅适用于字符而不适用于字符串。

关于C++:在没有显式循环的情况下重复输出 i 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14860063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com