tuple通用工具库学习

tuple直译为元组,他是固定大小的异类值的汇集,通常如果我们要传的参数或函数返回值都是单类型的,如果实在要返回多类型的就会把它封装成类或结构点,比如坐标Point(x,y),而如果不用这种方式那么tuple就可以做到返回多个值的方式,下面介绍tuple的一些基本使用。

1.成员函数

(构造函数)

(C++11)

构造新的 tuple
(公开成员函数)

operator=

(C++11)

赋值一个 tuple 的内容给另一个
(公开成员函数)

swap

(C++11)

交换两个 tuple 的内容
(公开成员函数)
#include <iostream>
#include <tuple>
#include <string>
#include <stdexcept>

using namespace std;

int main()
{
    //1.构造函数
    std::tuple<string, int, float> tup("Kuple", 35, 175.6);
    cout << "name=========================" << get<0>(tup) << endl;
    cout << "age==========================" << get<1>(tup) << endl;
    cout << "height=======================" << get<2>(tup) << endl;
    //2.赋值操作
    std::tuple<string, int, float> tup2;
    tup2 = tup;
    cout << "name==========================" << get<0>(tup) << endl;
    cout << "age===========================" << get<1>(tup) << endl;
    cout << "height========================" << get<2>(tup) << endl;
    cout << "name2=========================" << get<0>(tup2) << endl;
    cout << "age2==========================" << get<1>(tup2) << endl;
    cout << "height2=======================" << get<2>(tup2) << endl;
    //3.交换函数
    std::tuple<string, int, float> tup3("Camel", 32, 185.2);
    cout << "name=== " << get<0>(tup) << " age=== " << get<1>(tup) << " height=== " << get<2>(tup)<< endl;
    cout << "name3== " << get<0>(tup3) << " age3== " << get<1>(tup3) << " height3== " << get<2>(tup3)<< endl;
    tup.swap(tup3);
    cout << "tup.swap(tup3)========" << endl;
    cout << "name=== " << get<0>(tup) << " age=== " << get<1>(tup) << " height=== " << get<2>(tup)<< endl;
    cout << "name3== " << get<0>(tup3) << " age3== " << get<1>(tup3) << " height3== " << get<2>(tup3)<< endl;
    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

 2.非成员函数

make_tuple

创建一个 tuple 对象,其类型根据各实参类型定义
(函数模板)

tie

创建左值引用的 tuple,或将 tuple 解包为独立对象
(函数模板)

forward_as_tuple

创建转发引用tuple
(函数模板)

tuple_cat

通过连接任意数量的元组来创建一个tuple
(函数模板)

std::get(std::tuple)

元组式访问指定的元素
(函数模板)
#include <iostream>
#include <tuple>
#include <string>
#include <stdexcept>
#include <map>

using namespace std;
// name, age, height
std::tuple<string, int, float> getStudent(int id)
{
    if(id == 1001)
        return std::make_tuple("scott", 25, 174.3);
    if(id == 1002)
        return std::make_tuple("camel", 26, 180.4);
    if(id == 1003)
        return std::make_tuple("tom", 28, 178.5);
    throw std::invalid_argument("id");
}

// 打印任何大小 tuple 的帮助函数
template<class Tuple, std::size_t N>
struct TuplePrinter {
    static void print(const Tuple& t)
    {
        TuplePrinter<Tuple, N-1>::print(t);
        std::cout << ", " << std::get<N-1>(t);
    }
};

template<class Tuple>
struct TuplePrinter<Tuple, 1> {
    static void print(const Tuple& t)
    {
        std::cout << std::get<0>(t);
    }
};

template<class... Args>
void print(const std::tuple<Args...>& t)
{
    std::cout << "(";
    TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
    std::cout << ")\n";
}
// 结束帮助函数

int main()
{
    auto tup1 = getStudent(1001);
    cout << "name1=== " << get<0>(tup1) << " age1=== " << get<1>(tup1) << " height1=== " << get<2>(tup1)<< endl;
    auto tup2 = getStudent(1002);
    cout << "name2=== " << get<0>(tup2) << " age2=== " << get<1>(tup2) << " height2=== " << get<2>(tup2)<< endl;

    string name = "kuple";
    float height = 167.8;
    int age = 26;
    cout << "name==== " << name << " age==== " << age << " height==== " << height<< endl;
    std::tie(name, age, height) = tup1;
    cout << "name==== " << name << " age==== " << age << " height==== " << height<< endl;

    std::map<int, std::string> m;
    m.emplace(std::piecewise_construct, std::forward_as_tuple(10), std::forward_as_tuple(20, 'b'));
    std::cout << "m[10]===" << m[10] << endl;

    std::tuple<string, int, float> tup3("Blue", 25, 167.8);
    int n = 7;
    auto tup = std::tuple_cat(tup3, std::make_tuple("Foo", "bar"), tup3, std::tie(n));
    n = 10;
    print(tup);
    cout << "Hello World!" << endl;

    return 0;
}

运行结果:

 

参考:

标准库头文件 <tuple> - cppreference.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值