int DFS(a, b,c,d)
{
first=a+b;
second=c+d;
return(first,second);
}
solution, cost_limit = DFS(a, b,c,d);
can I do something like this ? and how?
int DFS(a, b,c,d)
{
first=a+b;
second=c+d;
return(first,second);
}
solution, cost_limit = DFS(a, b,c,d);
can I do something like this ? and how?
In C++11 you can use the tuple types and tie for that.
#include <tuple>
std::tuple<int, int> DFS (int a, int b, int c, int d)
{
return std::make_tuple(a + b, c + d);
}
...
int solution, cost_limit;
std::tie(solution, cost_limit) = DFS(a, b, c, d);
std::pair<int, int>tie in C++03, so it comes out looking pretty ugly: std::pair<int, int> result = DFS(a,b,c,d); solution = result.first; cost_limit = result.second;.tie)With C++17, you can unpack a pair or a tuple
auto[i, j] = pair<int, int>{1, 2};
cout << i << j << endl; //prints 12
auto[l, m, n] = tuple<int, int, int>{1, 2, 3};
cout << l << m << n << endl; //prints 123
<int, int> anymore BTW: stackoverflow.com/questions/9270563/…You can do this two ways:
Create a struct with two values and return it:
struct result
{
int first;
int second;
};
struct result DFS(a, b, c, d)
{
// code
}
Have out parameters:
void DFS(a, b, c, d, int& first, int& second)
{
// assigning first and second will be visible outside
}
call with:
DFS(a, b, c, d, first, second);
One thing you should be know is that if a,b,c,d are not base types, but instances of a class you defined, let's say Foo, and you overload the = operator of the class, you must ensure the fact that the operator will return a reference to the object which is assigned, or else you will not be able to chain assignments ( solution = cost_limit = DFS(..) will only assign to cost_limit). The = operator should look like this:
Foo& Foo::operator =(const Foo& other)
{
//do stuff
return other;
}