<<< std::set     Index     Functions returning a pair >>>

8. std::pair

  • 
    std::pair< T1, T2 >
    
    

    is a C++ structure that holds one object of type T1 and another one of type T2:

    
        #include <cassert>
        #include <string>
        #include <utility>
        using namespace std;
        int main (int argc, char* argv[])
        {
            pair< string, string > strstr;
            strstr.first = "Hello";
            strstr.second = "World";
    
            pair< int, string > intstr;
            intstr.first = 1;
            intstr.second = "one";
    
            pair< string, int > strint( "two", 2 );
            assert( strint.first == "two" );
            assert( strint.second == 2 );
    
            return 0;
        }
    
    
  • A pair is much like a container that holds exactly two elements.

  • The pair is defined in the standard header named utility.

<<< std::set     Index     Functions returning a pair >>>