const char* 连接
我需要连接两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
在您的示例中,one 和 two 是 char 指针,指向 char 常量。您无法更改这些指针指向的 char 常量。所以像这样的事情
是行不通的。相反,您应该有一个单独的变量(字符数组)来保存结果。像这样的东西:
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:
will not work. Instead you should have a separate variable(char array) to hold the result. Something like this:
C 方式:
C++ 方式:
编译时方式:
The C way:
The C++ way:
The compile-time way:
如果您使用 C++,为什么不使用
std::string
而不是 C 样式字符串呢?如果您需要将此字符串传递给 C 函数,只需传递
two.c_str()
If you are using C++, why don't you use
std::string
instead of C-style strings?If you need to pass this string to a C-function, simply pass
three.c_str()
使用 std::string :
Using
std::string
:更新:已更改
字符串总计 = 字符串(一)+ 字符串(二);
出于性能原因,改为
string total( string(one) + Two );
(避免构造字符串二和临时字符串总计)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)再举一个例子:
但是除非您明确不想或不能使用 C++ 标准库,否则使用
std::string
可能更安全。One more example:
But unless you specifically don't want or can't use the C++ standard library, using
std::string
is probably safer.看起来您正在将 C++ 与 C 库一起使用,因此您需要使用
const char *
。我建议将这些
const char *
包装到std::string
中: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 *
intostd::string
:首先,您必须创建一些动态内存空间。然后你可以将两个字符串 strcat 进去。或者您可以使用 C++“字符串”类。老式 C 方式:
新 C++ 方式
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:
New C++ way
如果您不知道字符串的大小,可以这样做:
If you don't know the size of the strings, you can do something like this:
您可以使用
strstream
。它已被正式弃用,但我认为,如果您需要使用 C 字符串,它仍然是一个很棒的工具。这会将
one
和two
写入流中,并使用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.This will write
one
and thentwo
into the stream, and append a terminating\0
usingstd::ends
. In case both strings could end up writing exactly99
characters - so no space would be left writing\0
- we write one manually at the last position.在动态分配内存时不使用strcpy命令连接两个常量char指针:
Connecting two constant char pointer without using strcpy command in the dynamic allocation of memory:
有一个 C 风格的技巧可以在构建时实现 (const/constexpr) 字符串文字连接。这应该不重要,因为
const char*
实际上是 C 风格的东西。并且没有动态分配、strlen 以及这里发布的所有丑陋的东西。一切都是由编译器完成的,执行期间什么也不做。诀窍是使用预处理器
#define
指令。这应该将
/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.This should assign
/dev/pts/3
to the variable (without any blank between the 2 preprocessor macros).