c++ - cpp 中是否存在一种数据结构可以轻松地提供一种基于已存在的实例构建新数据结构的方法?

标签 c++ arrays vector data-structures

我正在寻找的操作类型如下,但当然这段代码不起作用。

int a[3] = {0,1,2};
int b[3] = (a[2] = 0);

这样数组 b 有元素 {0,1,0} 和 a 有 {0,1,2}

最佳答案

Does there exist a data structure in cpp that easily provides a way to build a new one, based on an instance that already exists?

几乎所有标准容器都是可复制的。您可以使用 std::array 实现您的示例:

std::array a{0,1,2};
std::array b = a;
b[2] = 0;

关于c++ - cpp 中是否存在一种数据结构可以轻松地提供一种基于已存在的实例构建新数据结构的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572022/

相关文章:

c++ - 为什么 Microsoft Visual Studio 找不到 <stdint.h>?

查找模式的 C 函数始终返回 "no mode"

java - Math.random 到一个数组中,然后将数组打印成 10 行,每行 10

c++ - 读取文件时如何忽略无效输入?

c++ - 将 vector 拆分为多个数组/vector C++

c++ - 为什么我看到在堆和堆栈上分配的数组之间存在不同的行为?

c++ - 定义默认的 int 为 unsigned int

java - 无法将变量添加到我的数组

matlab - 在 MATLAB 中生成向量

C++:在 Windows 上使用 C++ 读写 BMP 文件的最简单方法是什么?