In this C++ tutorial, you will learn how to get the size or length of a vector using vector::size() function, with examples.
Get the Size of a C++ Vector with size()
To get the number of elements currently stored in a C++ std::vector, call its size() member function.
vectorName.size()
The return value is the vector’s current element count. For example, if a vector contains three integers, size() returns 3. If it contains no elements, size() returns 0.
C++ Vector size() Return Type
vector::size() returns the vector’s size_type, which is an unsigned integer type suitable for representing the number of elements in that vector. In most code, you can use the value directly without converting it to int.
std::vector<int> nums = {10, 20, 30};
std::vector<int>::size_type count = nums.size();
std::cout << count;
If you explicitly convert the result to int, make sure the vector cannot be larger than the range supported by int. Keeping the value as size_type or using auto avoids an unnecessary narrowing conversion.
C++ Vector Size Examples
1. Find Vector Length using size()
In the following C++ program, we define a vector of integers, add some elements to it, and then find its length using size() function.
C++ Program
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums;
nums.push_back(24);
nums.push_back(81);
nums.push_back(57);
cout << nums.size();
}
Output
3
The vector contains three elements, so nums.size() evaluates to 3.
2. Length of Empty Vector
In the following C++ program, we define a vector of integers, with no elements in it, and then find its length using size() function. size() should return a value of zero.
C++ Program
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums;
cout << nums.size();
}
Output
0
For a simple empty/non-empty test, C++ also provides empty(). Use size() when you need the actual element count.
3. Length of Vector using Foreach
You can also use C++ Foreach statement to get the length of size of a vector.
In the following program, we shall define a vector and initialize with elements. Then take a len variable with zero initial value that acts as a counter to count the number of elements in the vector. Use Foreach statement to iterate over the elements of vector and increment the counter during each iteration.
C++ Program
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums;
nums.push_back(24);
nums.push_back(81);
nums.push_back(57);
int len = 0;
for (int element: nums)
len++;
cout << len;
}
Output
3
This loop-based approach produces the same count, but it iterates through every element. When you only need the vector length, prefer size(), which directly reports the stored element count.
4. Get the Size of an Initialized C++ Vector
A vector can have elements immediately when it is constructed. The value returned by size() reflects those initialized elements.
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {10, 20, 30, 40, 50};
std::cout << nums.size();
}
Output
5
C++ Vector size() vs capacity()
size() and capacity() describe different properties of a vector. size() is the number of elements that currently exist. capacity() is the number of elements the vector can hold in its currently allocated storage before it must allocate more storage.
size(): current number of elements.capacity(): currently allocated element capacity.
After adding elements, a vector may have a capacity larger than its size. Therefore, do not use capacity() when you want the vector length.
Does C++ Vector Have a length() Function?
std::vector does not provide a length() member function. Use size() to get the number of elements. The word “length” is often used informally when describing a vector’s element count, but the C++ vector API uses size().
Set the Initial Size of a C++ Vector
To create a vector with a specific initial number of elements, pass the count to the vector constructor. For example, std::vector<int> nums(5); creates five integer elements, so nums.size() is 5.
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums(5);
std::cout << nums.size();
}
Output
5
You can change the element count later with resize(newSize). This is different from reserve(), which can increase storage capacity without changing the number of elements returned by size().
How C++ Vector Size Changes
A vector can grow as elements are added. Operations such as push_back() increase the size by adding elements. You can also change the number of elements explicitly with resize().
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums;
std::cout << nums.size() << "\n";
nums.push_back(10);
nums.push_back(20);
std::cout << nums.size() << "\n";
nums.resize(5);
std::cout << nums.size();
}
Output
0
2
5
When resize(5) is called on this vector<int>, the vector’s size becomes five. The newly added integer elements are value-initialized.
Checking C++ Vector Indexes with size()
Because valid vector indexes run from 0 through size() - 1 when the vector is not empty, size() is commonly used when writing index-based loops.
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {24, 81, 57};
for (std::vector<int>::size_type i = 0; i < nums.size(); ++i) {
std::cout << nums[i] << "\n";
}
}
The loop condition i < nums.size() prevents the loop from using an index beyond the current number of elements.
Common C++ Vector Size Mistakes
- Do not call
length()on a vector; usesize(). - Do not use
capacity()as the element count. - Do not subtract
1fromsize()without first ensuring the vector is non-empty. - Avoid converting
size()tointunless you know the value fits in anint. - Use
empty()when you only need to know whether the vector contains zero elements.
C++ Vector Size Editorial QA Checklist
- Verify that every example uses
size()for the current element count, notcapacity(). - Confirm that empty-vector examples return
0. - Check that any index loop uses a condition equivalent to
i < vector.size(). - Confirm that text does not imply
std::vectorhas alength()member function. - Check that examples converting the result of
size()to a signed integer explain the narrowing risk. - When
resize()is discussed, distinguish changing the number of elements from merely reserving storage.
Summary of C++ Vector Size and Length
In this C++ Tutorial, we learned to find the size of vector, which is the number of elements in a vector using builtin functions or statements like foreach.
For normal C++ code, use vector::size() when you need the vector’s current number of elements. An empty vector has size 0, and the size changes as elements are added, removed, or when resize() changes the element count.
TutorialKart.com