C++ const char* 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1995053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:47:37  来源:igfitidea点击:

const char* concatenation

c++cstring-concatenation

提问by Anthoni Caldwell

I need to concatenate two const chars like these:

我需要连接两个像这样的常量字符:

const char *one = "Hello ";
const char *two = "World";

How might I go about doing that?

我该怎么做呢?

I am passed these char*s from a third-party library with a C interface so I can't simply use std::stringinstead.

char*从带有 C 接口的第三方库中传递了这些s,所以我不能简单地使用std::string

采纳答案by codaddict

In your example oneand twoare char pointers, pointing to char constants. You cannot change the char constants pointed to by these pointers. So anything like:

在您的示例中,是字符指针,指向字符常量。您不能更改这些指针指向的字符常量。所以像:

strcat(one,two); // append string two to string one.

will not work. Instead you should have a separate variable(char array) to hold the result. Something like this:

不管用。相反,您应该有一个单独的变量(字符数组)来保存结果。像这样的东西:

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.

回答by Idan K

The C way:

C方式:

char buf[100];
strcpy(buf, one);
strcat(buf, two);

The C++ way:

C++方式:

std::string buf(one);
buf.append(two);

The compile-time way:

编译时方式:

#define one "hello "
#define two "world"
#define concat(first, second) first second

const char* buf = concat(one, two);

回答by Prasoon Saurav

If you are using C++, why don't you use std::stringinstead of C-style strings?

如果您使用的是 C++,为什么不使用std::stringC 风格的字符串来代替?

std::string one="Hello";
std::string two="World";

std::string three= one+two;

If you need to pass this string to a C-function, simply pass three.c_str()

如果您需要将此字符串传递给 C 函数,只需传递 three.c_str()

回答by Gregory Pakosz

Using std::string:

使用std::string

#include <string>

std::string result = std::string(one) + std::string(two);

回答by Pedro Reis

const char *one = "Hello ";
const char *two = "World";

string total( string(one) + two );

// to use the concatenation as const char*, use:
total.c_str()

Updated:changed string total = string(one) + string(two);to string total( string(one) + two );for performance reasons (avoids construction of string two and temporary string total)

更新:string total = string(one) + string(two);string total( string(one) + two );性能方面的原因(避免串两个临时串共建设)

// string total(move(move(string(one)) + two));  // even faster?

回答by stakx - no longer contributing

One more example:

再举一个例子:

// 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;

But unless you specifically don't want or can't use the C++ standard library, using std::stringis probably safer.

但是除非您特别不想或不能使用 C++ 标准库,否则使用std::string可能更安全。

回答by Luca Matteis

It seems like you're using C++ with a C library and therefore you need to work with const char *.

似乎您正在将 C++ 与 C 库一起使用,因此您需要使用const char *.

I suggest wrapping those const char *into std::string:

我建议将它们包装const char *std::string

const char *a = "hello "; 
const char *b = "world"; 
std::string c = a; 
std::string d = b; 
cout << c + d;

回答by Paul Tomblin

First of all, you have to create some dynamic memory space. Then you can just strcat the two strings into it. Or you can use the c++ "string" class. The old-school C way:

首先,您必须创建一些动态内存空间。然后你可以将两个字符串 strcat 放入其中。或者您可以使用 C++“字符串”类。老派的 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);

New C++ way

新的 C++ 方式

  std::string three(one);
  three += two;

回答by Iraklis Moutidis

If you don't know the size of the strings, you can do something like this:

如果您不知道字符串的大小,您可以执行以下操作:

#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;
}

回答by Jagannath

const char* one = "one";
const char* two = "two";
char result[40];
sprintf(result, "%s%s", one, two);