![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Queue::size() Function
The C++ std::queue::size() function in the queue container is used to return the number of elements currently stored in the queue. It allows efficient tracking of the queue occupancy, helping in making decisions regarding further operations or resource allocation. By invoking this function we can have a count of the elements without altering the queue structure. The time complexity of this function is Constant i.e. O(1).
Syntax
Following is the syntax for std::queue::size() function.
size_type size() const;
Parameters
It does not accept any parameters.
Return value
This function returns the total number of elements present in the queue.
Example
Let's look at the following example, where we are going to retrieve the size of the queue.
#include <iostream> #include <queue> int main() { std::queue<int> x; x.push(10); x.push(20); std::cout << "Queue Size: " << x.size() << std::endl; return 0; }
Output
Output of the above code is as follows −
Queue Size: 2
Example
Consider the another scenario, where we are going to retrieve the size of the dynamic queue.
#include <iostream> #include <queue> int main() { std::queue<int> a; for(int x = 0; x < 3; ++x) a.push(x * 3); std::cout << "Queue Size: " << a.size() << std::endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
Queue Size: 3
Example
In the following example, we are going to retrieve the size of the queue after performing the pop() function.
#include <iostream> #include <queue> int main() { std::queue<int> x; x.push(1); x.push(222); x.pop(); std::cout << "Queue Size: " << x.size() << std::endl; return 0; }
Output
If we run the above code it will generate the following output −
Queue Size: 1
Example
Following is the example, where we are going to retrieve the size of an empty queue.
#include <iostream> #include <queue> int main() { std::queue<int> x; std::cout << "Queue Size:" << x.size() << std::endl; return 0; }
Output
Output of the above code is as follows −
Queue Size:0