gpt4 book ai didi

c++ - 修改自制的 concat 函数,使其接受两个以上的参数

转载 作者:太空狗 更新时间:2023-10-29 20:00:02 30 4
gpt4 key购买 nike

我编写了一个自制的 concat 函数:

char * concat (char * str1, char * str2) {
for (int i=0; i<BUFSIZ; i++) {
if (str1[i]=='\0') {
for (int j=i; j<BUFSIZ; j++) {
if (str2[j-i]=='\0') return str1;
else str1[j]=str2[j-i];
}
}
}
}

现在如果我想连接 2 个以上的字符串,即 buf temp1 temp2,我必须使用类似的东西:

strcpy(buf, concat(concat(buf,temp1),temp2));

请告诉我,有没有一种简单的方法可以修改我的函数,使其接受多个参数?

最佳答案

在 C++ 中使用字符串代替 char* 和函数:std::string result = std::string(buf) + temp1 + temp2;

关于c++ - 修改自制的 concat 函数,使其接受两个以上的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9214180/

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