How does a C++ function return multiple return values?

This article introduces the method in C++ language for a function to return two or more return values ​​of the same type or different types .

For C++ , it cannot return multiple values ​​​​in one function like Python and other languages; but we often encounter the need to return two or even more values . In response to this situation, we can use pair , tuple (tuple) and other data structures to implement the C++ function to return two or more return values. This article takes pair as an example to introduce the specific usage of the two.

一、pair<>

First, we need to define the type of the C++ function as pair<> , where <> contains the data types of the two return values. For example, as shown in the following code, we define function A with the function type pair<double**, double*> , which means that this function has two return values, and the data types of the two return values ​​are double* . * with double* .

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

Among them, in the return value part of the function, we need to put two variables with data types of double** and double* together based on the make_pair() function to form the pair structure, and then return.

Secondly, the above pair, make_pair(), etc. are all defined in the std namespace, so this namespace needs to be declared first; again, before calling this function, we first need to define a pair <double** , a variable of type double*> and assign the return value of the function to it.

Then, through the .first and .second commands, the two variables in pair<double**, double*> are obtained respectively.

  At this point, we have implemented a method of returning two return values ​​through a C++ function.

Two, tuple<>

If you need to return three or more return values, you can implement it using a method similar to the above operation based on the tuple data structure.

  1. Reference header file

#include <tuple>

2. Tuple initialization

The initialization of std::tuple can be achieved through the constructor.

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

This initialization method requires defining the data type of each element, which is relatively cumbersome. C++11 also provides another method std::make_tuple.

3. std::make_tuple

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

This initialization method avoids the problem of specifying element types one by one and automatically realizes the derivation of each element type.

Complete example one:

#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;
}

Output:

<strong>int</strong> value = 7

<strong>double</strong> value = 9.8

string value = text

Complete example two:

#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";
}

Output:

The value of t is (10, Test, 3.14, 7, 1)

5 7

Guess you like

Origin blog.csdn.net/qq_45577269/article/details/129650027