multidimensional vector
Hi
How to create a multidimensional vector in C++,
and assign all the elements to default values of zero?
How to create a multidimensional vector in C++, |
std::vector<std::vector<int>>
and assign all the elements to default values of zero? |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <vector>
#include <iostream>
int main()
{
std::vector<std::vector<int>> arr {{0, 0, 0, 0}, {0, 0, 0, 0},
{0, 0, 0, 0}, {0, 0, 0, 0}};
for(const auto& v: arr) {
for(const auto i: v)
std::cout << i << ' ';
std::cout << '\n';
}
}
|
or:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <vector>
#include <iostream>
int main()
{
std::vector<std::vector<int>> arr(4, std::vector<int>(4)) ;
for(const auto& v: arr) {
for(const auto i: v)
std::cout << i << ' ';
std::cout << '\n';
}
}
|
Topic archived. No new replies allowed.