c++ - 从 C++ 中的二维 vector 中的特定索引获取值

标签 c++ vector cocos2d-x cocos2d-x-3.0

我制作了一个二维 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/

相关文章:

android - cocos2d-X facebook 集成 c++

android-studio - 在 Android Studio 中打开 cocos2d-x-3.7

c++ - C++ 的 Makefile,包含一个 .o 文件和一个 .CPP 文件

C++ - 如何将用户的字符串输入到 vector 字符串中,然后基于它执行一些操作?

C++试图交换 vector 中的值

对元组向量进行排序需要第二个值的引用?

c++ - 缓冲区如何知道在刷新操作期间要从外部文件传输多少个字符?

c++ - 未找到 SDL_Vertex 和 SDL_RenderGeometry

c++ - Lotus Notes Automation:Notes.NotesSession 没有实现 Domino::ISession?

c++ - 在cocos2dx项目中使用flash动画