- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
2D vector in C++ with user defined size
A vector of a vector is called 2D vector.
Algorithm
Begin Declare a variable v to the 2D vector type. Initialize values to the vector v. Print “the 2D vector is:”. for (int i = 0; i < v.size(); i++) for (int j = 0; j < v[i].size(); j++) print the value of 2D vector v[i][j]. End.
Example
#include <iostream> #include <vector> //header file for 2D vector in C++ using namespace std; int main() { vector<vector<int> > v{ { 4,5, 3, 10 }, // initializing 2D vector with values. { 2, 7, 11 }, { 3, 2, 1, 12 } }; cout<<"the 2D vector is:"<<endl; for (int i = 0; i < v.size(); i++) { // printing the 2D vector. for (int j = 0; j < v[i].size(); j++) cout << v[i][j] << " "; cout << endl; } return 0; }
Output
the 2D vector is: 4 5 3 10 2 7 11 3 2 1 12
Advertisements