# vector::empty() and vector::size() in C++ STL

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

vector::empty()

empty() function is used to check if the vector container is empty or not.
Syntax :

vectorname.empty()
Parameters :
No parameters are passed.
Returns :
True, if vector is empty
False, Otherwise

Examples:

Input  : myvector = 1, 2, 3, 4, 5
myvector.empty();
Output : False

Input  : myvector = {}
myvector.empty();
Output : True

Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.

 // CPP program to illustrate // Implementation of empty() function #include #include using namespace std;   int main() {     vector myvector{};     if (myvector.empty())     {         cout << "True";     }     else {         cout << "False";     }     return 0; }

Output

True

Application :
Given a list of integers, find the sum of the all the integers.

Input  : 1, 5, 6, 3, 9, 2
Output : 26
Explanation -  1+5+6+3+9+2 = 26

Algorithm
1. Check if the vector is empty, if not add the back element to a variable initialised as 0, and pop the back element.
2. Repeat this step until the vector is empty.
3. Print the final value of the variable.

 // CPP program to illustrate // Application of empty() function #include #include using namespace std;   int main() {     int sum = 0;     vector myvector{ 1, 5, 6, 3, 9, 2 };     while (!myvector.empty())     {         sum = sum + myvector.back();         myvector.pop_back();     }     cout << sum;     return 0; }

Output
26
vector::size()

size() function is used to return the size of the vector container or the number of elements in the vector container.
Syntax :

vectorname.size()
Parameters :
No parameters are passed.
Returns :
Number of elements in the container.

Examples:

Input  : myvector = 1, 2, 3, 4, 5
myvector.size();
Output : 5

Input  : myvector = {}
myvector.size();
Output : 0

Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.

 // CPP program to illustrate // Implementation of size() function #include #include using namespace std;   int main() {     vector myvector{ 1, 2, 3, 4, 5 };     cout << myvector.size();     return 0; }

Output
5

Why is empty() preferred over size()
empty() function is often said to be preferred over the size() function due to some of these points-

1. empty() function does not use any comparison operators, thus it is more convenient to use
2. empty() function is implemented in constant time, regardless of container type, whereas some implementations of size() function require O(n) time complexity such as list::size().

Application :
Given a list of integers, find the sum of the all the integers.

Input  : 1, 5, 6, 3, 9, 2
Output : 26
Explanation -  1+5+6+3+9+2 = 26

Algorithm
1. Check if the size of the vector is 0, if not add the back element to a variable initialised as 0, and pop the back element.
2. Repeat this step until the size of the vector becomes 0.
3. Print the final value of the variable.

 // CPP program to illustrate // Application of size() function #include #include using namespace std;   int main() {     int sum = 0;     vector myvector{ 1, 5, 6, 3, 9, 2 };     while (myvector.size() > 0) {         sum = sum + myvector.back();         myvector.pop_back();     }     cout << sum;     return 0; }

Output
26

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

Article Tags :
Practice Tags :