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

1. pair的定义和结构

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

成员访问 :测试对firstsecond成员变量的使用能力。

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


2. pair的嵌套

多层嵌套结构 :考察pair与其他容器(如vectormap)或自身嵌套的能力(如 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容器结合pairmapvector等容器的联合使用(如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. 声明一个存储intstringpair对象:
    答案: 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;
}
相关推荐
天赐学c语言17 分钟前
list常用接口及模拟实现
数据结构·c++·list
无名之逆36 分钟前
探索 Rust 高效 Web 开发:Hyperlane 框架深度解析
开发语言·后端·算法·面试·rust
轩宇^_^1 小时前
C++ 布尔类型(bool)深度解析
开发语言·c++
AI技术控1 小时前
计算机视觉算法实战——遗留物检测检测(主页有源码)
人工智能·算法·计算机视觉
AI技术控1 小时前
计算机视觉算法实战——花卉识别(主页有源码)
人工智能·算法·计算机视觉
最爱で毛毛熊1 小时前
VSCode C/C++环境搭建指南
c语言·c++·vscode
alicema11112 小时前
Python+Django网页前后端rsp云端摄像头人数监控系统
开发语言·网络·后端·python·神经网络·算法·django
曹天骄2 小时前
NAT 和 IP 直接通信的区别
服务器·网络协议·tcp/ip
Danta2 小时前
HTTP协议版本演进:从HTTP/0.9到HTTP/3的高分面试回答
前端·网络协议·面试
Dream it possible!2 小时前
LeetCode 热题 100_前 K 个高频元素(73_347_中等_C++)(堆)(哈希表+排序;哈希表+优先队列(小根堆))
数据结构·c++·leetcode·散列表