Created
April 10, 2023 06:39
-
-
Save muaddib1971/0f1ab24cc8c3de00bab78c5321e89032 to your computer and use it in GitHub Desktop.
vector initialisation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
int main(int argc, char * argv[]) { | |
//check for the correct args | |
if(argc != 3) { | |
std::cerr << "error: invalid args" << std::endl; | |
return EXIT_FAILURE; | |
} | |
//extract widht and height from the args | |
//std::stoi reads in a integer from a string | |
std::size_t width = std::stoi(argv[1]); | |
std::size_t height = std::stoi(argv[2]); | |
//create the 2d vector | |
std::vector <std::vector<std::size_t>> myvec; | |
//resize the vector to the right height based on the arg | |
myvec.resize(height); | |
//initialise the vector | |
for(std::size_t count = 0; count < height; ++count){ | |
//resize each row | |
myvec[count].resize(width); | |
for(std::size_t inner = 0 ; inner < width; ++inner){ | |
//initialise the values | |
myvec[count][inner] = inner; | |
} | |
} | |
for (std::size_t outer = 0; outer < myvec.size(); ++outer){ | |
for(std::size_t inner = 0; inner < myvec[outer].size(); ++inner) { | |
std::cout << myvec[outer][inner] << ", "; | |
} | |
std::cout << "\n"; | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment