蓝桥杯基础知识6 pair

蓝桥杯基础知识6 pair

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

pair类 的定义:

cpp 复制代码
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 类,可以方便地将两个值组合在一起,并进行传递、存储 和 操作。

cpp 复制代码
#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 对象的成员。

cpp 复制代码
#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 成员升序排序。

cpp 复制代码
#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对象。

cpp 复制代码
#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博客

相关推荐
樱木Plus1 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit3 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_4 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星4 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛6 天前
delete又未完全delete
c++
端平入洛7 天前
auto有时不auto
c++
哇哈哈20218 天前
信号量和信号
linux·c++
多恩Stone8 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马8 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝8 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode