在函数中返回2个值 [英] returning 2 values within a function

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

问题描述

我想在文件读取时返回值(即行和列),并且因为我将在多个文件中读取并从每个文件获取相同的变量,我认为这将是更好的我写一个函数

I am trying to return values (namely rows and columns) upon a file read, and since I will be reading in multiple files and getting the same variables from each file, I thought it will be better for me to write a function rather than copying and pasting duplicate codes.

无论如何,我想要返回2个值并使用它们,请参阅下面的代码:

Anyway, I am trying to return 2 values and to use them too, please see my code below:

#include <iostream>
#include <fstream>

using namespace std;

int r(string fn);

int main()
{
    int a, b = r("input_a.txt");

    cout << "a --- " << a << endl;
    cout << "b --- " << b << endl;

}

int r(string fn)
{
    ifstream fin01;
    string file = fn;

    fin01.open(file.c_str());

    ...
    ...
    ...

    // Suppose I should be getting 2 for 'rows' and 3 for 'cols'
    return rows, cols;
}

我得到 0x7fff670778ec 0x7fff670778e8 为我的输出...

I got 0x7fff670778ec and 0x7fff670778e8 for my output instead...

任何指针?

int 作为返回类型的函数返回两个值:
解决方案

int r(string fn){
    /*...*/
    return rows,cols;  // <-- this is not correct
}

函数的行为不符合您的期望:

Also the way you call this function is not behaving as you might expect:

int a, b = r("input_a.txt");

这会声明两个整数,并初始化第二个与函数的返回值,但第一个保持未初始化(有关逗号运算符的更多说明,请参见 TerraPass答案)。

this declares two integers and initializes the second one with the return value of the function, but the first one stays uninitialized (see TerraPass answer for more explanation on the comma operator).

你基本上有两个选择。第一个选项是传递对函数的引用,函数将结果赋给这些引用:

You basically have two options. First option is to pass references to the function and the function assigns the results to those references:

void r(string fn, int& rows,int& cols) {
    /*...*/
    rows = x;
    cols = y;
}

并且这样调用:

int a,b;
r("someString",a,b);

但是,这样调用者必须准备这些返回值。 Imho它是更方便的使用返回值从一个函数返回结果(听起来逻辑,没有?)。要做到这一点,你只需要定义一个类型封装两个整数:

However, in this way the caller has to "prepare" those return values. Imho it is more convenient to use the return value to return results from a function (sounds logical, no?). To do this you just have to define a type that encapsulates the two integers:

struct RowAndCol { int row;int col; };

RowAndCol r(string fn) {
    /*...*/
    RowAndCol result;
    result.row = x;
    result.col = y;
    return result;
}

并像这样调用:

RowAndCol rc = r("someString");

注意,你也可以使用 std :: pair< int,int> ; ,而不是定义自定义结构(参见例如 molbdnilos answer )。然而,当你知道确切的对象包含什么时,最好是给它一个合理的名称(例如 RowAndCol ),而不是使用裸的 std :: pair< int,int> 。这也将帮助你,如果你需要添加更多的方法到结构以后(例如,你可能想重载 std :: ostream& operator<< 以在屏幕上打印它)。

Note that you could also use a std::pair<int,int> instead of defining your custom struct (see e.g. molbdnilos answer). However, IMHO whenever you know exactly what is contained in the pair, it is better to give it a reasonable name (e.g. RowAndCol) instead of using a naked std::pair<int,int>. This will also help you in case you need to add more methods to the struct later (e.g you might want to overload the std::ostream& operator<< for your struct to print it on the screen).

PS:实际上,您的输出看起来不像是由您显示的代码生成的。这些是一些内存地址,但在你的代码中既没有指针也没有操作符的地址。

PS: Actually your output does not look like it was produced by the code you are showing. Theses are some memory addresses, but in your code there is neither a pointer nor an address-of operator.

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

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