<utility> in C++

It is a header file which contains utilities in unrelated domains.

  • Pairs: Theses are the objects which can hold two different types of values.
  • Generic Relational Approach: It is used for the relational operators !=, >, = under a specific namespace: rel_ops.
  • Generic swap function: This a standard definition used by default by the components of the standard library for all types that do not provide their own overload: swap.

Functions:

  1. swap: It exchanges the value of two objects.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate
    // the use of swap function
    #include <iostream>     
    #include <utility>     
    using namespace std;
      
    // Driver Program
    int main () 
    {
      
        // a: _ _ _ _ (empty array in beginning)
        int a[4];         
          
        // Initializing, a: _ _ _ _ & b: 10 20 30 40
        int b[] = {10, 20, 30, 40};
          
        // After swapping, a: 10 20 30 40 & b: _ _ _ _
        swap(a, b);             
          
        cout << "a contains:";
        for (int i; i<4;i++) 
            cout << a[i]<<endl;
          
        return 0;
    }

    chevron_right

    
    

    Output:

    a contains:10
    20
    30
    40
    
  2. make_pair: It constructs a pair with its first element set to x and its second element set to y.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate
    // the use of make_pair 
    #include <utility>     
    #include <iostream> 
    using namespace std;
      
    // Driver program
    int main () 
    {
          
        // Initializing the pair with int type
        pair <int, int> a;
        pair <int, int> b;
          
        // Use of the make_pair function
        a = make_pair (10, 20);
          
        // It's ok to write like this as implicit 
        // conversion takes place from pair<double, char>
        b = make_pair (15.5, 'B'); 
          
        // Printing the fist and second values of the pair
        cout << "a: " << a.first << ", " << a.second << endl;
        cout << "b: " << b.first << ", " << b.second << endl;
          
        return 0;
    }

    chevron_right

    
    

    Output:

    a: 10, 20
    b: 15, 66
    
  3. move: It moves as rvalue. move is used to indicate that an object may be “moved from”, i.e. allowing the transfer of resources from an object(to be moved) to another object.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate
    // the use of move 
    #include <utility>     
    #include <iostream>     
    #include <vector>     
    #include <string>     
    using namespace std;
      
    // Driver Program
    int main () 
    {
        string s = "Hello!!";
        string s1 = "I am a geek.";
        vector<string> gfg;
          
        // It copies 's' in gfg
        gfg.push_back (s); 
          
        // It moves 's1' in the gfg(containing 's')
        gfg.push_back (move(s1));         
          
        cout << "gfg contains:";
        for (int i=0;i< gfg.size(); i++) 
            cout << ' ' << gfg[i];
        cout<<endl;
          
        return 0;
    }

    chevron_right

    
    

    Output:

    gfg contains: Hello!! I am a geek.
    
  4. move (range of elements): It moves the elements in the range [first, last) into the range beginning at result.
    After this operation the elements in the moved-from range will still contain valid values of the appropriate type, but not necessarily the same values as before the move.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to 
    // Illustrate the use of vector
    // move range of elements
    #include <iostream> 
      
    // for move (ranges)
    #include <algorithm> 
      
    // for move (objects)
    #include <utility>   
    #include <vector>    
    #include <string> 
    using namespace std;
      
    // Driver program
    int main () 
    {
        vector<string> string1 = {"Hello!", "I", "am", "a", "geek"};
        vector<string> string2 (5);
          
        // moving ranges:
        cout << "Moving ranges...\n";
          
        // use of move i.e.it moves from first to last element in 
        // string 1 to the string2 from it's(string 2) starting
        move ( string1.begin(), string1.begin()+5, string2.begin() );
          
        cout << "string1 contains " << string1.size() 
                                    << " elements\n";
          
        cout << "string2 contains " << string2.size() 
                            << " elements(after moving):";
          
        for (int i=0; i<string2.size(); i++) 
            cout << string2[i] << " ";
        cout << endl;
          
          
        return 0;
    }

    chevron_right

    
    

    Output:

    Moving ranges...
    string1 contains 5 elements
    string2 contains 5 elements(after moving):Hello! I am a geek 
    
  5. move_if_noexcept: It obtains an rvalue reference to its argument if its move constructor does not throw exceptions, otherwise obtains an lvalue reference to its argument.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate
    // the use of move_if_noexcept
    #include<iostream>
    #include<utility>
    using namespace std;
      
    struct Bad
    {
          
        Bad() {}
          
        // may throw
        Bad(Bad&&) 
        {
            cout << "Throwing move constructor called"<<endl;
        }
          
        // may throw as well
        Bad(const Bad&) 
        {
            cout << "Throwing copy constructor called"<<endl;
        }
    };
      
    struct Good
    {
        Good() {}
          
        // will NOT throw
        Good(Good&&) noexcept
        {
            cout << "Non-throwing move constructor called"<<endl;
        }
          
        // will NOT throw
        Good(const Good&) noexcept
        {
            cout << "Non-throwing copy constructor called"<<endl;
        }
    };
      
      
    // Driver Program 
    int main()
    {
        Good g;
        Bad b;
        Good g2 = move_if_noexcept(g);
        Bad b2 = move_if_noexcept(b);
    }

    chevron_right

    
    

    Output:

    Non-throwing move constructor called
    Throwing copy constructor called
    
  6. decval: It returns an rvalue reference without referring to any object.
    This function selects the type of an argument as an rvalue reference if its move constructor never throw, or alternativelly, as an lvalue reference if the type is copy-constructible. If the type is neither, the function returns an rvalue, which will be selected for move-only types (even if they may throw).
    Some operations can be implemented both by moving or by copying objects, generally moving for rvalues and copying for lvalues: Moving is generally a more efficient operation than copying when the object is no longer needed (such as rvalues).

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP program to illustrate 
    // the use of declval
    #include <utility>
    #include <iostream>
    using namespace std;
      
    //class
    struct
    {
    virtual int value() = 0;
    };
      
    // Class with constructor
    class B : public A
    {
        int val_;
        public:
        B(int i, int j):val_(i*j){}
        int value() 
        {
            return val_;
              
        }
    };
      
    // Driver Program
    int main()
    {
        // int a
        decltype(declval<A>().value()) a;
          
        // int b
        decltype(declval<B>().value()) b;
              
        //same as constructor
        a = b = B(100, 20).value();
        cout << a << endl;
        return 0;
    }

    chevron_right

    
    

    Output:

    2000
    

This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.