在c ++中返回两个变量 [英] Return two variables in c++

查看:75
本文介绍了在c ++中返回两个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在函数中返回两个变量。在以下函数中,我想返回结果和计数器。有人可以帮帮我吗?





  int 重复( int  array3 [])
{
int copy [ 100 ];

int result = 0 ;
int number;
for int i = 0 ; i< 100; i ++)
{

copy [i] = array3 [i];
}

for int i = 0 ; i < 100 ; i ++)
{
int counter = 0 ;
for int j = 0 ; j < 100 ; j ++)
{
if (array3 [i] == copy [j])
{
counter ++;
}
如果(计数器> 结果)
{
result = counter;
number = array3 [i];
}
}

}

return 结果,数字;

}

解决方案

 std :: pair <   int,    int  > 重复(int array3 [])
{
//:
return std :: make_pair (结果,数字);
}

std :: pair < int, < span class =code-attribute> int > answer =重复(arr) ;
std :: cout < < answer.first << << answer.second < ;< std :: endl;


有一些选择。解决方案1是其中之一。类似的解决方案是返回具有两个元素的数组。这两个解决方案不是很干净,因为它们在返回的两个值的语义方面不健全。在某些(或多或少的情况下)这就是需要的。



如果值带有不同的语义,你应该更好地使用更清晰的方法:创建一个结构或类使用所需类型的两个实例成员并返回此类型的实例。



另外,让我们考虑超越返回的方式。或者,您可以通过引用传递参数。如果更改参数值,将在输出时修改它。请参阅: http://www.learncpp.com/cpp-tutorial/73-passing -arguments-by-reference [ ^ ]。



最后的旁注:很多开发人员都犯了一个很棒的代码设计错误:从函数中返回一些状态或错误代码。这种方法严重过时而且不好。使用C ++和大多数现代语言,你不应该返回任何类似的东西但是应该抛出异常。



-SA


There is any way we can return two variables in a function. In the following function I would like to return result and counter. Can anyone help me please?


int repetition(int array3[])
{
    int copy[100];

    int result = 0;
    int number;
    for (int i = 0; i<100; i++)
    {

        copy[i]=array3[i];
    }

    for (int i = 0; i < 100; i++)
    {
        int counter = 0;
        for (int j = 0; j < 100 ; j++)
        {
            if (array3[i] == copy[j])
            {
                counter++;
            }
            if (counter > result )
            {
                result = counter;
                number = array3[i];
            }
        }

    }

return result, number;

}

解决方案

std::pair<int, int> repetition(int array3[])
{
   // :
   return std::make_pair(result, number);
}

std::pair<int, int> answer = repetition(arr);
std::cout << answer.first << ", " << answer.second << std::endl;


There are some options. Solution 1 is one of them. A similar solution is to return an array with two elements. These two solutions are not very clean, because they are not sound in terms of semantics of the two values returned. In some (more or less rare cases) this is what's needed.

If the values carry distinct semantics, you should better use clearer approach: create a structure or a class with two instance member of required types and return the instance of this type.

Also, let's consider the way beyond return. Alternatively, you can pass a parameter by reference. If you change the parameter value, it will be modified on output. Please see: http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference[^].

And a final, side note: many developers do a great code design mistake: returning some status or error code from functions. This approach is badly obsolete and bad. With C++ and most modern languages, you shouldn't return anything like that but should throw an exception.

—SA


这篇关于在c ++中返回两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