greuzr
greuzr

Reputation: 3

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

Upvotes: 0

Views: 121

Answers (2)

Ron
Ron

Reputation: 15521

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

Upvotes: 2

greuzr
greuzr

Reputation: 3

@Ron up there helped me, but I also watched a video that cleared everything up, if anybody else needs it cleared up go watch https://www.youtube.com/watch?v=lnqjNYd_hho

Upvotes: 0

Related Questions