c++小学期的实验二

2024-06-07 14:32
文章标签 c++ 实验 小学

本文主要是介绍c++小学期的实验二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  实验一是做一个猜单词的游戏,bug太多,所以就不贴出来了。。。

  实验二的题目是:

1.完成图形类及其派生类的定义

(1) 设计一个计算图形面积的类库。它的顶层是一个抽象类,并且提供三个纯虚函数:显示数据成员、返回面积和返回体积。

class Shape

{public:

virtual void showData()=0;

        virtualdouble reArea()=0;

        virtualdouble reVolume()=0;

};

    第二层有Shape类派生TwoDimShape(二维图形)和ThreeShape(三维图形)类。他们增加了有关的数据成员,但没有成员函数的实现。

第三层派生具体的图形类。TwoDimShape类派生Circle(圆)、Elipse(椭圆)、Rectangle(矩形)和Triangle(三角形)等类。ThreeShape类派生Ball(球体)、Cylider(圆柱体)和RectangularParallelepiped(长方体)等类。

(2) 设计一个测试程序,全面测试你的设计,要求使用基类指针引用派生类对象的三个函数。

2.用异质链表组织上述各类图形对象。

(1) 实现单链表:将结点数据域定义为int,链表具有插入结点、删除结点、反转、遍历等功能,并测试其正确性。

(2) 将结点数据域定义修改为Shape *,用链表中的每个结点表示一个具体的图形对象,创建图形对象链表,并测试其正确性。

(3) 将所建单链表中的内容写到一个文本文件中,反转单链表并再次将其内容追加到同一文本文件中,查阅其正确性。


 代码有点累赘,用了许多if,有需要的话可以改成switch。另外可以实现保存链表到txt文本,从txt文本导入链表。虽然质量不高,bug也很多,大家就多多担待吧大笑

  另外代码在cfree上编译通过的,推荐大家用cfree或mingw运行。

