C++ 関数はどのようにして複数の戻り値を返すのでしょうか?

この記事では、C++言語で関数が同じ型または異なる型の戻り値を複数返す方法を紹介します。

C++の場合、 Pythonや他の言語のように 1 つの関数で複数の値を返すことはできませんが、 2 つ以上の値を返す必要がある場合がよくあります。この状況に対応して、ペアタプル(タプル)、およびその他のデータ構造を使用して、 2 つ以上の戻り値を返すC++関数を実装できます。この記事では、pair を例として、この 2 つの具体的な使い方を紹介します。

一、ペア<>

まず、 C++関数の型をペア<>として定義する必要があります<>には2 つの戻り値のデータ型が含まれます。たとえば、次のコードに示すように、関数Aを関数型ペア<double**, double*>で定義します。これは、この関数には 2 つの戻り値があり、2 つの戻り値のデータ型は次のとおりであることを意味します。double* . * double * .

pair<double**, double*> A(double** pafScanline)
{
// 这里是函数的代码部分
// ...
    return make_pair(a, b);
}

このうち、関数の戻り値の部分では、データ型が double** と double* の 2 つの変数を make_pair() 関数に基づいて結合し、ペア構造を形成して返す必要があります。

次に、上記のペア、make_pair()などはすべてstd名前空間で定義されているため、この名前空間を最初に宣言する必要があります。繰り返しになりますが、この関数を呼び出す前に、最初にペア<double** 、変数を定義する必要があります。 double*> 型の値を指定し、関数の戻り値をそれに代入します。

次に、.firstおよび.secondコマンドを通じて、pair<double**, double*>の 2 つの変数がそれぞれ取得されます。

この時点で、 C++関数  を通じて 2 つの戻り値を返すメソッドを実装しました

二、tuple<>

3つ以上の戻り値を返す必要がある場合は、タプルのデータ構造に基づいて上記の操作と同様の方法で実装できます。

  1. 参照ヘッダファイル

#include <tuple>

2. タプルの初期化

std::tuple の初期化は、コンストラクターを通じて実現できます。

// Creating and Initializing a tuple
std::tuple<int, double, std::string> result1 { 22, 19.28, "text" };

この初期化メソッドは各要素のデータ型を定義する必要があり、比較的面倒ですが、C++11 には別のメソッド std::make_tuple も用意されています。

3. std::make_tuple

// Creating a tuple using std::make_tuple
auto result2 = std::make_tuple( 7, 9.8, "text" );

この初期化方法は、要素タイプを 1 つずつ指定するという問題を回避し、各要素タイプの導出を自動的に実現します。

例 1 を完成させます。

#include <iostream>
#include <tuple>
#include <string>

int main()
{
  // Creating and Initializing a tuple
  std::tuple<int, double, std::string> result1 { 22, 19.28, "text" };
  // Compile error, as no way to deduce the types of elements in tuple
  //auto result { 22, 19.28, "text" }; // Compile error
  // Creating a tuple using std::make_tuple
  auto result2 = std::make_tuple( 7, 9.8, "text" );
  // std::make_tuple automatically deduced the type and created tuple
  // Print values
  std::cout << "int value = " << std::get < 0 > (result2) << std::endl;
  std::cout << "double value = " << std::get < 1 > (result2) << std::endl;
  std::cout << "string value = " << std::get < 2 > (result2) << std::endl;
  return 0;
}

出力:

<strong>int</strong> 値 = 7

<strong>2 倍</strong>値 = 9.8

文字列値 = テキスト

例 2 を完了します。

#include <iostream>
#include <tuple>
#include <functional>
 
std::tuple<int, int> f() // this function returns multiple values
{
  int x = 5;
  return std::make_tuple(x, 7); // return {x,7}; in C++17
}
 
int main()
{
  // heterogeneous tuple construction
  int n = 1;
  auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
  n = 7;
  std::cout << "The value of t is " << "("
       << std::get<0>(t) << ", " << std::get<1>(t) << ", "
       << std::get<2>(t) << ", " << std::get<3>(t) << ", "
       << std::get<4>(t) << ")\n";
 
  // function returning multiple values
  int a, b;
  std::tie(a, b) = f();
  std::cout << a << " " << b << "\n";
}

出力:

t の値は (10, テスト, 3.14, 7, 1) です。

5 7

おすすめ

転載: blog.csdn.net/qq_45577269/article/details/129650027