对元组使用函数

C++11中,STL添加了std::tuple,这种类型可以用来将多个不同类型的值捆绑在一起。元组这种类型已经存在与很多编程语言中,本书的一些章节已经在使用这种类型,这种类型的用途很广泛。

不过,我们有时会将一些值捆绑在一个元组中,然后我们需要调用函数来获取其中每一个元素。对于元素的解包的代码看起来非常的冗长(并且易于出错)。其冗长的方式类似这样:func(get<0>(tup), get<1>(tup), get<2>(tup), ...);

本节中,你将了解如何使用一种优雅地方式对元组进行打包和解包。调用函数时,你无需对元组特别地了解。

How to do it…

我们将实现一个程序,其能对元组值进行打包和解包。然后,我们将看到在不了解元组中元素的情况下,如何使用元组:

  1. 包含必要的头文件,并声明所使用的命名空间:

    1. #include <iostream>
    2. #include <iomanip>
    3. #include <tuple>
    4. #include <functional>
    5. #include <string>
    6. #include <list>
    7. using namespace std;
  2. 首先定义一个函数,这个函数能接受多个参数,其描述的是一个学生,并将学生的相关信息进行打印。其和C风格的函数看起来差不多:

    1. static void print_student(size_t id, const string &name, double gpa)
    2. {
    3. cout << "Student " << quoted(name)
    4. << ", ID: " << id
    5. << ", GPA: " << gpa << '\n';
    6. }
  3. 主函数中,将对一种元组类型进行别名,然后将具体学生的信息填入到这种类型的实例中:

    1. int main()
    2. {
    3. using student = tuple<size_t, string, double>;
    4. student john {123, "John Doe"s, 3.7};
  4. 为了打印这种类型的实例,我们将会对元组中的元素进行分解,然后调用print_student函数将这些值分别进行打印:

    1. {
    2. const auto &[id, name, gpa] = john;
    3. print_student(id, name, gpa);
    4. }
    5. cout << "-----\n";
  5. 然后,我们来创建一个以元组为基础类型的多个学生:

    1. auto arguments_for_later = {
    2. make_tuple(234, "John Doe"s, 3.7),
    3. make_tuple(345, "Billy Foo"s, 4.0),
    4. make_tuple(456, "Cathy Bar"s, 3.5),
    5. };
  6. 这里,我们依旧可以通过对元素进行分解,然后对其进行打印。当要写这样的代码时,我们需要在函数接口变化时,对代码进行重构:

    1. for (const auto &[id, name, gpa] : arguments_for_later) {
    2. print_student(id, name, gpa);
    3. }
    4. cout << "-----\n";
  7. 当然可以做的更好,我们无需知道print_student的参数的个数,或学生元组中元素的个数,我们使用std::apply对直接将元组应用于函数。这个函数能够接受一个函数指针或一个函数对象和一个元组,然后会将元组进行解包,然后与函数参数进行对应,并传入函数:

    1. apply(print_student, john);
    2. cout << "-----\n";
  8. 循环中可以这样用:

    1. for (const auto &args : arguments_for_later) {
    2. apply(print_student, args);
    3. }
    4. cout << "-----\n";
    5. }
  9. 编译并运行程序,我们就能得到如下的输出:

    1. $ ./apply_functions_on_tuples
    2. Student "John Doe", ID: 123, GPA: 3.7
    3. -----
    4. Student "John Doe", ID: 234, GPA: 3.7
    5. Student "Billy Foo", ID: 345, GPA: 4
    6. Student "Cathy Bar", ID: 456, GPA: 3.5
    7. -----
    8. Student "John Doe", ID: 123, GPA: 3.7
    9. -----
    10. Student "John Doe", ID: 234, GPA: 3.7
    11. Student "Billy Foo", ID: 345, GPA: 4
    12. Student "Cathy Bar", ID: 456, GPA: 3.5
    13. -----

How it works…

std::apply是一个编译时辅助函数,可以帮助我们处理不确定的类型参数。

试想,我们有一个元组t,其有元素(123, "abc"s, 456.0)。那么这个元组的类型为tuple<int, string, double>。另外,有一个函数f的签名为int f(int, string, double)(参数类型也可以为引用)。

然后,我们就可以这样调用函数x = apply(f, t),其和x = f(123, "abc"s, 456.0)等价。apply方法还是会返回f的返回值。