在C ++中,是否可以直接从另一个数组初始化数组? [英] In C++, is it possible to initialize an array directly from another?

查看:382
本文介绍了在C ++中,是否可以直接从另一个数组初始化数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,是否可以直接从另一个数组初始化内置数组?据我所知,一个人只能拥有一个数组,然后将每个元素从另一个数组复制/移动到另一个数组,这是某种分配,而不是初始化.

In C++, is it possible to initialize a built-in array directly from another? As far as I know, one can only have an array and then copy/move each element from another array to it, which is some kind of assignment, not initialization.

推荐答案

数组既没有复制构造函数,也没有复制赋值运算符.您只能将元素从一个数组复制到另一个元素.

Arrays have neither the copy constructor nor the copy assignment operator. You can only copy elements from one array to another element by element.

字符数组可以由字符串文字初始化.或者可以使用标头<cstring>中声明的strcpystrncpymemcpy等标准C函数来复制字符串.

Character arrays can be initialized by string literals. Or strings can be copied using standard C functions like strcpy, strncpy, memcpy declared in header <cstring>.

对于其他数组,您可以使用例如标头<algorithm>中声明的标准算法std::copystd::copy_ifstd::transform.

For other arrays you can use for example standard algorithms std::copy, std::copy_if, std::transform declared in header <algorithm>.

否则,您可以使用标准容器std::arraystd::vector,这些容器可以将类型的一个对象分配给相同类型的另一个对象,或从另一个对象创建一个对象.

Otherwise you can use either standard container std::array or std::vector that allow to assign one object of the type to another object of the same type or create one object from another object.

这篇关于在C ++中,是否可以直接从另一个数组初始化数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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