高效地将一个字符串插入另一个字符串

发布于 2024-10-03 15:42:33 字数 127 浏览 0 评论 0原文

我有

char aa[] = {“你好,!” };

char bb[] = { "World" };

如何使用 cstring 最有效地将 bb 插入到 aa 中?

I have

char aa[] = { "Hello, !" };

char bb[] = { "World" };

How to insert bb into aa the most efficiently with cstring ?

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

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

发布评论

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

评论(4

执妄 2024-10-10 15:42:34

char *newStr = malloc(strlen(aa) + strlen(bb));

sprintf(newStr, "%s%s", aa, bb);

char *newStr = malloc(strlen(aa) + strlen(bb));

sprintf(newStr, "%s%s", aa, bb);

江南月 2024-10-10 15:42:33
  • 分配足够大的缓冲区 (malloc/new[])
  • 将 aa 字符串的第一部分复制到缓冲区 (strncpy/memcpy
  • 复制 bb 字符串(strcpy/memcpy
  • 复制 aa 字符串的其余部分(strncpy/memcpy< /代码>)
  • allocate big enough buffer (malloc/new[])
  • copy the first part of the aa string into the buffer (strncpy/memcpy)
  • copy the bb string (strcpy/memcpy)
  • copy the rest of the aa string (strncpy/memcpy)
苯莒 2024-10-10 15:42:33

最大程度的优化通常需要利用特定的要求。您可以使代码变得不那么通用,以提高速度(或其他一些指标)。你可能知道一些可以捷径的地方,而我们必须猜测。

下面的代码片段使用这些标头:

#include <stdio.h>
#include <string.h>

如果您想最大限度地提高性能,它将有助于为最终字符串预先分配足够的空间,插入后:

#define MAX_STR_LEN 256 

char aa[MAX_STR_LEN] = "Hello, !";
char bb[] = "World";

您不指示如何知道在哪里插入。对于我的示例代码,我将插入点定义为目标字符串的偏移量:

size_t pos = 7;

如果您提前知道任一字符串长度,则应该使用 const 而不是调用 strlen,后者必须扫描字符串。我不认为你有那么幸运。

size_t srclen = strlen(bb);

memmove 函数正确处理重叠的源和目标:

//  +1 for count to include terminating null
memmove(aa+pos+srclen, aa+pos, strlen(aa)-pos+1);

memcpy 在某些平台上速度更快,并且可以安全地用于在字符串之间进行复制:

memcpy(aa+pos, bb, srclen);

现在 aa 包含结果。

如果您无法将 aa 预先分配到所需的大小,则:

// additional include for malloc
#include <stdlib.h>

char aa[] = "Hello, !";
char bb[] = "World";
size_t pos = 7;    

size_t srclen = strlen(bb);
size_t dstlen = strlen(aa);
char *m = malloc(srclen+dstlen+1);
// Test for NULL or use an allocator that throws

memcpy(m, aa, pos);
memcpy(m+pos, bb, srclen);
memcpy(m+pos+srclen, aa+pos, dstlen-pos+1);

m 包含结果,并且最终可能需要释放

请记住,如果有可能发生缓冲区溢出,您必须检查长度以避免内存损坏和可能的安全漏洞。

我希望这有帮助。

Maximum optimization usually requires taking advantage of the specific requirements. You make the code less general to gain speed (or some other metric). You may know some corner you can cut, while we have to guess.

Code fragments below use these headers:

#include <stdio.h>
#include <string.h>

If you want to maximize performance, it would help to pre-allocate enough space for the final string, post- insert:

#define MAX_STR_LEN 256 

char aa[MAX_STR_LEN] = "Hello, !";
char bb[] = "World";

You don’t indicate how you know where to insert. For my sample code I define the insert point as an offset into the destination string:

size_t pos = 7;

If you know either of the string lengths in advance you should use a const instead of calling strlen, which has to scan the string. Here I don’t’ assume you’re that lucky.

size_t srclen = strlen(bb);

The memmove function properly handles overlapping source and destination:

//  +1 for count to include terminating null
memmove(aa+pos+srclen, aa+pos, strlen(aa)-pos+1);

memcpy is faster on some platforms and can be safely used to copy between strings:

memcpy(aa+pos, bb, srclen);

Now aa contains the result.

If you can’t pre-allocate aa to the required size, then:

// additional include for malloc
#include <stdlib.h>

char aa[] = "Hello, !";
char bb[] = "World";
size_t pos = 7;    

size_t srclen = strlen(bb);
size_t dstlen = strlen(aa);
char *m = malloc(srclen+dstlen+1);
// Test for NULL or use an allocator that throws

memcpy(m, aa, pos);
memcpy(m+pos, bb, srclen);
memcpy(m+pos+srclen, aa+pos, dstlen-pos+1);

m contains the result and probably needs to be free'd eventually.

Remember if there is any chance for buffer overflow, you have to check lengths to avoid memory corruption and a possible security vulnerability.

I hope this helps.

玩物 2024-10-10 15:42:33
std::string aa = "Hello, !";
std::string bb = "World";
aa.insert(7, bb);

不想使用 C++ 中的字符串

那为什么你要把它标记为 C++ 呢?

std::string aa = "Hello, !";
std::string bb = "World";
aa.insert(7, bb);

dont wanna use string from C++

Then why are you tagging this as C++?

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