
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- User-defined Exceptions in C# with Example
- User Defined Literals in C++
- Flatten 2D Vector in C++
- User-defined Custom Exception in C#
- User-defined Exceptions in Python with Examples
- What are user-defined exceptions in C#?
- What are user defined data types in C#?
- How to create user defined exceptions in C#?
- C++ set for user defined data type?
- User-Defined Exceptions in Python
- Select into a user-defined variable with MySQL
- MySQL ORDER BY with numeric user-defined variable?
- PHP User-defined functions
- Using User-Defined Variables in MySQL
- Set user-defined variable with table name in MySQL prepare statement?

Advertisements