蓝桥杯基础知识6 pair

蓝桥杯基础知识6 pair

pair 的定义和结构:在C++中,pair是一个模板类,用于表示一对值的组合,头文件<utility>。

pair类 的定义:

template<class T1, class T2>
struct pair{
    T1 first;    // 第一个值
    T2 second;    // 第二个值

    // 构造函数
    pair();
    pair(const T1& x, const T2& y);

    // 比较运算符重载
    bool operator == (const pair& rhs) const;
    bool operator == (const pair& rhs) const;

    // 其他成员函数和特性
    // ...
};    

 

pair 类模板 有两个模板参数T1 和 T2,分别表示第一个值 和 第二个值的类型;

有两个成员变量,fist 和 second,分别表示 第一个值 和 第二个值;。

pair 类有一些成员函数 和 特性,例如默认构造函数、带参数的构造函数、比较运算符重载等。

使用pair 类,可以方便地将两个值组合在一起,并进行传递、存储 和 操作。

#include<iostream>
#include<utility>

int main(){
    std::pair<int, double> p1(1, 3.14);
    std::pair<char, std::string> p2('a', "hello");

    std::cout << p1.first << "," << p1.second << std::endl;
    std::cout << p2.first << "," << p2.second << std::endl;

    return 0;
}    

创建两个pair 对象,分别包含不同类型的值,分别访问并输出对象 p1 、p2 的 first 和 second 成员变量的值。

pair 的嵌套:将一个 pair 对象作为另一个 pair 对象的成员。

#include<iostream>
#include<utility>

int main(){
    std::pair<int, int> p1(1, 2);
    std::pair<int, std::pair<int, int>> p2(3, std::make_pair(4, 5));
    std::pair<std::pair<int, int>, std::pair<int, int>> p3(std::make_pair(6, 7), std::make_pair(8, 9));
    
    std::cout << p1.first << "," << p1.second << std::endl;
    std::cout << p2.first << "," << p2.second.first << p2.second.second << std::endl;
    std::cout << p3.first .first <<"," << p3.first.second << "," << p3.second.first << p3.second.second << std::endl;

    return 0; 
}

pair自带的排序规则:按照first成员升序排序,若first成员相等,则按照second 成员升序排序。

#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>

int main(){
    std::vector<std::pair<int, int>> vec;
    vec.push_back(std::make_pair(3, 2));
    vec.push_back(std::make_pair(1, 4));
    vec.push_back(std::make_pair(2, 1));

    std::sort(vec.begin(), vec.end());

    for(const auto& p : vec){
        std::cout << p.first << "," << p.second << std::endl;
    }    // 1,4  2,1  3,2
    
    return 0;

}

 

创建一个存储 pair 对象的向量 vec,包含三个pair对象。

#include<iostream>
#include<utility>
#include<vector>

// 定义一个结构体,表示一个人的信息
struct Person{
    std::string name;
    int age;
};

int main(){
    // 创建一个存储Person对象的向量
    std::vector<Person> people;

    // 添加一些Person对象到向量中
    people.push_back({"Alice", 25});
    people.push_back({"Bob", 30});
    people.push_back({"Charlie", 20});

    
    // 创建一个存储pair的向量,每个pair包含一个Person对象和一个评分
    std::vector<std::pair<Person, int>> scores;

    // 添加一些pair到向量中
    scores.push_back({people[0], 90});
    scores.push_back({people[1], 85});
    scores.push_back({people[2], 95});

    // 遍历pair向量,并输出每个人的姓名、年龄 和 评分
    for(const auto& pair : scores){
        std::cout << "Name: " << pair.first.name << std::endl;
        std::cout << "Age: " << pair.first.age << std::endl;
        std::cout << "Score: " << pair.second << std::endl;
    }

    return 0;

}

C++ 在线工具 | 菜鸟工具 (jyshare.com)

Name: Alice
Age: 25
Score: 90
Name: Bob
Age: 30
Score: 85
Name: Charlie
Age: 20
Score: 95

可拓展学习:

C++ pair的基本用法总结(整理)_c++ pair用法-CSDN博客

C++中push_back()函数_pushback函数-CSDN博客

  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值