C++新特性unordered_map和tuple

#include <iostream>
#include <map>
#include <unordered_map>

#include <tuple>

void test1()
{
    /* map、mutimap都是安装key进行排序的容器,内部实现是红黑树
        插入和搜索的时间复杂度是O(log(size)),区别是map中key不可重复
    */
    std::map<int, std::string> map1 = {
        {1, "1"},
        {3, "3"},
        {2, "2"},
    };

    for (auto &it : map1)
    {
        std::cout << it.first << ":" << it.second << std::endl;
    }
    // 无序map
    std::unordered_map<int, std::string> unmap = {
        {1, "1"},
        {3, "3"},
        {2, "2"},
    };
    for (auto &it : unmap)
    {
        std::cout << it.first << ":" << it.second << std::endl;
    }
}


void test2()
{
    /*元组三要素:除了自定义结构,tuple可以存储不同类型的数据
        1.std::make_tuple:构造元组
        2.std::get<index>/<type>:根据位置或类型获取元组元素
        3.std::tie:元组拆分,缺点需要事先知道元素的个数和对应的具体类型
    */
    std::string nm;
    int age;
    double grade;
    auto tp = std::make_tuple("zhangsan", 10, 99.5);
    // 位置获取元素
    std::cout<<std::get<0>(tp)<<std::endl;
    std::cout<<std::get<1>(tp)<<std::endl;

    // 也可以通过类型获取元素
    std::cout<<std::get<double>(tp)<<std::endl;

    // 元组拆包
    std::tie(nm, age, grade) = std::make_tuple("lisi", 20, 60.5);
    std::cout<<nm<<","<<age<<"," <<grade<<std::endl;

}
int main()
{
    test1();
    test2();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值