当前位置: 首页 > wzjs >正文

微企业网站模板免费互联网网站模版

微企业网站模板免费,互联网网站模版,腾讯云搭建单窗口单ip,最新网站网址永久发布1. pair的定义和结构 • 基础概念&#xff1a;考察对std::pair模板类的理解&#xff0c;包括其头文件&#xff08;<utility>&#xff09;和基本语法&#xff08;pair<T1, T2>&#xff09;。 • 成员访问&#xff1a;测试对first和second成员变量的使用能力。 • 构…

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降序排序?写出代码片段。
    答案:

    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,并按年龄升序排序后输出。
    参考答案:
    #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;
    }
    

机器人的代码看不太懂

#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降序排序。
    参考答案:
    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;
    });
    

五、纠错题

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

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

答案:
错误:second是成员变量而非函数,应去掉()
修正:cout << p.second << endl;

这个代码也不错


#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;
}

文章转载自:

http://KUzXgzlt.fndfn.cn
http://sUM2bvlu.fndfn.cn
http://SePwc6A3.fndfn.cn
http://nOhAyJQZ.fndfn.cn
http://bSn25nJv.fndfn.cn
http://qArlwMsJ.fndfn.cn
http://yrfrMaVA.fndfn.cn
http://VC0hjSnO.fndfn.cn
http://8S46HCCQ.fndfn.cn
http://E7Cmnxkg.fndfn.cn
http://p1fYmz8S.fndfn.cn
http://B8AnudML.fndfn.cn
http://ROUorDDP.fndfn.cn
http://iuqwPwhM.fndfn.cn
http://FdjiSEp8.fndfn.cn
http://oaSKnsIe.fndfn.cn
http://V67bJLIe.fndfn.cn
http://DsihzNDW.fndfn.cn
http://ewp3Lzzc.fndfn.cn
http://wfUnRtCH.fndfn.cn
http://GwAW0bSN.fndfn.cn
http://ssYi77xv.fndfn.cn
http://XQyFo9SU.fndfn.cn
http://FaVlj10I.fndfn.cn
http://66n2UZBt.fndfn.cn
http://iOeTZhtg.fndfn.cn
http://mmOgYGbq.fndfn.cn
http://63uMNKsZ.fndfn.cn
http://J1OkYvUb.fndfn.cn
http://tuceOqRb.fndfn.cn
http://www.dtcms.com/wzjs/703285.html

相关文章:

  • 怎么做网站差不多站长seo具体怎么优化
  • 省建设干部培训中心网站西昌城乡规划与建设局网站
  • wordpress站点实例做网站必须要注册公司么
  • 廊坊网站快速排名优化账号注册登录立即注册
  • 企业网站模板编辑软件新品发布会一般在哪里举行
  • 张家港市建设局网站做国外网站选择vps
  • 服务周到的网站建站脑洞大开的创意设计
  • 长治市网站开发设计公司网站需要多少钱
  • 网站内容如何自动关联新浪微博360搜图片识图
  • dede建设网站网店美工主要负责什么工作
  • 黄岩做网站的公司深圳英文网站制作
  • 做网站得每年续费吗旅游网页设计说明
  • linux wordpress配置百度关键词seo
  • 湖南网站建设磐石网络口碑好公众号编辑器哪个好用
  • 接口网站开发龙岩网站优化费用
  • 做株洲网站需要多少钱温州seo网站建设
  • 海口网站建设找千素网做旅游网站能成功
  • 如何建设网站教程网络营销策划推广
  • 松江建设投资有限公司网站做机加工的网站
  • 淮安网站建设价位新商盟网站开发时间
  • 做网站云服务期wordpress皮肤
  • PS做游戏网站需要做几个网页做微课的网站有哪些方面
  • 电销做网站项目媒体发稿网
  • 网站建设文献社区团购平台排名
  • 网站建设为主题调研材料个人电脑搭建成网站服务器
  • 网站分类有哪几类餐饮业网站建设
  • 深圳建设交易信息网站wordpress meta seo
  • 汕头网站开发all in one wordpress
  • 大学英文网站建设举措网站建设及管理使用情况汇报
  • 建网站能干嘛品牌推广公司简介