const char* 连接

发布于 2024-08-16 06:53:18 字数 188 浏览 3 评论 0原文

我需要连接两个 const 字符,如下所示:

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

我该怎么做?

我从具有 C 接口的第三方库传递了这些 char* ,因此我不能简单地使用 std::string 来代替。

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::string instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

狠疯拽 2024-08-23 06:53:18

在您的示例中,onetwo 是 char 指针,指向 char 常量。您无法更改这些指针指向的 char 常量。所以像这样的事情

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

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

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.

In your example one and two are 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.
深海里的那抹蓝 2024-08-23 06:53:18

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

The C way:

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

The C++ way:

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);
段念尘 2024-08-23 06:53:18

如果您使用 C++,为什么不使用 std::string 而不是 C 样式字符串呢?

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

std::string three= one+two;

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

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

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()

与风相奔跑 2024-08-23 06:53:18

使用 std::string :

#include <string>

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

Using std::string:

#include <string>

std::string result = std::string(one) + std::string(two);
掩于岁月 2024-08-23 06:53:18
const char *one = "Hello ";
const char *two = "World";

string total( string(one) + two );

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

更新:已更改
字符串总计 = 字符串(一)+ 字符串(二);
出于性能原因,改为 string total( string(one) + Two ); (避免构造字符串二和临时字符串总计)

// string total(move(move(string(one)) + two));  // even faster?
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(move(move(string(one)) + two));  // even faster?
柏林苍穹下 2024-08-23 06:53:18

再举一个例子:

// 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 可能更安全。

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::string is probably safer.

抱猫软卧 2024-08-23 06:53:18

看起来您正在将 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;

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

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

const char *a = "hello "; 
const char *b = "world"; 
std::string c = a; 
std::string d = b; 
cout << c + d;
若沐 2024-08-23 06:53:18

首先,您必须创建一些动态内存空间。然后你可以将两个字符串 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);

新 C++ 方式

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

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:

  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

  std::string three(one);
  three += two;
瞳孔里扚悲伤 2024-08-23 06:53:18

如果您不知道字符串的大小,可以这样做:

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

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;
}
温折酒 2024-08-23 06:53:18

您可以使用strstream。它已被正式弃用,但我认为,如果您需要使用 C 字符串,它仍然是一个很棒的工具。

char result[100]; // max size 100
std::ostrstream s(result, sizeof result - 1);

s << one << two << std::ends;
result[99] = '\0';

这会将 onetwo 写入流中,并使用 std::ends 附加终止 \0 。万一两个字符串最终都可以精确地写入 99 个字符 - 因此不会留下空格来写入 \0 - 我们在最后一个位置手动写入一个。

You can use strstream. It's formally deprecated, but it's still a great tool if you need to work with C strings, i think.

char result[100]; // max size 100
std::ostrstream s(result, sizeof result - 1);

s << one << two << std::ends;
result[99] = '\0';

This will write one and then two into the stream, and append a terminating \0 using std::ends. In case both strings could end up writing exactly 99 characters - so no space would be left writing \0 - we write one manually at the last position.

老娘不死你永远是小三 2024-08-23 06:53:18
const char* one = "one";
const char* two = "two";
char result[40];
sprintf(result, "%s%s", one, two);
const char* one = "one";
const char* two = "two";
char result[40];
sprintf(result, "%s%s", one, two);
酒与心事 2024-08-23 06:53:18

在动态分配内存时不使用strcpy命令连接两个常量char指针:

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;

Connecting two constant char pointer without using strcpy command in the dynamic allocation of memory:

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;
哭泣的笑容 2024-08-23 06:53:18

有一个 C 风格的技巧可以在构建时实现 (const/constexpr) 字符串文字连接。这应该不重要,因为 const char* 实际上是 C 风格的东西。并且没有动态分配、strlen 以及这里发布的所有丑陋的东西。一切都是由编译器完成的,执行期间什么也不做。

诀窍是使用预处理器 #define 指令。

#define PREFIX "/dev/"
#define DEVICE PREFIX "pts/3"

constexpr const char* defaultDevice = DEVICE;

这应该将 /dev/pts/3 分配给变量(2 个预处理器宏之间没有任何空格)。

There is a C-style trick for achieving the (const/constexpr) string literals concatenation in build time. It should not matter because const char* are actually C-style stuff anyhow. And without dynamic allocations, strlen, and all the ugly stuff posted here. Everything is done by the compiler, nothing during execution.

The trick is to use the preprecessor #define directive.

#define PREFIX "/dev/"
#define DEVICE PREFIX "pts/3"

constexpr const char* defaultDevice = DEVICE;

This should assign /dev/pts/3 to the variable (without any blank between the 2 preprocessor macros).

望她远 2024-08-23 06:53:18
std::string concat_cpp98(const char* left, const char* right)
{
    const size_t left_len = strlen(left);
    const size_t right_len = strlen(right);

    std::string result;
    result.reserve(left_len + right_len);
    result.append(left, left_len);
    result.append(right, right_len);
    return result;
}

std::string concat_cpp17(std::string_view left, std::string_view right)
{
    std::string result;
    result.reserve(left.size() + right.size());
    result.append(left);
    result.append(right);
    return result;
}

int main()
{
    const char* one = "Hello ";
    const char* two = "World";
    std::cout << concat_cpp98(one, two) << '\n'
              << concat_cpp17(one, two);

    return 0;
}
std::string concat_cpp98(const char* left, const char* right)
{
    const size_t left_len = strlen(left);
    const size_t right_len = strlen(right);

    std::string result;
    result.reserve(left_len + right_len);
    result.append(left, left_len);
    result.append(right, right_len);
    return result;
}

std::string concat_cpp17(std::string_view left, std::string_view right)
{
    std::string result;
    result.reserve(left.size() + right.size());
    result.append(left);
    result.append(right);
    return result;
}

int main()
{
    const char* one = "Hello ";
    const char* two = "World";
    std::cout << concat_cpp98(one, two) << '\n'
              << concat_cpp17(one, two);

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文