Using std::pair

suggest change

The struct template std::pair can bundle together exactly two return values, of any two types:

#include <utility>
std::pair<int, int> foo(int a, int b) {
    return std::make_pair(a+b, a-b);
}

With C++11 or later, an initializer list can be used instead of std::make_pair:

#include <utility>
std::pair<int, int> foo(int a, int b) {
    return {a+b, a-b};
}

The individual values of the returned std::pair can be retrieved by using the pair’s first and second member objects:

std::pair<int, int> mrvs = foo(5, 12);
std::cout << mrvs.first + mrvs.second << std::endl;

Output:

10

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Returning multiple values from a function:
* Using std::pair

Table Of Contents
8 Arrays
11 Loops
39 Streams
42 Returning multiple values from a function
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype