Answers for "c++ how to return 2 values from function"

C++
7

c++ return multiple values

#include <tuple>

std::tuple<int, int> divide(int dividend, int divisor) {
    return  std::make_tuple(dividend / divisor, dividend % divisor);
}

#include <iostream>

int main() {
    using namespace std;

    int quotient, remainder;

    tie(quotient, remainder) = divide(14, 3);

    cout << quotient << ',' << remainder << endl;
}
Posted by: Guest on May-16-2020
0

return multiple values c++

#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;
}
Posted by: Guest on January-24-2022

Code answers related to "c++ how to return 2 values from function"

Browse Popular Code Answers by Language