vector多维数组
时间: 2023-09-30 16:08:03 浏览: 112
要输入多维数组,您可以使用嵌套的循环结构。以下是一个示例代码,展示了如何输入一个二维向量(多维数组):
```c++
#include <iostream>
#include <vector>
int main() {
int rows, cols;
std::cout << "请输入二维向量的行数和列数:";
std::cin >> rows >> cols;
std::vector<std::vector<int>> matrix;
std::cout << "请逐个输入二维向量的元素:" << std::endl;
for (int i = 0; i < rows; ++i) {
std::vector<int> row;
for (int j = 0; j < cols; ++j) {
int element;
std::cin >> element;
row.push_back(element);
}
matrix.push_back(row);
}
std::cout << "输入的二维向量为:" << std::endl;
for (const auto& row : matrix) {
for (const auto& element : row) {
std::cout << element << ' ';
}
std::cout << std::endl;
}
return 0;
}
```
在上述代码中,首先输入了二维向量的行数和列数。然后使用嵌套的循环结构,逐个输入二维向量的元素,并将它们存储到一个二维 vector 中。最后,遍历输出这个二维向量。
请注意,这只是一个示例,您可以根据需要进行修改和扩展。
阅读全文
相关推荐
















