How to create 2D vector in C++

1 Answer

0 votes
#include <iostream>
#include <iomanip>
#include <vector>

constexpr int ROW = 3;
constexpr int COL = 4;

int main()
{
    std::vector<std::vector<int>> v2d(ROW, std::vector<int> (COL, 0));

    srand(time(NULL));
    for (auto &item : v2d) {
        for (auto &item1 : item) {
            item1 = rand() % 100;
            std::cout << std::setw(2) << item1 << " ";
        }
        std::cout << "\n";
    }
 
    return 0;
}
     
     
     
     
/*
run:
   
17 10 56 86 
86 80 58  2 
65 56  6 17 
      
*/

 



answered May 12, 2021 by avibootz

Related questions

2 answers 212 views
4 answers 131 views
131 views asked Oct 23, 2025 by avibootz
1 answer 124 views
2 answers 131 views
1 answer 109 views
...