<utility> standard header file

The <utility> header file contains templates structure and some other functions which is utilized by the STL containers to carry out various operations.The <utility> header file is especially meant for used by the other headers file.So it supports other C++ features as a general purpose utilities.

Programmer can also use the structure templates declared under this header’s file directly in their program.In fact,knowing how to use the structure-mainly the ‘pair‘ structure- provided in this header file will provide the user with a basic understanding of how the STL containers namely map ,multimap,unordered_map, and unordered_multimap are built ,as these containers use key/value pair to manage their elements.

***The content will be updated constantly.


template<class Tp1 , class Tp2 >struct pair;Manages two objects as a pair.
template<class T>class initializer_list;This class can store indeterminate number of values of the type.


get<val>( ) (C++11)Get either the value of the object in a pair element.
exchange() (C++14)Exchange values in two objects.

Specialization (C++11)


template<class Tp1 , class Tp2>
  struct tuple_element<0, std::pair<Tp1, Tp2>>
  { typedef Tp1 type; };

template<class Tp1 , class Tp2>
  struct tuple_element<1, std::pair<Tp1, Tp2>>
  { typedef Tp2 type; };

Each of the template defines a type that is synonyms for the type corresponding to the type of the pair element.


template<typename Tp1, typename Tp2>
  struct tuple_size<std::pair<Tp1, Tp2>>
  : public integral_constant<std::size_t, 2> { };

The tuple_size of <utility> header is inherited from the integral_constant structure.The integral_const structure has a static const member data name value.In this specialization the member data value is initialized to 2.


Stop wasting time,earn money($$$) from your website-Join Now!

Code example

#include <utility>
#include <iostream>

using namespace std ;

int main( )
pair<int, string > pr{ 45 , “forty-five” };

///using ‘get’ function
cout<< get<0>(pr) << endl;

///using ‘exchange’ function
exchange( pr.first , 4500 ) ;
cout<< get<0>(pr) << endl;

cin.get( ) ;
return 0 ;