gpt4 book ai didi

c++ - C++中二维 vector 的列大小和行大小

转载 作者:IT老高 更新时间:2023-10-28 21:51:37 32 4
gpt4 key购买 nike

我有一个这样的 vector :

vector< vector<int> > myVector;

此 vector 中的所有行号和列号都相同。

我想找出这个 vector 的行数和列数。

我想出的行数:

myVector[0].size();

对于列数,我想不出任何办法。你能告诉我我的行数是否正确,你能告诉我如何获得列数吗?谢谢。

最佳答案

你有一个整数 vector 的 vector myVector[0].size() 返回二维 vector 中第一个 int vector 中元素的数量。

这种 vector 的结构是这样的:

myVector[
Vector[0, 4, 2, 5],
Vector[1, 4, 2]
];

当您调用 myVector[1].size() 时,它将返回 3,而 [0] 将返回 4。

对于二维 vector 中的行数(int vector ),您可以使用myVector.size()

您可以运行它以在操作中查看它

#include <iostream>
#include <vector>

int main(){
std::vector<std::vector<int>>MyVector;
std::vector<int>temp;

temp.push_back(1);
temp.push_back(2);
temp.push_back(3);
MyVector.push_back(temp);

std::cout << "Rows in the 2d vector: " << MyVector.size() <<
std::endl << "Collumns in the 1st row: " << MyVector[0].size() <<
std::endl;

system("pause");
return 0;
}

这是输出:

Rows in the 2d vector: 1
Collumns in the 1st row: 3

关于c++ - C++中二维 vector 的列大小和行大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27340220/

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