Vector size() in C++ STL Last Updated : 15 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the vector size() is a built-in method used to find the size of a vector. The size of a vector tells us the number of elements currently present in the vector. In this article, we will learn about the vector size() method.Let's take a look at the simple code example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 5, 4}; // Finding the size of v cout << v.size(); return 0; } Output5Explanation: The number of elements in the vector v is 5, which is returned by the vector size() function.Syntax of Vector size()The vector size() method is the member function of std::vector defined inside <vector> header file.v.size();This function does not take any parameters.Return ValueReturns the number of elements in the vector as size_t value.If the vector is empty, returns 0.Example of Vector size()The vector size() is a very simple and easy to use function. The code examples below show how to use this function:Find the Number of Elements in Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2}; // Finding the size of v cout << v.size() << endl; v.insert(v.end(),{3, 5, 4}); // Finding the size of v cout << v.size(); return 0; } Output2 5Check if Vector is EmptyThe size() method can be used to check whether a vector is empty by comparing its size to zero. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Check if vector is empty if (v.size() == 0) cout << "Empty."; else cout << "Not empty."; return 0; } OutputEmpty.Traverse the Vector Using Index and size() C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 5, 4}; // Print elements using a loop for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } return 0; } Output1 2 3 5 4 Vector size() vs capacity()The vector size() returns the number of elements currently stored in the vector while vector capacity() returns the total number of elements the vector can hold before needing to reallocate memory.Featuresize()capacity()PurposeReturns the number of elements in the vector.Returns the total memory capacity allocated to the vector.Dynamic BehaviourIncreases when elements are added. Decreases when the element is deleted.May remain the same until a reallocation occurs.RelationThe vector size is always less than or equal to the vector capacity.Vector capacity is always greater than or equal to the vector size. Comment More infoAdvertise with us Next Article Vector max_size() in C++ STL A abhishekcpp Follow Improve Article Tags : C++ STL CPP-Functions cpp-vector cpp-containers-library +1 More Practice Tags : CPPSTL Similar Reads Vector max_size() in C++ STL In C++, vector max_size() is a built-in function used to find the maximum number of elements that can be held by the vector container. In this article, we will learn about the vector max_size() function in C++.Let's take a look at an example that illustrates this method:CPP#include <bits/stdc++.h 3 min read Vector insert() in C++ STL In C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++. Letâs take a look at an example that shows the how to use this function:C++#include <bits/stdc++.h> using 4 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 Vector reserve() in C++ STL In C++, vector reserve() is a built-in function that reserves the memory for at least a specified number of elements in the vector. It changes the capacity of the vector such that you can add at least the specified number of elements without triggering the internal memory reallocation.Letâs take a q 2 min read Vector end() in C++ STL In C++, the vector end() is a built-in method used to obtain an iterator pointing to the theoretical element after the last element of the vector. Even though this iterator does not point to a valid element, it serves as a marker for the end of the vector.Letâs take a look at an example that illustr 3 min read vector::at() in C++ STL In C++, vector at() is a built-in method used to access an element in a vector using index. It is the only access method that performs bound checking before accessing the element to confirm whether the given index lies is within the vector.Letâs take a quick look at a simple example that uses vector 2 min read Like