【3-14 STC-pair超级详细的解说】

1. pair的定义和结构

• 基础概念 :考察对std::pair模板类的理解,包括其头文件(<utility>)和基本语法(pair<T1, T2>)。

• 成员访问 :测试对first和second成员变量的使用能力。

• 构造方式 :如何通过构造函数或make_pair()函数创建pair对象(如 pair<int, string> p(1, "test"))。


2. pair的嵌套

• 多层嵌套结构 :考察pair与其他容器(如vector、map)或自身嵌套的能力(如 pair<int, pair<string, float>>)。

• 复杂数据组织 :可能涉及在嵌套pair中访问多层数据(如 p.second.first)。

• 实际应用场景 :例如用pair存储坐标点(pair<int, int>)或键值对的组合(pair<string, map<int, float>>)。


3. pair的自带排序规则

• 默认比较逻辑 :理解pair的默认排序规则(先按first升序,若相等再按second升序)。

• 自定义排序 :在需要打破默认规则时(如降序排序),如何通过自定义比较函数或Lambda表达式实现。

• 结合STL算法 :例如在sort()函数中使用pair的排序特性处理容器数据。


考试题型推测

  1. 选择题/填空题 :考察pair的定义、成员变量、默认行为等基础知识点。
  2. 简答题 :解释pair的排序规则或嵌套应用场景。
  3. 编程题 :完成基于pair的算法实现或数据操作。
  4. 代码分析题 :阅读并改进包含pair的代码逻辑。

重点应用方向

• STL容器结合 :pair与map、vector等容器的联合使用(如map的键值对本质是pair)。

• 算法优化 :利用pair简化多属性数据的比较和排序逻辑。

• 工程实践 :在数据结构设计中灵活使用pair嵌套(如树节点、图边权重的组合存储)。

一些测试题


一、选择题

  1. std::pair 的头文件是?

    A. <algorithm>

    B. <utility>

    C. <vector>

    D. <map>
    答案:B

  2. 如何访问pair对象的第二个成员?

    A. p.first

    B. p.second

    C. p.value

    D. p.key
    答案:B

  3. pair的默认排序规则是?

    A. 先按second升序,再按first升序

    B. 先按first升序,再按second升序

    C. 按内存地址排序

    D. 无序
    答案:B


二、填空题

  1. 声明一个存储int和string的pair对象:
    答案: pair<int, string> p;

  2. 通过函数创建pair对象的语法:auto p = ___________(3, "hello");
    答案: make_pair

  3. 访问嵌套pair的示例:pair<int, pair<char, float>> p;,要获取char值应写为:________
    答案: p.second.first


三、简答题

  1. 简述pair的默认排序规则,并举例说明。
    答案:

    默认按first成员升序排序,若first相等则按second升序。

    例如:pair<int, int>(2,3)会排在pair<int, int>(2,5)之前。

  2. 如何对vector<pair<int, string>>按first降序排序?写出代码片段。
    答案:

    cpp 复制代码
    sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {
        return a.first > b.first;
    });

四、编程题

  1. 题目: 定义一个嵌套pair结构pair<string, pair<int, float>>,表示学生姓名、年龄和成绩。创建包含3个此类对象的vector,并按年龄升序排序后输出。
    参考答案:

    cpp 复制代码
    #include <iostream>
    #include <utility>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        vector<pair<string, pair<int, float>>> students = {
            {"Alice", {20, 88.5}},
            {"Bob", {18, 90.0}},
            {"Charlie", {22, 85.0}}
        };
    
        sort(students.begin(), students.end(), [](const auto& a, const auto& b) {
            return a.second.first < b.second.first;
        });
    
        for (const auto& s : students) {
            cout << s.first << ": Age=" << s.second.first << ", Score=" << s.second.second << endl;
        }
        return 0;
    }

机器人的代码看不太懂

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int main() {
	//pair<string,pair<int,float>>stu;
	vector<pair<string,pair<int,float>> >stu;
	stu.push_back({"xiaoMing",{23,89.9}});
	stu.push_back({"xiaoHong",{21,88.5}});
		stu.push_back({"xiaoTong",{28,98.5}});

auto compare = [](const pair<string, pair<int,     float>>& a, const pair<string, pair<int, float>>& b) {
        return a.second.first < b.second.first; // 比较年龄
    };
    // 使用sort函数进行排序
    sort(stu.begin(), stu.end(), compare);
    
    
    for (const auto& student : stu) {
        cout << "Name: " << student.first << ", Age: " << student.second.first << ", Score: " << student.second.second << endl;
    }
    return 0;
    
}
  1. 题目: 使用pair存储坐标点(x, y),对vector<pair<int, int>>按x升序排序,若x相同则按y降序排序。
    参考答案:

    cpp 复制代码
    sort(coords.begin(), coords.end(), [](const auto& a, const auto& b) {
        if (a.first == b.first) return a.second > b.second;
        return a.first < b.first;
    });

五、纠错题

找出以下代码的错误并修正:

cpp 复制代码
pair<int, string> p = {5, "test"};
cout << p.second() << endl;  // 试图输出second成员

答案:

错误:second是成员变量而非函数,应去掉()。

修正:cout << p.second << endl;

这个代码也不错

c 复制代码
#include <iostream>
#include <utility>
#include <vector>

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

int main() {
    // 创建一个存储Person对象的向量
    std::vector<Person> people;
    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;
    scores.push_back({people[0], 98});
    scores.push_back({people[1], 85});
    scores.push_back({people[2], 95});

    // 遍历输出每个pair的内容
    for (const auto& entry : scores) {
        std::cout << "Name: " << entry.first.name 
                  << ", Age: " << entry.first.age 
                  << ", Score: " << entry.second << std::endl;
    }
    return 0;
}
相关推荐
2601_962218611 小时前
C++中decltype关键字的实现
开发语言·c++
Discipline10291 小时前
尼泊尔河流流量:天气驱动的预测与高流量风险
人工智能·算法
繁华的地方不一定留下你的脚印1 小时前
C++ std::variant 与 std::visit:安全保存多种类型,写清每个处理分支
开发语言·c++
XuCoder1 小时前
改一个数,右边全得重算,这题怎么扛住两万次查询
算法
小羊没烦恼!2 小时前
在Scrum中实施敏捷建模
java·开发语言·windows·算法·c#
程序员老陆2 小时前
WIN32_LEAN_AND_MEAN:Windows 头文件里的“精简编译开关”
c++·windows
weixin_307779132 小时前
基于睿擎工业开发平台的预训练视觉模型轻量化适配与低代码部署优化
开发语言·算法
2501_937860942 小时前
下篇:网络层、数据链路层与应用层:IP、NAT、ARP、DNS 一网打尽
网络·网络协议·tcp/ip
hold?fish:palm2 小时前
45 二叉树的右视图
开发语言·c++·算法
粤鼎恒业3 小时前
惠州工厂电子料回收厂家推荐:从交接单反推筛选标准
大数据·数据库·算法·硬件架构·硬件工程·pcb工艺·材料工程