我制作了一个二维 vector :
std::vector<std::vector<int> *> hp;
我想初始化 hp vector 并从特定索引获取数据。
for eg,
Getting the values from hp[2][2];
请帮忙
最佳答案
尝试以下操作
#include <iostream>
#include <vector>
int main()
{
std::vector<std::vector<int> *> hp =
{
new std::vector<int> { 1, 2, 3 },
new std::vector<int> { 4, 5, 6 }
};
for ( std::vector<std::vector<int> *>::size_type i = 0;
i < hp.size(); i++ )
{
for ( std::vector<int>::size_type j = 0; j < hp[i]->size(); j++ )
{
std::cout << ( *hp[i] )[j] << ' ';
// std::cout << hp[i]->operator[]( j ) << ' ';
}
std::cout << std::endl;
}
for ( auto &v : hp ) delete v;
return 0;
}
对于内部循环中的注释和未注释的语句,程序输出将是相同的并且看起来像
1 2 3
4 5 6
关于c++ - 从 C++ 中的二维 vector 中的特定索引获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27440434/