C++ C+中的字符串乘法+;
这里已经有了一个问题:然而,由于这个问题表述得很糟糕,所以主要给出了关于字符乘法的答案。有两个正确但昂贵的答案,所以我将在这里提高要求C++ C+中的字符串乘法+;,c++,string,multiplication,repeat,C++,String,Multiplication,Repeat,这里已经有了一个问题:然而,由于这个问题表述得很糟糕,所以主要给出了关于字符乘法的答案。有两个正确但昂贵的答案,所以我将在这里提高要求 Perl提供了x操作符:可以让我这样做: $foo = "0, " x $bar; 我知道我可以用助手函数(如其他答案中的函数)来实现这一点。我想知道在没有自己的助手函数的情况下我能做到这一点吗?我的偏好是我可以初始化一个常量字符串,但如果我不能这样做,我很确定这可以用标准算法和lambda来回答。您可以覆盖乘法运算符 #include <string
Perl提供了
x
操作符:可以让我这样做:
$foo = "0, " x $bar;
我知道我可以用助手函数(如其他答案中的函数)来实现这一点。我想知道在没有自己的助手函数的情况下我能做到这一点吗?我的偏好是我可以初始化一个
常量字符串,但如果我不能这样做,我很确定这可以用标准算法和lambda来回答。您可以覆盖乘法运算符
#include <string>
#include <sstream>
#include <iostream>
std::string operator*(const std::string& str, size_t times)
{
std::stringstream stream;
for (size_t i = 0; i < times; i++) stream << str;
return stream.str();
}
int main() {
std::string s = "Hello World!";
size_t times = 5;
std::string repeated = s * times;
std::cout << repeated << std::endl;
return 0;
}
这可能更快:与相比,仅使用标准算法和lambda就可以实现这一点,但它仍然无法初始化常量字符串
,需要在单独的一行中完成:
string foo;
const auto bar = 13U;
generate_n(back_inserter(foo), bar * 3U, [](){
static const char multiplicand[] = "0, ";
static const auto length = strlen(multiplicand);
static auto i = 0U;
return multiplicand[i++ % length];});
我在这里创建了一个实例:但正如所有的问题注释所表明的那样,在一行中执行此操作的要求会导致代码质量低下。一开始,我们就可以看到裸常量3
,表示被乘数
的大小,并且不必要地需要更改被乘数
的初始化来更新此文本
应该作出的明显改进是:
string foo;
const auto bar = 13U;
const char multiplicand[] = "0, ";
const auto length = strlen(multiplicand);
generate_n(back_inserter(foo), bar * length, [&](){
static auto i = 0U;
return multiplicand[i++ % length];
});
下一个改进是在foo
增长时消除重新分配,如果bar
或length
很大,重新分配可能会很昂贵。这可以通过构造foo
来实现,其中有足够的空间来包含整个生成的字符串:
const auto bar = 13U;
const char multiplicand[] = "0, ";
const auto length = strlen(multiplicand);
string foo(bar * length, '\0');
generate_n(foo.begin(), bar * length, [&](){
static auto i = 0U;
return multiplicand[i++ % length];
});
[]“标准算法和lambda”-这怎么不是辅助函数?请澄清。你想在这里实现什么?@KarolyHorvath我认为这些部分结合在一起“在没有我自己的辅助函数的情况下实现它”-“如果我不能做到这一点,我很确定这可以用标准算法和lambda来回答”也许op想要为std::string
?@KarolyHorvath自定义乘法运算符大多数链接答案都是关于重复单个字符的。唯一的问题是重复一个完整的字符串。我对我的答案“真的很难看”和“不必要的复杂”一点也不生气,但你介意告诉我到底是什么让你感到困扰吗?链接的答案要复杂得多。@KarolyHorvath我也同意,当lambda函数只在一个地方使用时,使用它是不必要的。只在适当的位置编写代码要容易得多。string::reserve
而不是在构造时调整大小,这样可以避免两次初始化foo
。@Jarod42-Ummm。。。我既不调用reserve
也不调用resize
。你是在说我的构造参数,即calloc
sfoo
:stringfoo(bar*length,'\0')代码>?是的,我谈到了字符串foo(条形*长度,'\0')
。替换为字符串foo;食物储备(酒吧*长度)
@Jarod42我不确定我是否明白为什么在前面使用calloc
不可取。这会不会导致foo
被malloc
ed,然后realloc
ed,因为字符串必须始终包含至少1个char
(终止空值)。仍然存在小字符串优化,所以我希望reserve
只能分配一个。
string foo;
const auto bar = 13U;
generate_n(back_inserter(foo), bar * 3U, [](){
static const char multiplicand[] = "0, ";
static const auto length = strlen(multiplicand);
static auto i = 0U;
return multiplicand[i++ % length];});
string foo;
const auto bar = 13U;
const char multiplicand[] = "0, ";
const auto length = strlen(multiplicand);
generate_n(back_inserter(foo), bar * length, [&](){
static auto i = 0U;
return multiplicand[i++ % length];
});
const auto bar = 13U;
const char multiplicand[] = "0, ";
const auto length = strlen(multiplicand);
string foo(bar * length, '\0');
generate_n(foo.begin(), bar * length, [&](){
static auto i = 0U;
return multiplicand[i++ % length];
});