#include<iostream>
#include<windows.h>
#include<fstream>
using namespace std; 
class Shape
{public:virtual void showData()=0;virtual double reArea()=0;virtual double reVolume()=0;virtual void saveData()=0;virtual int reflag()=0;Shape *next;
};
class TwoDimShape:public Shape
{public:double area;virtual void showData(){}virtual void saveData(){}virtual double reArea(){return 0;}virtual int reflag(){return 0;}
};
class ThreeShape:public Shape
{public:double area;double volume;virtual void showData(){}virtual void saveData(){}virtual double reArea(){return 0;}virtual double reVolume(){return 0;}virtual int reflag(){return 0;}
};
//二维派生
class Circle:public TwoDimShape//圆类
{private:double data_1;double data_2;double data_3;int flag;//area=3.14*radius*radius;public: double reVolume(){return 0;}Circle(double r){data_1=r;}void showData(){cout<<"圆的半径是: "<<data_1<<endl;}double reArea(){area=3.14*data_1*data_1;cout<<"圆的面积是:"<<area<<endl<<endl;}void saveData(){fstream save;save.open("E:\\game\\list.txt",ios::app|ios::out); save<<"0"<<endl;save<<"1"<<endl;save<<data_1<<endl;save<<area<<endl; }int reflag(){flag=1;return flag;}
};
class Elipse:public TwoDimShape//椭圆类
{private:double data_1;double data_2;double data_3;int flag;public:double reVolume(){return 0;}Elipse(double a,double m){data_1=a;data_2=m;}void showData(){cout<<"椭圆的长轴是: "<<data_1<<endl;cout<<"椭圆的短轴是: "<<data_2<<endl;}double reArea(){area=(3.14*data_1*data_2)/4;cout<<"椭圆的面积是:"<<area<<endl<<endl;}void saveData(){fstream save;save.open("E:\\game\\list.txt",ios::app|ios::out);save<<"0"<<endl; save<<"2"<<endl;save<<data_1<<endl;save<<data_2<<endl;save<<area<<endl;}int reflag(){flag=2;return flag;}
};
class Rectangles:public TwoDimShape//长方形类
{private:double data_1;double data_2;double data_3;int flag;public:double reVolume(){return 0;}Rectangles(double l,double w){data_1=l;data_2=w; }void showData(){cout<<"长方形的长是:"<<data_1<<endl;cout<<"长方形的宽是:"<<data_2<<endl;}double reArea(){area=data_1*data_2;cout<<"长方形的面积是:"<<area<<endl<<endl;}void saveData(){fstream save;save.open("E:\\game\\list.txt",ios::app|ios::out);save<<"0"<<endl;save<<"3"<<endl;save<<data_1<<endl;save<<data_2<<endl;save<<area<<endl;}int reflag(){flag=3;return flag;}
};
class Triangle:public TwoDimShape//三角形类
{private:double data_1;double data_2;double data_3;int flag;public:double reVolume(){return 0;}Triangle(double b,double h){data_1=b;data_2=h;} void showData(){cout<<"三角形的底是:"<< data_1<<endl;cout<<"三角形的高是:"<< data_2<<endl;}double reArea(){area=(data_1*data_2)/2 ;cout<<"三角形的面积是:"<<area<<endl<<endl; }void saveData(){fstream save;save.open("E:\\game\\list.txt",ios::app|ios::out); save<<"0"<<endl;save<<"4"<<endl;save<<data_1<<endl;save<<data_2<<endl;save<<area<<endl;}int reflag(){flag=4;return flag;}
};
//三维派生
class Ball:public ThreeShape//球类
{private:double data_1;double data_2;double data_3;int flag;public:Ball(double b){data_1=b;} void showData(){cout<<"球的半径是:"<< data_1<<endl;}double reArea(){area=4*data_1*data_1;cout<<"球的面积是:"<< area<<endl;}double reVolume(){volume=(4/3)*data_1*data_1*data_1;cout<<"球的体积是:"<< volume<<endl<<endl;}void saveData(){fstream save;save.open("E:\\game\\list.txt",ios::app|ios::out); save<<"0"<<endl;save<<"5"<<endl;save<< data_1<<endl;save<<area<<endl; save<<area<<endl;}int reflag(){flag=5;return flag;}
};
class Cylider:public ThreeShape//圆柱类
{private:double data_1;double data_2;double data_3;int flag;public:Cylider(double r,double h){data_1=r;data_2=h;} void showData(){cout<<"圆柱的底面半径是:"<< data_1<<endl;cout<<"圆柱的高是:"<< data_2<<endl;}double reArea(){area=(2*3.14*data_1*data_1)+(2*3.14*data_1*data_2);cout<<"圆柱的面积是:"<< area <<endl;}double reVolume(){volume=3.14*data_1*data_1*data_2;cout<<"圆柱的体积是:"<< volume<<endl<<endl;}void saveData(){fstream save;save.open("E:\\game\\list.txt",ios::app|ios::out);save<<"0"<<endl; save<<"6"<<endl;save<<data_1<<endl;save<<data_2<<endl;save<<area<<endl; save<<area<<endl;}int reflag(){flag=6;return flag;}
};
class RectangularParallelepiped:public ThreeShape//长方体类
{private:double data_1;double data_2;double data_3;int flag;public:RectangularParallelepiped(double l,double w,double h) {data_1=l;data_2=w;data_3=h;}void showData(){cout<<"长方体的长是:"<<data_1<<endl;cout<<"长方体的宽是:"<<data_2<<endl; cout<<"长方体的高是:"<<data_3<<endl;}double reArea(){area=2*(data_1*data_2+data_1*data_3+data_2*data_3) ;cout<<"长方体的面积是:"<<area<<endl;} double reVolume(){volume=data_1*data_2*data_3;cout<<"长方体的体积是:"<<volume<<endl<<endl; }void saveData(){fstream save;save.open("E:\\game\\list.txt",ios::app|ios::out);save<<"0"<<endl;save<<"7"<<endl; save<<data_1<<endl;save<<data_2<<endl;save<<data_3<<endl;save<<area<<endl; save<<area<<endl;}int reflag(){flag=7;return flag;}
};void creatlist(Shape * & head,int n)//创建链表
{ int creat_judge=0;int creat_num=0;//创建节点数量Shape *move=NULL;Shape *p=NULL;//用于创建每个链表 for(creat_num=1;creat_num<=n;creat_num++){cout<<"1 建立圆形的链表"<<endl<<"2 建立椭圆形的链表"<<endl<<"3 建立长方形的链表"<<endl<<"4 建立三角形的链表"<<endl<<"5 建立球体的链表"<<endl<<"6 建立圆柱体的链表"<<endl<<"7 建立长方体的链表"<<endl;cout<<"当每个图形输入数据为0时退出!"<<endl;cout<<endl<<"请输入你的选择:";cin>>creat_judge;if(creat_judge==0){cout<<"输入错误,请从新输入:"; cin>> creat_judge;} if(creat_judge==1){double data;cout<<"请输入半径:";cin>>data;if(data==0){return;}p=new Circle(data);p->reArea();}else if(creat_judge==2){double data1;double data2;cout<<"请输入长轴:";cin>>data1;if(data1==0){return;}cout<<"请输入短轴:";cin>>data2;if(data2==0){return;}p=new Elipse(data1,data2);p->reArea();}else if(creat_judge==3){double data1;double data2;cout<<"请输入长:";cin>>data1;if(data1==0){return;}cout<<"请输入宽:";cin>>data2;if(data2==0){return;}p=new Rectangles(data1,data2);p->reArea();}else if(creat_judge==4){double data1;double data2;cout<<"请输入底边:";cin>>data1;if(data1==0){return;}cout<<"请输入高:";cin>>data2;if(data2==0){return;}p=new Triangle(data1,data2);p->reArea();}else if(creat_judge==5){double data1;cout<<"请输入半径:";cin>>data1;if(data1==0){return;}p=new Ball(data1);p->reArea();p->reVolume(); }else if(creat_judge==6){double data1;double data2;cout<<"请输入底面半径:";cin>>data1;if(data1==0){return;}cout<<"请输入高:";cin>>data2;if(data2==0){return;}p=new Cylider(data1,data2);p->reArea();p->reVolume();}else if(creat_judge==7){double data1;double data2;double data3;cout<<"请输入长:";cin>>data1;if(data1==0){return;}cout<<"请输入宽:";cin>>data2;if(data2==0){return;}cout<<"请输入高:";cin>>data3;if(data3==0){return; }p=new RectangularParallelepiped(data1,data2,data3);p->reArea();p->reVolume();}p->next=NULL;if(head==NULL){head=p;move=p;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_BLUE);cout<<'\n'<<"第"<<creat_num<<"个链表创建成功!"<<'\n'<<endl;if(creat_num<n) {cout<<"您现在正在创建第"<<(creat_num)+1<<"个链表"<<'\n'<<endl;} SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);}else{move->next=p;move=p;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_BLUE);cout<<'\n'<<"第"<<creat_num<<"个链表创建成功!"<<'\n'<<endl;if(creat_num<n) {cout<<"您现在正在创建第"<<(creat_num)+1<<"个链表"<<'\n'<<endl;} SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN); }}}void browselist(Shape *head)//遍历链表 
{if(head==NULL){cout<<endl;cout<<"您还没有建立链表呦~"<<endl<<endl;return; } Shape *browse=NULL;browse=head;while(browse!=NULL){browse->showData();browse->reArea();browse->reVolume(); browse=browse->next;}delete browse;
}
void insertlist(Shape * & head)//插入		第n个数据后插入数据 
{if(head==NULL){cout<<endl;cout<<"您还没有建立链表呦~"<<endl<<endl;return; } int insert_num;int select_num;int i; Shape *p=NULL;Shape *judge=head;//判断插入的合法性cout<<"在第__个链表后插入?"; cin>>insert_num;for(i=2;i<=insert_num;i++)//判断插入的合法性{judge=judge->next;}if(judge==NULL){cout<<'\n'<<"不合法的插入!"<<endl;return;}cout<<endl;cout<<"1 插入圆形的链表"<<endl<<"2 插入椭圆形的链表"<<endl<<"3 插入长方形的链表"<<endl<<"4 插入三角形的链表"<<endl<<"5 插入球体的链表"<<endl<<"6 插入圆柱体的链表"<<endl<<"7 插入长方体的链表"<<endl<<"当输入数据为0时退出!"<<endl;cout<<endl<<"请输入你的选择:";cin>>select_num;if(insert_num==0)//如果在头结点插入 {if(select_num==1){int data1;cout<<"请输入半径:";cin>>data1;if(data1==0){return;}p=new Circle(data1);p->reArea();p->next=head;head=p;cout<<"数据成功插入!"<<endl;}else if(select_num==2){int data1;int data2;cout<<"请输入长轴:";cin>>data1;if(data1==0){return;}cout<<"请输入短轴:";cin>>data2; if(data2==0){return;}p=new Elipse(data1,data2);p->reArea();p->next=head;head=p;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_BLUE);cout<<"数据成功插入!"<<endl;}else if(select_num==3){int data1;int data2;cout<<"请输入长:";cin>>data1;if(data1==0){return;}cout<<"请输入短宽:";cin>>data2; if(data2==0){return;}p=new Rectangles(data1,data2);p->reArea();p->next=head;head=p;cout<<"数据成功插入!"<<endl;}else if(select_num==4){int data1;int data2;cout<<"请输入底:";cin>>data1;if(data1==0){return;}cout<<"请输入高:";cin>>data2; if(data2==0){return;}p=new Triangle(data1,data2);p->reArea();p->next=head;head=p;cout<<"数据成功插入!"<<endl;}else if(select_num==5){int data1;cout<<"请输入半径:";cin>>data1;if(data1==0){return;}cout<<"请输入高:";p=new Ball(data1);p->reArea();p->reVolume();p->next=head;head=p;cout<<"数据成功插入!"<<endl;}else if(select_num==6){int data1;int data2;cout<<"请输入底面半径:";cin>>data1;if(data1==0){return;}cout<<"请输入高:";cin>>data2; if(data2==0){return;}p=new Cylider(data1,data2);p->reArea();p->reVolume();p->next=head;head=p;cout<<"数据成功插入!"<<endl;}else if(select_num==6){int data1;int data2;int data3; cout<<"请输入长:";cin>>data1;if(data1==0){return;}cout<<"请输入宽:";cin>>data2; if(data2==0){return;}cout<<"请输入高:";cin>>data3; if(data3==0){return;}p=new RectangularParallelepiped(data1,data2,data3);p->reArea();p->reVolume();p->next=head;head=p;cout<<"数据成功插入!"<<endl;}}else//如果不在头结点插入 {Shape *m;//用于指向到指定的链表位置 int i=0;//用于循环到指定的链表位置 m=head;for(i=1;i<insert_num;i++) {m=m->next; }if(select_num==1){int data1;cout<<"请输入半径:";cin>>data1; if(data1==0){return;}p=new Circle(data1); p->reArea();p->next=m->next; m->next=p;cout<<"数据成功插入!"<<endl;} else if(select_num==2){int data1;int data2; cout<<"请输入长轴:";cin>>data1;if(data1==0){return;}cout<<"请输入短轴:";cin>>data2;if(data2==0){return;}p=new Elipse(data1,data2);p->reArea();p->next=m->next; m->next=p;cout<<"数据成功插入!"<<endl;} else if(select_num==3){int data1;int data2; cout<<"请输入长:";cin>>data1;if(data1==0){return;}cout<<"请输入宽:";cin>>data2;if(data2==0){return;}p=new Rectangles(data1,data2);p->reArea();p->next=m->next; m->next=p;cout<<"数据成功插入!"<<endl;}else if(select_num==4){int data1;int data2; cout<<"请输入底:";cin>>data1;if(data1==0){return;}cout<<"请输入高:";cin>>data2;if(data2==0){return;}p=new Triangle(data1,data2);p->reArea();p->next=m->next; m->next=p;cout<<"数据成功插入!"<<endl;} else if(select_num==5){int data1;cout<<"请输入半径:";cin>>data1;if(data1==0){return;}p=new Ball(data1);p->reArea();p->reVolume();p->next=m->next; m->next=p;cout<<"数据成功插入!"<<endl;} else if(select_num==6){int data1;int data2; cout<<"请输入底面半径:";cin>>data1;if(data1==0){return;}cout<<"请输入高:";cin>>data2;if(data2==0){return;}p=new Cylider(data1,data2);p->reArea();p->reVolume();p->next=m->next; m->next=p;cout<<"数据成功插入!"<<endl;} else if(select_num==7){int data1;int data2; int data3; cout<<"请输入底面长:";cin>>data1;if(data1==0){return;}cout<<"请输入宽:";cin>>data2;if(data2==0){return;}cout<<"请输入高:";cin>>data3;if(data3==0){return;}p=new RectangularParallelepiped(data1,data2,data3);p->reArea();p->reVolume();p->next=m->next; m->next=p;cout<<"数据成功插入!"<<endl;} } 
} 
void deletelist(Shape * & head)
{if(head==NULL){cout<<endl;cout<<"您还没有建立链表呦~"<<endl<<endl;return; } int delete_sel; //删除节点的位置 int delete_num;//选择删除指定相同的节点 int j=0;//用于选择删除指定相同的节点 Shape *d,*e,*f;//用于指向到指定的链表位置的前一位 d=head;cout<<"1 删除圆形的节点"<<endl<<"2 删除椭圆形的节点"<<endl<<"3 删除长方形的节点"<<endl<<"4 删除三角形的节点"<<endl<<"5 删除球体的节点"<<endl<<"6 删除圆柱体的节点"<<endl<<"7 删除长方体的节点"<<endl<<"当输入数据为0时退出!"<<endl;cout<<endl<<"请输入你的选择:"; cin>>delete_sel;if(delete_sel>7){cout<<"不合法输入";return; }cout<<"删除此图形的第几个数据?"; cin>> delete_num;for(j=0;j<delete_num;j++){f=d; for(;d->reflag()!=delete_sel;) {f=d;d=d->next; }if((j+1)!=delete_num){d=d->next;}} if(d==head){head=head->next;delete d; cout<<'\n'<<"删除成功!"<<endl;}else{e=f->next;f->next=f->next->next;delete e; cout<<'\n'<<"删除成功!"<<endl;}/*if(delete_num==0){cout<<"第1个节点才是头结点,请从新输入:"; cin>>delete_num;}else if(delete_num==1){head=head->next;delete d; cout<<'\n'<<"删除成功!"<<endl; }else if(delete_num>1 && delete_num<999){for(j=2;j<delete_num;j++){d=d->next; }e=d->next;d->next=d->next->next;delete e; cout<<'\n'<<"删除成功!"<<endl; }*/
}
void turnlist(Shape * & head)//翻转链表
{if(head==NULL){cout<<endl;cout<<"您还没有建立链表呦~"<<endl<<endl;return;}Shape *t1=NULL;Shape *t2=NULL; Shape *t3=NULL;//三个配合来翻转t1=head;t2=t1->next;t3=t2->next;t1->next=NULL; for(;t3!=NULL;){t2->next=t1;t1=t2;t2=t3;t3=t3->next;} if(t3==NULL){t2->next=t1;delete t3;head=t2;}SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_BLUE);cout<<'\n'<<"链表反转成功!"<<'\n'<<endl;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN); 
} 
void savelist(Shape * & head)
{if(head==NULL){cout<<endl;cout<<"您还没有建立链表呦~"<<endl<<endl;return; } Shape *browse=head;while(browse!=NULL){browse->saveData(); browse=browse->next;}delete browse;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_BLUE);cout<<endl<<"导出到文本成功!"<<endl<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);
}
void readlist(Shape * & head)
{head=NULL;fstream read;read.open("E:\\game\\list.txt",ios::in);double m[9999];//文本数据存储 int i,j;int judge;//只是个判断,不用定义为int Shape *p=NULL;Shape *move=NULL; for(i=0;!read.eof();i++){read>>m[i];	//m[i+1]=0; }   for(j=0;j<i-1;j++){if(m[j]==0)	{ if(m[j+1]==1){double data1;data1=m[j+2];p=new Circle(data1); }else if(m[j+1]==2){double data1;double data2;data1=m[j+2];data2=m[j+3];p=new Elipse(data1,data2);}else if(m[j+1]==3){double data1;double data2;data1=m[j+2];data2=m[j+3];p=new Rectangles(data1,data2);}else if(m[j+1]==4){double data1;double data2;data1=m[j+2];data2=m[j+3];p=new Triangle(data1,data2);}else if(m[j+1]==5){double data1;data1=m[j+2];p=new Ball(data1);}else if(m[j+1]==6){double data1;double data2;data1=m[j+2];data2=m[j+3];p=new Cylider(data1,data2);}else if(m[j+1]==7){double data1;double data2;double data3;data1=m[j+2];data2=m[j+3];p=new RectangularParallelepiped(data1,data2,data3);}} //p->next=NULL;if(head==NULL){head=p;//move=p;}else{move->next=p;//move=p;}move=p;p->next=NULL;}
}int main()
{SetConsoleTitle("图形链表录入系统     CREATED BY 刘麒   2013.8");//设置标题SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);int data_num;//链表个数int menu_sel;//菜单选择 Shape *head=NULL;void creatlist(Shape * & head,int n);void browselist(Shape *head);void insertlist(Shape * & head);void deletelist(Shape * & head);void turnlist(Shape * & head);void savelist(Shape * & head);void readlist(Shape * & head);while(1) {cout<<"■■■■■■■■■■■■■■" <<endl;cout<<"■1  创建链表		  ■"<<endl<<"■2  遍历链表		  ■"<<endl<<"■3  插入链表		  ■"<<endl<<"■4  删除链表		  ■"<<endl<<"■5  反转链表		  ■"<<endl<<"■6  存储链表到txt文件	  ■"<<endl<<"■7  查看链表文件	  ■"<<endl<<"■8  从txt文本中导入链表  ■"<<endl<<"■9  安全退出		  ■"<<endl;cout<<"■  !链表只可创建一次!  ■"<<endl; cout<<"■■■■■■■■■■■■■■" <<endl;cout<<"请输入你的选择:"; cin>>menu_sel; //int showsystime();//showsystime();if(menu_sel==9){return 0;} else if(menu_sel==1){if(head!=NULL){cout<<endl<<"!链表只可创建一次!"<<endl<<"程序已退出!"<<endl;return 0;}cout<<"请输入需要创建节点个数:"; cin>>data_num;cout<<endl;if(data_num==0) {cout<<"您没有创建链表,程序退出!"<<endl; return 0;}creatlist(head,data_num); } else if(menu_sel==2) {browselist(head);}else if(menu_sel==3){insertlist(head); }else if(menu_sel==4){deletelist(head); }else if(menu_sel==5){turnlist(head);}else if(menu_sel==6){savelist(head);} else if(menu_sel==7){system("E:\\game\\list.txt"); } else if(menu_sel==8){readlist(head);}} 
}



