Implementation of size( ) function in C++

Implementation of size( ) function in C++
Techiio-author
Written by Sagar RabidasFebruary 12, 2022
6 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start the discussion with Implementation of size( ) function in C++

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 that stores some value. The function returns the size of the variable or container or 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 to Implement size( ) function in C++

We write the c++ code to understand the size( ) function more clearly with the following example where we use the size( ) function to get the size of a 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;
}

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;
}

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;
}

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;
}

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;
}

C
C++
C++ functions
size( ) function
size( ) C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance