在 C++ 中从函数中返回多个值
    
    Jinku Hu
    2023年10月12日
    
    C++
    C++ Function
    
 
本文将介绍如何从 C++ 函数中返回多个值的多种方法。
使用 struct 从 C++ 中的函数返回多个值
    
可以使用自定义的 struct 变量从函数返回多个值。即,我们演示一个示例,该示例实现了两个数据成员结构以从函数中返回 int 对,该对在整数 vector 中搜索最大值/最小值。注意,可以定义任何类型的 struct 来满足他们的需求,并扩展数据成员的数量。在这种情况下,我们在 findMaxMin 函数作用域中声明本地 struct 变量,然后按值将其返回到 main 例程。struct 变量也可以在 main 函数中初始化,并通过引用传递到 findMaxMin 以将结果值存储在那里。
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::pair;
using std::string;
using std::vector;
struct IntPair {
  int first, second;
} typedef IntPair;
IntPair findMaxMin(vector<int> &arr) {
  IntPair ret;
  int max = arr[0];
  int min = arr[0];
  for (const auto &item : arr) {
    if (item > max) max = item;
    if (item < min) min = item;
  }
  ret.first = max;
  ret.second = min;
  return ret;
}
int main() {
  vector<int> array = {1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
                       11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
  auto ret = findMaxMin(array);
  cout << "Maximum element is " << ret.first << ", Minimum - " << ret.second
       << endl;
  return EXIT_SUCCESS;
}
输出:
Maximum element is 20, Minimum - 1
在 C++ 中使用 std::pair 从一个函数返回多个值
C++ 标准库提供了 std::pair,它可以像一对一样存储两个异质对象。因此,我们可以重新实现前面的示例,而无需将我们的自定义 struct 定义为 std::pair 已经内置了广泛的功能。缺点之一是,如果我们不在其中存储其他一些多元素结构,则只能返回两个值。请注意,我们分别将 max/min 查找算法与 STL 交换-std::max_element 和 std::min_element。
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::pair;
using std::string;
using std::vector;
std::pair<int, int> findMaxMin2(vector<int> &arr) {
  std::pair<int, int> ret;
  auto max = std::max_element(arr.begin(), arr.end());
  auto min = std::min_element(arr.begin(), arr.end());
  ret.first = arr.at(std::distance(arr.begin(), max));
  ret.second = arr.at(std::distance(arr.begin(), min));
  return ret;
}
int main() {
  vector<int> array = {1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
                       11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
  auto ret = findMaxMin2(array);
  cout << "Maximum element is " << ret.first << ", Minimum - " << ret.second
       << endl;
  return EXIT_SUCCESS;
}
输出:
Maximum element is 20, Minimum - 1
在 C++ 中使用数组从一个函数返回多个值
另外,我们可以声明一个 C 风格的数组来存储和从函数返回多个值。此方法为使用大量值提供了更直接的界面。在调用方函数中声明数组并将其地址传递给被调用方,效率更高。这样,即使只使用一个指针也可以传递巨大的数组,而不会增加开销。
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::pair;
using std::string;
using std::vector;
int *findMaxMin2(vector<int> &arr, int ret[]) {
  auto max = std::max_element(arr.begin(), arr.end());
  auto min = std::min_element(arr.begin(), arr.end());
  ret[0] = arr.at(std::distance(arr.begin(), max));
  ret[1] = arr.at(std::distance(arr.begin(), min));
  return ret;
}
int main() {
  vector<int> array = {1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
                       11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
  int ret_array[2];
  auto ret = findMaxMin2(array, ret_array);
  cout << "Maximum element is " << ret[0] << ", Minimum - " << ret[1] << endl;
  return EXIT_SUCCESS;
}
输出:
Maximum element is 20, Minimum - 1
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Jinku Hu
    
