gpt4 book ai didi

c++ - 如何将多个数组作为 C++ 中的输入?

转载 作者:行者123 更新时间:2023-11-28 01:52:04 26 4
gpt4 key购买 nike

我一周前开始学习 C++,来自 C。

我的输入格式如下:

7
13 -4 -10 4 9 7 -3
4
0 -2 -1 2
2
3 5
0

第一个数字给出了第一个数组中元素的数量。一旦这个数字为零,我们就停止扫描数组。

我想将这些数组扫描成一个数组数组,如下所示:

[[13,-4,-10,4,9,7,-3] , [0,-2,-1,2] , [3,5]]

我知道如何扫描第一个数组:

int n;
int array1[MAXLENGTH];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> array1[i];
// scanf("%d", &array1[i]);
}

我卡在 0 -2 -1 2 上,因为它从零开始。

如何扫描这些数组并在遇到最后一个零时停止?

最佳答案

无论数据如何,输入模式都是相同的:

unsigned int array_index = 0U;
unsigned int quantity;
std::vector<std::vector<int> > database;
while (cin >> quantity)
{
if (quantity == 0U)
{
break;
}
int value = 0;
for (unsigned int i = 0; i < quantity; ++i)
{
cin >> value;
database[array_index].push_back(value);
}
++array_index;
}

vector 的 vector 应该能够包含数据。

输入数据线:

4
0 -2 -1 2

4 表示第二组数字的数量。0是第二组数据的第一个数据。

您的输入文件中有 3 组数据。

关于c++ - 如何将多个数组作为 C++ 中的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42630687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com