这篇关于c++小学期的实验二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1039437

相关文章

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

C++包装器

包装器 在 C++ 中,“包装器”通常指的是一种设计模式或编程技巧,用于封装其他代码或对象,使其更易于使用、管理或扩展。包装器的概念在编程中非常普遍,可以用于函数、类、库等多个方面。下面是几个常见的 “包装器” 类型: 1. 函数包装器 函数包装器用于封装一个或多个函数,使其接口更统一或更便于调用。例如,std::function 是一个通用的函数包装器,它可以存储任意可调用对象(函数、函数

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

06 C++Lambda表达式

lambda表达式的定义 没有显式模版形参的lambda表达式 [捕获] 前属性 (形参列表) 说明符 异常 后属性 尾随类型 约束 {函数体} 有显式模版形参的lambda表达式 [捕获] <模版形参> 模版约束 前属性 (形参列表) 说明符 异常 后属性 尾随类型 约束 {函数体} 含义 捕获:包含零个或者多个捕获符的逗号分隔列表 模板形参:用于泛型lambda提供个模板形参的名

uva 11044 Searching for Nessy(小学数学)

题意是给出一个n*m的格子,求出里面有多少个不重合的九宫格。 (rows / 3) * (columns / 3) K.o 代码: #include <stdio.h>int main(){int ncase;scanf("%d", &ncase);while (ncase--){int rows, columns;scanf("%d%d", &rows, &col

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

【C++高阶】C++类型转换全攻略:深入理解并高效应用

📝个人主页🌹:Eternity._ ⏩收录专栏⏪:C++ “ 登神长阶 ” 🤡往期回顾🤡:C++ 智能指针 🌹🌹期待您的关注 🌹🌹 ❀C++的类型转换 📒1. C语言中的类型转换📚2. C++强制类型转换⛰️static_cast🌞reinterpret_cast⭐const_cast🍁dynamic_cast 📜3. C++强制类型转换的原因📝

C++——stack、queue的实现及deque的介绍

目录 1.stack与queue的实现 1.1stack的实现  1.2 queue的实现 2.重温vector、list、stack、queue的介绍 2.1 STL标准库中stack和queue的底层结构  3.deque的简单介绍 3.1为什么选择deque作为stack和queue的底层默认容器  3.2 STL中对stack与queue的模拟实现 ①stack模拟实现

c++的初始化列表与const成员

初始化列表与const成员 const成员 使用const修饰的类、结构、联合的成员变量,在类对象创建完成前一定要初始化。 不能在构造函数中初始化const成员,因为执行构造函数时,类对象已经创建完成,只有类对象创建完成才能调用成员函数,构造函数虽然特殊但也是成员函数。 在定义const成员时进行初始化,该语法只有在C11语法标准下才支持。 初始化列表 在构造函数小括号后面,主要用于给