English 中文(简体)
能否同时在C++中分配2个变量?
原标题:Can i assign in 2 variables at the same time in C++?
  • 时间:2012-01-12 10:31:10
  •  标签:
  • c++
int DFS(a, b,c,d)
{
    first=a+b;
    second=c+d;
    return(first,second);
}

solution, cost_limit = DFS(a, b,c,d);

我能做这样的事情吗? 如何?

最佳回答

在C++11中,可以为此使用图形类型和<代码>tie。

#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);
问题回答

有了C++17,你可以不包奶粉或面粉

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

您可以采取以下两种方式:

  1. 1. 创建具有两个价值观的构体,并将其归为:

    struct result
    {
        int first;
        int second;
    };
    
    struct result DFS(a, b, c, d)
    {            
        // code
    }
    
  2. 具备参数:

    void DFS(a, b, c, d, int& first, int& second)
    {
        // assigning first and second will be visible outside
    }
    

    呼吁:

    DFS(a, b, c, d, first, second);
    

你们应当知道的是,如果一、b、c、d不是基类,而是你定义的一类,请说Foo,而且你超负荷了该类的操作者,那么,你必须确保经营者返回提及所分配的物体,否则你将无法进行链条转让(解决办法=费用限额=每日生活津贴)。 = 运营商应当这样做:

Foo& Foo::operator =(const Foo& other)
    {
       //do stuff
       return other;
    }

如果不可能使用C++11,则有可能使用参考文献。

通过提及参数中的变量。

int DFS(int a, int b, int c, int d, int &cost_limit)
{
    cost_limit = c + d;
    return a + b;
}

int solution, cost_limit;

solution = DFS(a, b, c, d, cost_limit);




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签