附加到C ++中Char数组的末尾 [英] Append to the end of a Char array in C++

查看:139
本文介绍了附加到C ++中Char数组的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在可以将一个char数组追加到另一个char数组的命令?从理论上讲,这样的事情会发生:

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

我想这是一个非常简单的功能,我很惊讶没有内置命令.

* 编辑

我应该更加清楚,我并不是说要更改数组的大小.如果将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] != '\0');

我非常确定语法会起作用,或者类似的用法.

解决方案

如果不允许您使用C ++的字符串类(这对C ++ imho来说是很糟糕的),那么原始的,安全的数组版本将看起来像这样.

>

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

这可确保您要进行串联的数组中有足够的空间,而无需假定一些预定义的MAX_SIZE.唯一的要求是您的字符串必须以null终止,这是通常的情况,除非您要进行一些奇怪的固定大小的字符串黑客操作.

编辑,假设缓冲区空间足够大"的安全版本:

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

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:

//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] != '\0');

I am pretty sure that syntax would work, or something similar.

解决方案

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.

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

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.

Edit, a safe version with the "enough buffer space" assumption:

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

这篇关于附加到C ++中Char数组的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