Updated April 1, 2023
Introduction to size() in C++
The std::size( ) function returns the size of the given variable, container or an array. The std::size( ) function is a built in function in the C++ STL (Standard Template Library). The std::size( )function is available if any of the headers are included like <array>, <set>, <list>, <map>, <string>, <deque>, <forward_list>, <regex>, <vector>, <string_view>, <unordered_map>, <unordered_set> etc and hence the std::size( ) function can be apply to all these variables, containers or arrays.
Syntax:
template< class T, std::size_t N >constexpstd::size_t size(const T (&a)[N]) noexcept
Where a is the variable or container or an array which stores some value. The function returns the size of the variable or container or an array.
Implementation of size() function in C++ SLT
Lets see the latest implantation version that is c++ 11 version of std::size() function as below:
Code:
template< class T, std::size_t N >
constexpstd::size_t size( const T (&a)[N]) noexcept
{
return N;
}
As in above code the size(const T (&a)[N] ) function is call by reference. The references stores the address of pass variable and directly referencing variable and processing directly to them and returns the size of the variable passes. C++ overloading size( ) function to std::size_t size( const T (&a)[N] ).
Examples
We write the c++ code to understand the size() function more clearly with the following example where we use size( ) function to get the size of string variable, as below:
Example #1
Code:
#include<iostream>
using namespace std;
int main()
{
int n;
string s = "Get the size of this string";
n = s.size();
cout<< "The size of the given string is = " << n;
return 0;
}
Output:
As in above code this function is use to return the length of the string or number of characters present in string object in terms of bytes. The function does not accept any parameter.
Next we write the c++ code and we apply the size( ) function on vector object, so we will call size( ) function on vector object-
Example #2
Code:
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> v;
cout<< "Initial size of the vector = " <<v.size() << '\n';
for (int i=10; i<60; i=i+10)
v.push_back(i);
cout<< "After insert an elements the size of the vector = " <<v.size() << '\n';
v.pop_back();
cout<< "After pop an elements the size of the vector = " <<v.size() <<endl;
return 0;
}
Output:
As in above code the first vector is empty hence, v.size() function return 0, after inserting 5 elements it will return 5 and after pop 1 element it will return 4.
Next we write the c++ code to apply the size( ) function on list object, so we will call size( ) function on list object-
Example #3
Code:
#include <iostream>
#include <list>
using namespace std;
int main ()
{
list<char> l;
cout<< "Initial size of the list = " <<l.size() << '\n';
for ( int i=0; i<26; i++ )
l.push_back( 'a'+1 );
cout<< "After insert an elements the size of the list = " <<l.size() << '\n';
l.pop_back( );
cout<< "After pop an elements the size of the list = " <<l.size() <<endl;
return 0;
}
Output:
As in above code the first list is empty hence, l.size() function return 0, after inserting 26 characters it will return 26 and after pop 1 element it will return 25.
Next we write the c++ code to apply the size( ) function on set object, which stores unique elements in specific order so we will call size( ) function on set object-
Example #4
Code:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> s;
cout<< "Initial size of the set = " <<s.size() <<endl;
for ( int i = 10; i< 60; i=i+10 ) {
s.insert(i );
}
cout<< "After insert an elements the size of the set = " <<s.size() << '\n';
s.erase(10);
cout<< "After pop an elements the size of the set = " <<s.size() << '\n';
return 0;
}
Output:
As in above code the first set is empty hence, s.size() function return 0, after inserting 5 elements it will return 5 and after erase 1 element it will return 4.
Next we write the c++ code to apply the size( ) function on array object, which stores duplicate element, so we will call size( ) function on array object-
Example #5
Code:
#include <iostream>
#include <array>
using namespace std;
int main(void) {
array<int, 5> a;
cout<< "The size of the array is = " <<a.size() <<endl;
return 0;
}
Output:
As in above code the first array object is created with 5 elements, so the size( ) function return 5.
Conclusion
The std::size( ) function returns the size of variable, container or an array, which is a built in function in the C++ STL. The std::size( )function is available if we include <array>, <set>, <list>, <map>, <string>, <deque>, <vector> and all header files.
Recommended Articles
This is a guide to size() in C++. Here we discuss the brief overview on size() in C++ and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –