#include iostream#include vector#include stack#include queue#include list#include map#include algorithmusing namespace std;class Dogpublic Dogstring na int age = 0 m_name = na; m_age = age; bool o
解释每一行
#include <iostream>: 包含输入输出流库,可以使用输入输出操作。#include <vector>: 包含向量库,可以使用向量数据结构。#include <stack>: 包含栈库,可以使用栈数据结构。#include <queue>: 包含队列库,可以使用队列数据结构。#include <list>: 包含链表库,可以使用链表数据结构。#include <map>: 包含映射库,可以使用映射数据结构。#include <algorithm>: 包含算法库,可以使用算法函数。using namespace std;: 使用标准命名空间,避免使用std::前缀。class Dog: 定义一个类Dog。Dog(string na, int age = 0): 类Dog的构造函数,用于创建一个Dog对象。接受一个字符串参数na和一个整数参数age,age默认为0。void show(): 类Dog的成员函数,用于显示Dog对象的信息。private:: 类Dog的私有成员变量。string m_name;: 类Dog的私有成员变量,表示狗的名字。int m_age;: 类Dog的私有成员变量,表示狗的年龄。friend bool cmp_name(const Dog &dg1, const Dog &dg2);: 申明一个友元函数cmp_name,用于比较两个Dog对象的名字大小。bool cmp_name(const Dog &dg1, const Dog &dg2): 定义一个全局函数cmp_name,用于比较两个Dog对象的名字大小。list<Dog> lst;: 创建一个Dog对象的链表。Dog wc("wc", 1);: 创建一个名为wc,年龄为1的Dog对象。Dog ah("ah", 5);: 创建一个名为ah,年龄为5的Dog对象。Dog lf("lf", 2);: 创建一个名为lf,年龄为2的Dog对象。Dog xhh("xhh", 4);: 创建一个名为xhh,年龄为4的Dog对象。lst.push_back(wc);: 在链表末尾插入wc对象。lst.push_front(ah);: 在链表头部插入ah对象。lst.push_front(lf);: 在链表头部插入lf对象。lst.push_front(xhh);: 在链表头部插入xhh对象。list<Dog>::iterator iter = lst.begin();: 创建一个链表迭代器iter,并将其指向链表的起始位置。lst.sort(cmp_name);: 使用cmp_name函数对链表进行排序。for(iter = lst.begin(); iter != lst.end(); iter++) {: 循环遍历链表中的每个元素。iter->show();: 调用show函数显示当前元素的信息。return 0;: 程序正常结束,返回0
原文地址: https://www.cveoy.top/t/topic/iOjV 著作权归作者所有。请勿转载和采集!