Двухмерный Vector C++

Задача: создать двухмерный вектор 3 на 5 и заполнить его случайными числами от 0 до 9

#include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<vector<int>> vec; for (int i = 0; i < 3; i++) { vector <int> temp; for (int j = 0; j < 5; j++) { temp.push_back(rand()%10); } } cout << endl; for (int vecline = 0; vecline < 3; vecline++) { for (int vecelem = 0; vecelem < 5; vecelem++) { cout << vec[vecline][vecelem] << " "; } cout << endl; } return 0; }

После запуска программы результат будет таким:

1 7 4 0 9 4 8 8 2 4 5 5 1 7 1 C:\Users\ao\source\repos\vector_02.exe (process 145912) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .

Видео

В видео ниже есть всё, что описано в статье кроме более быстрого перебора элеметов вектора.

Share in social media: