Ant*_*ell 108 c c++ string-concatenation
我需要连接两个这样的const字符:
const char *one = "Hello ";
const char *two = "World";
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
我char*从带有C接口的第三方库中传递了这些,所以我不能简单地使用它std::string.
cod*_*ict 102
在您的示例中,一个和两个是char指针,指向char常量.您无法更改这些指针指向的char常量.所以像:
strcat(one,two); // append string two to string one.
Run Code Online (Sandbox Code Playgroud)
不管用.相反,你应该有一个单独的变量(char数组)来保存结果.像这样的东西:
char result[100]; // array to hold the result.
strcpy(result,one); // copy string one into the result.
strcat(result,two); // append string two to the result.
Run Code Online (Sandbox Code Playgroud)
Ida*_*n K 74
C方式:
char buf[100];
strcpy(buf, one);
strcat(buf, two);
Run Code Online (Sandbox Code Playgroud)
C++方式:
std::string buf(one);
buf.append(two);
Run Code Online (Sandbox Code Playgroud)
编译时方式:
#define one "hello "
#define two "world"
#define concat(first, second) first second
const char* buf = concat(one, two);
Run Code Online (Sandbox Code Playgroud)
Pra*_*rav 31
如果您使用的是C++,为什么不使用std::stringC风格的字符串呢?
std::string one="Hello";
std::string two="World";
std::string three= one+two;
Run Code Online (Sandbox Code Playgroud)
如果需要将此字符串传递给C函数,只需传递即可 three.c_str()
Gre*_*osz 20
使用std::string:
#include <string>
std::string result = std::string(one) + std::string(two);
Run Code Online (Sandbox Code Playgroud)
Ped*_*eis 16
更新:改
string total = string(one) + string(two);
到string total( string(one) + two );性能方面的原因(避免串两个临时串共建设)
const char *one = "Hello ";
const char *two = "World";
string total( string(one) + two ); // OR: string total = string(one) + string(two);
// string total(move(move(string(one)) + two)); // even faster?
// to use the concatenation in const char* use
total.c_str()
Run Code Online (Sandbox Code Playgroud)
还有一个例子:
// calculate the required buffer size (also accounting for the null terminator):
int bufferSize = strlen(one) + strlen(two) + 1;
// allocate enough memory for the concatenated string:
char* concatString = new char[ bufferSize ];
// copy strings one and two over to the new buffer:
strcpy( concatString, one );
strcat( concatString, two );
...
// delete buffer:
delete[] concatString;
Run Code Online (Sandbox Code Playgroud)
但除非您特别不想或不能使用C++标准库,否则使用std::string可能更安全.
首先,您必须创建一些动态内存空间.然后你可以将两个字符串strcat到它中.或者您可以使用c ++"string"类.老派的C方式:
char* catString = malloc(strlen(one)+strlen(two)+1);
strcpy(catString, one);
strcat(catString, two);
// use the string then delete it when you're done.
free(catString);
Run Code Online (Sandbox Code Playgroud)
新的C++方式
std::string three(one);
three += two;
Run Code Online (Sandbox Code Playgroud)
看起来你正在使用带有C库的C++,因此你需要使用它const char *.
我建议把它们包装const char *成std::string:
const char *a = "hello ";
const char *b = "world";
std::string c = a;
std::string d = b;
cout << c + d;
Run Code Online (Sandbox Code Playgroud)
如果您不知道字符串的大小,则可以执行以下操作:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
const char* q1 = "First String";
const char* q2 = " Second String";
char * qq = (char*) malloc((strlen(q1)+ strlen(q2))*sizeof(char));
strcpy(qq,q1);
strcat(qq,q2);
printf("%s\n",qq);
return 0;
}
Run Code Online (Sandbox Code Playgroud)