
C++
千和儿
每日打卡~
展开
-
C++25——STL
a pair class:表达两个东西之间的关系 容器 vector:可扩展的数字,在一头固定增长 deque:可扩展的数字,往两头增长 list:内部是双向链表 set:集合,没有重复的,无序的 map:映射(ket,value) 基本函数的模板(排序、搜索等) 以上所有的内容都在std里(使用using namespace std;)所有标识符都是小写#include<iostre..原创 2022-04-03 17:26:33 · 1269 阅读 · 0 评论 -
C++24——流
insert:将一个对象插入输出流中 <<extractor:将内容从流里解析出来 >>流:一维 单方向cin:标准输入cout:标准输出cerr:标准错误clog:标准日志//全局函数,而非成员函数istream& operator>>(istream& is,T& obj){//流里的position(读取位置在修改)会变化,因此不加const //... return is;.原创 2022-04-03 16:48:31 · 1193 阅读 · 0 评论 -
C++23——异常
try{ open the file; determine its size; allocate that much memory; read the file into memory; close the file;} catch(fileOpenFailed){ doSomething;} catch(sizedDeterminationFailed){ doSomething;} catch(memoryAllocateFail...原创 2022-04-03 16:45:06 · 979 阅读 · 0 评论 -
C++22——模板
template < class T > //声明void swap(T& x,T& y){ T temp=x; x=y; y=temp;}void swap(int& x,int& y){ int temp=x; x=y; y=temp;}int i=3;int j=4;swap(i,j); //调用具体的int swapfloat k=4.5;float m=3.7;swap(k,m); .原创 2022-04-02 23:39:48 · 641 阅读 · 0 评论 -
C++21——运算符重载
只能对已经有的符号进行重载,不能凭空创造符号进行重载 只能对类或者枚举类型进行重载 符号操作数个数不能改变(如:+有两个操作数,1+1),优先级不能改变 实例const String String::operator+(const String& that);//作为成员函数const String operator+(const String& r,const String& l);//作为全局函数class Integer{...原创 2022-03-30 18:32:42 · 1804 阅读 · 0 评论 -
C++20——静态对象&静态成员
void f(){ static int num_calls=0; ... num_calls++;}class X{ X(int,int); ~X();};void f(){ static X my_X(10,20);//变量只做一次初始化}类里不能对静态成员做初始化,只能对非静态成员做初始化。class A{public: //A():i(0){ } 这句会报错,不能对静态成员做初始化 A(){ i=0; }..原创 2022-03-30 18:28:22 · 1071 阅读 · 0 评论 -
C++19——拷贝构造
void func(Currency p){ cout<<"X="<<p.dollars();}Currency bucks(100,0);func(bucks);Currency p=bucks;//初始化p=bucks;//赋值在C++中可以用=和()初始化对象。Person baby_a("Fred");Person baby_b=baby_a;Person baby_c(baby_a);#include<iostream>.原创 2022-03-29 20:12:33 · 590 阅读 · 0 评论 -
C++18——引用再研究
class X{publilc: int& m_y; X(int& a);};X::X(int& a):m_y(a){ }函数不能返回本地变量的引用&指针;可以返回对象。#include<assert.h>const int SIZE=32;double myarray[SIZE];double& subscript(const int i){ return myarray[i]; //正确,返回的是全局变量.原创 2022-03-29 20:10:28 · 849 阅读 · 0 评论 -
C++17——多态性
class XYPos{ ... }; //x,y pointclass Shape{public: Shape(); virtual ~Shape(); virtual void render();//virtual 表示这个render()和子类的render()有联系 void move(const XYPos&); virtual void resize();protected: XYPos center;};class Ellipse.原创 2022-03-26 16:15:34 · 997 阅读 · 0 评论 -
C++16——向上造型
把子类的对象当作父类来看待:如果对象B是对象A的子类,那么对象B可以当作对象A来使用。#include<iostream>using namespace std;class A{public://public private可以写多个 int i;public: A():i(10) {}};class B:public A{private: int j;public: B():j(30){ } void f() {cout<&原创 2022-03-26 16:13:32 · 807 阅读 · 0 评论 -
C++15——引用(reference)
char c; //字符char *p=&c; //指针char &r=c; //引用,在定义时需要有初始值(变量或者是可以做左值的东西),定义时必须初始化//r是c的别名//作为参数表或者成员变量可以没有初始值,但是本地变量或者全局变量必须有初始值int x=47;int &y=x;cout<<"y="<<y; //y=47y=18;cout<<"x="x; //x=18int x=3;int &y=x;.原创 2022-03-11 23:03:10 · 1233 阅读 · 0 评论 -
C++14——不可修改的对象
#include<iostream>using namespace std;class A{ int i;public: A():i(0){} void f() {cout<<"f()"<<endl;}//f(A* this) void f() const {cout<<"f() const"<<endl;}//f(const A* this) //以上两个函数构成overload的条件,参数不同};.原创 2022-03-11 23:00:52 · 1534 阅读 · 0 评论 -
C++学习13——const
变量加上const 后仍然是变量,初始化之后不能赋值,不能修改。const int x=123;x=27;//错误x++;//错误int y=x;//正确,将const赋值给non-consty=x;//正确,同上,non-const可以修改const int z=y;//正确,const赋值给const,但是z之后不可以再被赋值或者修改了const int bufsize=1024;必须要预先初始化,除非加上externextern const int bufsize;编译器编原创 2022-03-05 20:18:05 · 336 阅读 · 0 评论 -
C++学习12——内联函数
overhead:额外的开销,总的开销 。int f(int i){ return i*2;}main(){ int a=4; int b=f(a);}调用函数的额外的步骤: 将参数压入栈中 将返回地址压入栈中 准备返回值 取出所有被压入栈中的值 使用内联函数可以省略以上步骤:如果一个函数是内联函数,不会需要以上的步骤,在调用函数时将函数代码放到调用它的地方,且还能保持函数的独立性。int f(int i){ re原创 2022-03-05 20:14:58 · 367 阅读 · 0 评论 -
C++学习11——函数重载和默认参数(缺省参数值)
如果两个函数参数表相同,名称相同,返回类型不同,不能过程重载。如:int a=f();double a=f();//错误,无法重载void f(short i);void f(double d);f('a');f(2);f(2L);f(3.2);//重载Stach(int size,int initQuantit=0);//Stach(1,1);Stach(1);都可以int harpo(int n,int m=4,int j=5);//默认参数必须从右边开始写i原创 2022-01-22 23:51:53 · 359 阅读 · 0 评论 -
C++学习10——父类子类的关系
//std::string类的全名,由于没有加using namespace std;所以要写全名class Employee{public: Employee(const std::string& name,const std::string& ssn); const std::string& get_name() const; void print(std::ostream& out) const; void print(std::o.原创 2022-01-20 22:58:29 · 629 阅读 · 0 评论 -
C++学习9——继承
继承:在已有的类上做一些改造,做出一个新的类。接口与成员函数的区别:成员函数(成员变量)可以是public和private,其中public的部分构成接口Student继承了Person,拥有更多属性被继承的类放在上面,继承的类放在下面 。基类——派生类父类——子类#include<iostream>using namespace std;class A{public: A():i(0){cout<<"A::A()"<&l..原创 2022-01-20 22:57:31 · 113 阅读 · 0 评论 -
C++学习8——对象组合
op(面向对象)的三大属性:分装、继承、多态性op软件重用的方式:继承(不是唯一的方式)组合 也是软件重用的方式组合:使用已有的对象,组合成新的对象(类里有成员变量,这个变量是其他类的对象)class Person{...};class Currency{...};class SavingsAccount{public: SavingsAccount(const char* name,const char* address,int cents); ~SavingsAc原创 2022-01-20 22:56:12 · 767 阅读 · 0 评论 -
C++学习7——初始化列表
A():p(0){cout<<...}//初始化列表,相当于A(){p=0;cout<<...}class Point{private: const float x,y; Point(float xa=0.0,float ya=0.0):y(ya),x(xa){} //初始化列表,可以初始化任何类型的数据 //y(ya),x(xa)的初始化早于构造函数};Student::Student(string s):name(s){}//初始.原创 2022-01-20 01:10:29 · 300 阅读 · 0 评论 -
C++学习6——访问限制
public:公开的,任何人都可以访问private:私有的,只有这个类的成员函数可以访问成员变量与成员函数protected:保护的,只有这个类自己以及子子孙孙可以访问#include<iostream>using namespace std;class A{private: int i; int *p; public: A(){p=0;i=0;cout<<"A::A()"<<endl;} ~A(){if(p原创 2022-01-20 01:08:35 · 368 阅读 · 0 评论 -
C++学习5——new&&delete
new(可以通过指针访问) new int;(分配空间) new Stash(类);(先分配空间,再调用构造函数) new int[10]; delete delete p;(delete 这个对象,先析构,再收回空间) delete[] p; int * psome=new int [10];//有多个数据delete [] psome;//如果使用不带[]的delete,psome只有第一个数据被de..原创 2022-01-20 01:07:18 · 196 阅读 · 0 评论 -
C++学习4——对象初始化
void f(int i){ if(i<10){ goto jump1;//如果直接跳到jump1,则直接跳过x1初始化,没有进行构造,则在离开时,也无法析构,编译时会报错 } X x1; jump1: switch(i){ case 1: X x2; break; case 2: //报错,理由同上 X x3; br.原创 2022-01-16 01:37:20 · 171 阅读 · 0 评论 -
C++学习3
constructor:构造函数destructor:析构函数class X{ int i;public: X();//构造函数与类名相同,没有返回值,在对象被创建时自动被调用,构造函数也是成员函数};void f() { X a;//只要对象被定义,构造函数就会被调用,即此时一定会发生a.X();}class Point {//C++的类不负责自动初始话public: void init(int x,int y);//初始化函数,如果不初始化.原创 2022-01-10 00:46:43 · 156 阅读 · 0 评论 -
C++学习2
原创 2022-01-08 23:59:05 · 262 阅读 · 0 评论 -
学习C++
观看B站翁恺老师的视频学习C++。原创 2022-01-03 21:36:33 · 351 阅读 · 0 评论