Find Column and Row Size of a 2D Vector in C++ Last Updated : 17 Feb, 2025 Comments Improve Suggest changes Like Article Like Report In C++, 2D vector is the vector in which each element is a vector in itself. The number of these vectors represent the column size, and the size of each vector represents the row size. In this article we will learn how to find the row size and column size of 2D vectors in C++.The simplest way to find the row size and column size is by using vector size() method. The column size can be determined by using this method on the 2d vector and the row size by using it on any of the member vector. Let's take a look at the code example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> v = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Get column size cout << "Column size: " << v.size() << endl; // Get Row size cout << "Row size: " << v[0].size(); return 0; } OutputColumn size: 3 Row size: 3Explanation: In the above method, we have taken the 2D vector in which the size of each row (member vector) is same. But in practical scenarios, it is very much possible that size of each row is different resulting in different row size. Such vectors are called jagged vectors.Note: Numbers of rows and row size are different. Number of rows refer to the total count of horizontal lines of elements within the 2D Vector while Row Size refers to the number of elements present in each individual row of the 2D Vector. Same with number of columns and column size.Row and Column Size for Jagged VectorsFor a jagged 2D vector, the column size can be determined same as that of normal 2D vector, but the row size would be determined by the size of every member vector. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> v = { {1, 2, 3}, {4, 5, 6, 7}, {8, 9} }; // Get column size cout << "Column size: " << v.size() << endl; // Row size of individual row for (int i = 0; i < v.size(); i++) cout << "\tRow " << i + 1 << " size: " << v[i].size() << endl; return 0; } OutputColumn size: 3 Row 1 size: 3 Row 2 size: 4 Row 3 size: 2 Note: If the size of the 2D vector (column size) is zero, then do not try to find the row size as the empty 2D vector does not have any member vector and trying to access it may lead to segmentation fault. Comment More infoAdvertise with us Next Article Find Column and Row Size of a 2D Vector in C++ A abhishekcpp Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read 30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is 15 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so 9 min read Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot 8 min read C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i 15+ min read C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and 9 min read C++ Polymorphism The word polymorphism means having many forms. A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So, the same person exhibits different behaviour in different situations. This is ca 5 min read Like