c++ return multiple values
#include
std::tuple divide(int dividend, int divisor) {
return std::make_tuple(dividend / divisor, dividend % divisor);
}
#include
int main() {
using namespace std;
int quotient, remainder;
tie(quotient, remainder) = divide(14, 3);
cout << quotient << ',' << remainder << endl;
}
return multiple values c++
#include
using namespace std;
// A Method that returns multiple values using
// tuple in C++.
tuple 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 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 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;
}
|