std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++ Last Updated : 12 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report There can be some instances where you need to return multiple values (maybe of different data types ) while solving a problem. One method to do the same is by using pointers, structures or global variables, already discussed here There is another interesting method to do the same without using the above methods, using tuples (for returning multiple values ) and pairs (for two values). We can declare the function with return type as pair or tuple (whichever is required) and can pack the values to be returned and return the packed set of values. The returned values can be unpacked in the calling function. Tuple A tuple is an object capable to hold a collection of elements where each element can be of a different type. Class template std::tuple is a fixed-size collection of heterogeneous values Pair This class couples together a pair of values, which may be of different types A pair is a specific case of a std::tuple with two elements Note : Tuple can also be used to return two values instead of using pair . Cpp #include<bits/stdc++.h> using namespace std; // A Method that returns multiple values using // tuple in C++. tuple<int, int, char> foo(int n1, int n2) { // Packing values to return a tuple return make_tuple(n2, n1, 'a'); } // A Method returns a pair of values using pair std::pair<int, int> foo1(int num1, int num2) { // Packing two values to return a pair return std::make_pair(num2, num1); } int main() { int a,b; char cc; // Unpack the elements returned by foo tie(a, b, cc) = foo(5, 10); // Storing returned values in a pair pair<int, int> p = foo1(5,2); cout << "Values returned by tuple: "; cout << a << " " << b << " " << cc << endl; cout << "Values returned by Pair: "; cout << p.first << " " << p.second; return 0; } Output: Values returned by tuple: 10 5 a Values returned by Pair: 2 5 Comment More infoAdvertise with us Next Article multimap value_comp() function in C++ STL K kartik Improve Article Tags : C++ STL CPP-Functions Practice Tags : CPPSTL Similar Reads std::tuple_element() and std::tuple_size() in C++ with Examples A tuple is an object that can hold a number of elements. The elements can be different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. The functions- tuple_element() and tuple_size() are only defined for elements using tuple_like interface 2 min read multimap value_comp() function in C++ STL The multimap::value_comp() method returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second. Here the 1st object compares the object of type std::multimap::type. The arguments taken by this function object are of member type t 2 min read unordered_multimap begin() and end() function in C++ STL The unordered_multimap::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the container or to the first element in one of its buckets. Syntax: unordered_multimap_name.begin(n) Parameters: The function accepts one parameter. If a parameter is passed, 5 min read How to traverse through all values for a given key in multimap? Given a multimap and a key of the multimap, our task is to simply display the (key - value) pairs of the given key. In multimap we can have multiple (key - value) pair for the same key. Suppose our multimap contains key value1 102 202 302 403 504 604 70key : 2key value2 202 302 40Like in unordered_m 5 min read unordered_multimap size() function in C++ STL The unordered_multimap::size() is a built-in function in C++ STL which returns the size of the unordered_multimap. It denotes the number of elements in that container. Syntax: unordered_multimap_name.size() Parameters: The function does not accept any parameters. Return Value: It returns an integral 3 min read unordered_multiset swap() function in C++ STL The unordered_multiset::swap() is a built-in function in C++ STL which swaps the contents of two unordered_multiset containers. Note: Both of the containers should have the same type of elements. Sizes of the containers may differ. Syntax: unordered_multiset1.swap(unordered_multiset2); Parameters: T 3 min read Like