greuzr greuzr - 1 year ago 44
C++ Question

inserting an array into a vector<vector<int>>

I have an object called "tileManager"
and I wanted to make something that will allow me to set the position of game objects using [0][1] [0][2]..... [1][0] etc.

inside that object I have an

std::vector<std::vector<int> >
in order to get a multidimentional vector.

this is currently the code I have, I was wondering how do i insert an array into a multi dimentional vector

code:

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;
}
}

Ron Ron
Answer Source

Here is an example of how to push back the array into a vector of vectors:

#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;
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download