追加到 C++ 中的 Char 数组的末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9955236/
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
Append to the end of a Char array in C++
提问by Taylor Huston
Is there a command that can append one array of char onto another? Something that would theoretically work like this:
是否有可以将一个字符数组附加到另一个字符数组的命令?理论上可以这样工作的东西:
//array1 has already been set to "The dog jumps "
//array2 has already been set to "over the log"
append(array2,array1);
cout << array1;
//would output "The dog jumps over the log";
This is a pretty easy function to make I would think, I am just surprised there isn't a built in command for it.
这是一个非常简单的功能,我想,我只是惊讶没有内置命令。
*Edit
*编辑
I should have been more clear, I didn't mean changing the size of the array. If array1 was set to 50 characters, but was only using 10 of them, you would still have 40 characters to work with. I was thinking an automatic command that would essentially do:
我应该更清楚,我不是说改变数组的大小。如果将 array1 设置为 50 个字符,但只使用了其中的 10 个字符,那么您仍然可以使用 40 个字符。我在想一个基本上可以做的自动命令:
//assuming array1 has 10 characters but was declared with 25 and array2 has 5 characters
int i=10;
int z=0;
do{
array1[i] = array2[z];
++i;
++z;
}while(array[z] != '#include <cstring>
#include <iostream>
int main()
{
char array1[] ="The dog jumps ";
char array2[] = "over the log";
char * newArray = new char[std::strlen(array1)+std::strlen(array2)+1];
std::strcpy(newArray,array1);
std::strcat(newArray,array2);
std::cout << newArray << std::endl;
delete [] newArray;
return 0;
}
');
I am pretty sure that syntax would work, or something similar.
我很确定语法会起作用,或者类似的东西。
回答by KillianDS
If you are not allowed to use C++'s string class (which is terrible teaching C++ imho), a raw, safe array version would look something like this.
如果不允许您使用 C++ 的字符串类(恕我直言,这对 C++ 的教学很糟糕),原始的、安全的数组版本将如下所示。
#include <cstring>
#include <iostream>
int main()
{
const unsigned BUFFER_SIZE = 50;
char array1[BUFFER_SIZE];
std::strncpy(array1, "The dog jumps ", BUFFER_SIZE-1); //-1 for null-termination
char array2[] = "over the log";
std::strncat(array1,array2,BUFFER_SIZE-strlen(array1)-1); //-1 for null-termination
std::cout << array1 << std::endl;
return 0;
}
This assures you have enough space in the array you're doing the concatenation to, without assuming some predefined MAX_SIZE
. The only requirement is that your strings are null-terminated, which is usually the case unless you're doing some weird fixed-size string hacking.
这可确保您在进行连接的数组中有足够的空间,而无需假设一些预定义的MAX_SIZE
. 唯一的要求是您的字符串以空字符结尾,除非您正在执行一些奇怪的固定大小字符串黑客攻击,否则通常是这种情况。
Edit, a safe version with the "enough buffer space" assumption:
编辑,一个具有“足够缓冲空间”假设的安全版本:
char array1[BIG_ENOUGH];
char array2[X];
/* ...... */
/* check array bounds */
/* ...... */
strcat(array1, array2);
回答by Alok Save
If your arrays are character arrays(which seems to be the case), You need a strcat().
Your destination array should have enough space to accommodate the appended data though.
如果您的数组是字符数组(似乎是这种情况),您需要一个strcat()。
不过,您的目标数组应该有足够的空间来容纳附加的数据。
In C++, You are much better off using std::stringand then you can use std::string::append()
在 C++ 中,最好使用std::string,然后可以使用std::string::append()
回答by masoud
You should have enough space for array1
array and use something like strcat
to contact array1
to array2
:
您应该有足够的空间用于array1
数组并使用类似strcat
联系array1
方式array2
:
std::string first ("The dog jumps ");
std::string second ("over the log");
std::cout << first + second << std::endl;
回答by Luchian Grigore
There's no built-in command for that because it's illegal. You can't modify the size of an array once declared.
没有内置命令,因为它是非法的。一旦声明,您就不能修改数组的大小。
What you're looking for is either std::vector
to simulate a dynamic array, or better yet a std::string
.
您正在寻找的是std::vector
模拟动态数组,或者更好的是std::string
.