const char *连接
我需要连接两个常量字符,如下所示:
const char *one = "Hello "; const char *two = "World"; 我该怎么做呢?
 我从一个C接口的第三方库传递这些char* s,所以我不能简单地使用std::string来代替。 
在你的例子中, 一个和两个是char指针,指向char常量。 你不能改变这些指针指向的字符常量。 所以就像
 strcat(one,two); // append string two to string one. 
不pipe用。 相反,你应该有一个单独的variables(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. 
C方式:
 char buf[100]; strcpy(buf, one); strcat(buf, two); 
C ++方式:
 std::string buf(one); buf.append(two); 
编译时的方式:
 #define one "hello " #define two "world" #define concat(first, second) first second const char* buf = concat(one, two); 
 如果您使用C ++,为什么不使用std::string而不是C风格的string? 
 std::string one="Hello"; std::string two="World"; std::string three= one+two; 
 如果您需要将此string传递给C函数,只需传递three.c_str() 
 使用std::string : 
 #include <string> std::string result = std::string(one) + std::string(two); 
  更新:改变的string total = string(one) + string(two);  string total( string(one) + two ); 性能方面的原因(避免构buildstring2和临时string总数) 
 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() 
再举一个例子:
 // 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; 
 但除非你不想或不能使用C ++标准库,否则使用std::string可能更安全。 
 看起来好像你在C库中使用C ++,因此你需要使用const char * 。 
 我build议把这些const char *包装成std::string : 
 const char *a = "hello "; const char *b = "world"; std::string c = a; std::string d = b; cout << c + d; 
首先,你必须创build一些dynamic的内存空间。 然后,你可以把这两个弦放入它。 或者你可以使用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); 
新的C ++方式
  std::string three(one); three += two; 
 你可以使用strstream 。 这是正式的弃用,但它仍然是一个伟大的工具,如果你需要使用Cstring,我想。 
 char result[100]; // max size 100 std::ostrstream s(result, sizeof result - 1); s << one << two << std::ends; result[99] = '\0'; 
 这将写入one ,然后two到stream中,并附加一个终止\0使用std::ends 。 如果两个string都可能最终写入99字符 – 所以没有空间留下写入\0 – 我们在最后一个位置手动写入一个字符。 
 const char* one = "one"; const char* two = "two"; char result[40]; sprintf(result, "%s%s", one, two); 
如果你不知道string的大小,你可以这样做:
 #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; } 
在dynamic分配内存时不使用strcpy命令连接两个常量字符指针:
 const char* one = "Hello "; const char* two = "World!"; char* three = new char[strlen(one) + strlen(two) + 1] {'\0'}; strcat_s(three, strlen(one) + 1, one); strcat_s(three, strlen(one) + strlen(two) + 1, two); cout << three << endl; delete[] three; three = nullptr;