2017-07-26 75 views
0

我有一个叫做“tileManager”的对象,我想做一些让我可以用[0] [1] [0] [2] ..... [1] [0]等将数组插入向量中<vector<int>>

里面的那个对象我有一个std::vector<std::vector<int> >为了得到一个多维向量。

这是目前我的代码,我想知道如何插入的阵列成多维向量

代码:

void tileManager::initTileVec() { 
    int checkWidth = 0; 
    int checkHeight = 0; 
    int row = 0; 
    int column = 0; 
    int pixels = (GetSystemMetrics(SM_CXSCREEN) - GetSystemMetrics(SM_CYSCREEN))/3; 
    for (int i = 0; i < 8; i++) { 
     for (int j = 0; j < 8; j++) { 
      tileVec[column][row] = [checkHeight , checkWidth]; 
      row += 1; 
     } 
     column += 1; 
    } 
} 
+0

你不能。类型必须匹配。在'vector >'中使用'std :: pair ',否则使用类型,直到它们匹配。 – AndyG

+1

我也建议你看看[逗号运算符](http://en.cppreference.com/w/c/language/operator_other#Comma_o​​perator)。因为'tileVec [column,row]'可能不会像你期望的那样工作(我认为)。 –

+0

是啊是的,我只是修复它,并把它变成tileVec [专栏] [行]我的坏 – greuzr

回答

2

下面是如何推回阵列的例子成向量向量:

#include <iostream> 
#include <vector> 
int main(){ 
    int arr[] = { 1, 2, 3, 4 }; 
    int arr2[] = { 5, 6, 7, 8 }; 
    std::vector<std::vector<int>> v; 
    v.push_back(std::vector<int>(arr, arr + 4)); 
    v.push_back(std::vector<int>(arr2, arr2 + 4)); 
    for (size_t i = 0; i < v.size(); i++){ 
     for (size_t j = 0; j < v[i].size(); j++){ 
      std::cout << v[i][j] << ' '; 
     } 
     std::cout << std::endl; 
    } 
} 
+0

谢谢!有效。 :) – greuzr

相关问题