How to declare two dimensional (2D) vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
  
int main ()
{
    std::vector<std::vector<int>> v {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12}};
    
    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v[0].size(); j++) {
            std::cout << v[i][j] << " ";
        }
        std::cout << "\n";
    }

    return 0;
}
   
   
   
   
/*
run:
 
1 2 3 4 
5 6 7 8 
9 10 11 12 
    
*/

 



answered Dec 7, 2020 by avibootz
edited Dec 7, 2020 by avibootz

Related questions

...