How to Find the Size of a Vector in C++? Last Updated : 19 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Let’s take a look at a simple example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 4, 7, 9}; // Finding the size of vector cout << v.size(); return 0; } Output4 Explanation: The vector container automatically tracks its own size which can be efficiently retrieved by size() method.There are also some other methods in C++ to find the size of vector. They are as follows:Table of ContentUsing IteratorsManually Using Range Based for LoopUsing IteratorsThe vector store all its elements sequentially in a continuous memory. It means that all the elements between the memory address of first and last element belongs to that vector. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 4, 7, 9}; // Find the size of vector cout << v.end() - v.begin(); return 0; } Output4Explanation: We find the number of elements in vector by subtracting the vector begin() iterator which points to the first element and vector end() iterator which point to the element just after the last element.Note: We can also use distance() method instead of directly subtracting the iterators.By CountingRange based for loop allows users to iterate an STL container without needing its size or iterator. We can use this property to determine the size of the vector by counting the number of iterations. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 4, 7, 9}; // Initialize the counter with 0 int c = 0; // Count number of elements in vector manually for (auto i : v) c++; cout << c; return 0; } Output4 Comment More infoAdvertise with us Next Article How to Find the Size of a Map in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find the Size of a Vector in Bytes in C++? In C++, Vectors are dynamic containers that can change their size during the insertion or deletion of elements. In this article, we will explore how we can find the size of the vector in bytes in C++. Example: Input:myVector = {10,20,30,40,50}Output:Size of the vector in bytes is : 20 bytesFind the 2 min read How to Find the Size of a Set in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how we can find the size of a set container in C++. Example: Input: mySet={1 2 min read How to Find the Size of a Map in C++? In C++ STL, maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. In this article, we will learn how to find the size of a map in C++ STL. Example: Input: myMap = { {1, "Sravan"}, { 2 min read How to Find the Size of a List in C++? In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Size o 2 min read How to Find the Mode of Vector in C++? In C++, a vector is a dynamic array that resizes itself when needed. It also allows random access to its elements. In this article, we will learn how to find the mode of a vector in C++. The mode of a vector is the element that highest number of occurrences in the vector. Example: Input: myVector = 2 min read How to Find the Capacity of a Vector in C++? In C++, the capacity of a vector means the amount of space allocated for the vector, so the vector can hold more elements than its size without any reallocation of space. In this article, we will learn how to find the capacity of a vector in C++. Example: Input: myVector = {10,20,30}; Output: Capaci 2 min read Like