c++ - 如何在此代码中将数字添加到我的数组中?
问题描述
我想制作一个包含所有数字的 3 维列表。示例:N=2 我的数组必须包含所有变体:
000
001
002
003
010
...
210
211
212
213
如何将数字添加到我的数组中?
#include <iostream>
using namespace std;
int main ()
{
int N;
cin >> N;
int array[N][2][4]={0};
for (int i=0; i<N; ++i)
{
for (int j=0; j<2; ++j)
{
for (int k=0; k<4 ; ++k)
{
cout << array [i][j][k]<< endl;
}
}
}
return 0;
}
解决方案
嗯,我很难理解你的问题。你只想动态分配内存?然后你可以使用该new
语句。
就像下面的例子:
#include <iostream>
#include <iomanip>
constexpr unsigned int DimensionZ = 4;
constexpr unsigned int DimensionY = 2;
using YZArray = int[DimensionY][DimensionZ];
int main() {
unsigned int dimensionX{};
if ((std::cin >> dimensionX) and (dimensionX > 0)) {
YZArray* xyzArray = new YZArray[dimensionX]{};
for (unsigned int x = 0; x < dimensionX; ++x)
for (unsigned int y = 0; y < DimensionY; ++y)
for (unsigned int z = 0; z < DimensionZ; ++z)
xyzArray[x][y][z] = 100 * x + 10 * y + z;
for (unsigned int x = 0; x < dimensionX; ++x)
for (unsigned int y = 0; y < DimensionY; ++y)
for (unsigned int z = 0; z < DimensionZ; ++z)
std::cout << std::right << std::setfill('0') << std::setw(3) << xyzArray[x][y][z] << '\n';
delete[] xyzArray;
}
}
请在上面的示例中输入 3,然后您将获得您的序列。
但是,因为答案很简单,所以我想这是错误的。我的猜测是你想要 3 个并行的一维数组,然后构建它们的组合。请更清楚地说明。
推荐阅读
- macos - NSButton:蓝色背景
- c++ - 我的 mat4 翻译功能无法正常工作
- r - 将单个列中的相同值分组,同时将数据保留在
- node.js - 通过 cpanel 上的应用程序管理器运行时 socket.io 上的错误?
- .net - 在 OracleBulkCopy 中打开 excel 文件时出现读取错误
- php - 在 foreach 循环中创建变量
- mysql - 如何将由两个表的内部连接产生的唯一 ID 计数除以其中一个表中的唯一 ID 计数
- javascript - 如何从对象数组中获取最高 3 值 - ES6/JS
- nginx - 如何在 minikube 中直接访问 ingress-nginx
- c++ - 候选函数不可行:对于第一个参数 void getData(Record listofRecords, int & listSize),没有已知的从“记录 [10]”到“记录”的转换;