cpp知识点总结(精简版)
文章目录
一、程序结构
1、随机函数
srand(time(0));
n=rand()%5+1;
二、面向对象编程
1、使用类里面的数据、函数
(但是一般不这么做,一般使用构造函数)
class test
{
private:
int a,b;
public:
int add()
{
return a+b;
}
};
int main()
{
test t;
int a=1,b=2;
t.a=a;
t.b=b;
cout<<c.add()<<endl;
return 0;
}
三、类的使用-初始化
1、函数参数的缺省值
#include <iostream>
using namespace std;
int f(int n=10)
{
return n*n;
}
int main()
{
cout<<f(12)<<endl;
cout<<f()<<endl;
return 0;
}
2、函数的重载
#include <iostream>
using namespace std;
//函数的重载
//识别括号里的数据类型、个数
//参数的类型个数不一样时候适用
int add(int n)
{
return n*n;
}
int add(int x,int y,int z)
{
return x+y+z;
}
int main()
{
cout<<add(1,2)<<endl;
return 0;
}
四、指针与引用
1、动态申请内存
(1)cpp变量
int* p;
p=new int;
···
delete p;
int* p=new int;
···
delete p;
(2)cpp数组
int* p;
int n=3;
p=new int[n];
···
delete [] p;
int n=3;
int* p=new int[n];
···
delete [] p;
(3)cpp结构体
struct student
{
string name;
int number;
double score;
};
int main()
{
student* p;//
p=new student;//
cin>>(*p).number;
cin>>p->score;
cout<<(*p).number<<endl;
cout<<p->score<<endl;
delete p;//
return 0;
}
(4)cpp结构体数组
struct student
{
string name;
int number;
double score;
};
int main()
{
student* p;
int n=3,i;
p=new student[n];//
for(i=0;i<n;i++)
{
cin>>p[i];
}
for(i=0;i<n;i++)
{
cout<<p[i]<<endl;
}
delete [] p;//释放掉p和指向的数组部分 //
return 0;
}
2、引用的使用
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int a=100,b=200;
int& p=a;//p叫做变量a的引用 //就是把a改了个名字
p=b;
cout<<a<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void swap1(int& a,int& b)//引用的作用:参数的传入,简化写法 ——增加程序可读性,节省内存空间
{
int temp=a;
a=b;
b=temp;
}
//cpp中可以用引用代替指针
int main()
{
int x=100,y=200;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"=============swap1()================"<<endl;
swap1(x,y);//swap是库函数
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
return 0;
}
3、链表
#include <iostream>
#include <cstdlib>
using namespace std;
struct score
{
int number;
score* next;
};
class Select
{
private:
int n;
score* head,* end,* p;
public:
Select(int nn)
{
n=nn;
}
void input()
{
head=new score;
end=head;
int i;
for(i=0;i<n;++i)
{
p=new score;
cout<<"input score:";cin>>p->number;
end->next=p;
end=p;
}
end->next=0;
}
void browse()
{
p=head;
while(true)
{
p=p->next;
if(p==0)break;
cout<<p->number<<endl;
}
}
void output()
{
int i;
int max=0,min=11,sum=0;
p=head;
while(p->next!=0)
{
p=p->next;
sum=sum+p->number;
if(p->number>max)
{
max=p->number;
}
if(p->number<min)
{
min=p->number;
}
}
sum=sum-min-max;
cout<<"score="<<sum<<endl;
}
};
int main()
{
Select s(3);
s.input();
s.browse();
// s.output();
return 0;
}
五、类的继承与派生
1、模板
(1)2个类
#include <iostream>
#include <cstdlib>
using namespace std;
//继承与派生
class A
{
protected://protected家族——受保护
int m;
public:
A()
{
m=1000;
}
A(int mm)
{
m=mm;
}
void input()
{
cout<<"input m:";cin>>m;
}
void output()
{
cout<<"m="<<m<<endl;
}
};//注意‘;’
class B:public A//公有继承//软件重用
{
protected:
int n;
public:
//派生类的构造函数
B():A()//子类构造函数数量不能少于父类//父类重载子类也必须重载
{
n=2000;
}
B(int mm/*千万不要忘了写*/,int nn):A(mm)//加了A(mm)上面A也得加一个构造函数
{
n=nn;
}
void input()
{
A::input();//重载之后也能用父类的函数
cout<<"input n:";cin>>n;
}
void output()
{
A::output();//重载之后也能用父类的函数
cout<<"n="<<n<<endl;
}
};//注意‘;’
int main()
{
//B b(10,20);
B b;
// b.input();//都有input output——重载,用自己的函数
b.output();//上面加入A::input();重载之后也能用父类的函数
return 0;
}
(2)3个类
#include <iostream>
#include <cstdlib>
using namespace std;
class Y
{
protected:
int y;
public:
Y(){
y=2021;}
Y(int yy){
y=yy;}
};//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
class M:public Y
{
protected:
int m;
public:
M(){
m=3;}
M(int yy,int mm):Y(yy){
m=mm;}
M(int mm)
{
m=mm;
}
};
class D:public M//写父亲不要写爷爷!
{
protected:
int d;
public:
D()
{
d=23;
}
D(int yy,int mm,int dd):M(yy,mm)//只管父亲
{
d=dd;
}
D(int dd)
{
d=dd;
}
D(int mm,int dd):M(mm)//父类必须得追加一个只有mm的构造函数
{
d=dd;
}
void output()
{
cout<<y<<"-"<<m<<"-"<<d<<endl;
}
};
int main()
{
// D d;
// D d(2022,3,23);
// D d(24);
D d(4,3);
d.output();
return 0;
}
六、运算符重载
1、友元函数
可以让函数中调用类里面的函数
但是不能直接使用类里面的元素,需要这么写:a.m
class A
{
protected:
int m;
public:
A()
{
m=100;
}
void input()
{
cout<<"input m:";
cin>>m;
}
void output()
{
cout<<"m="<<m<<endl;
}
friend void display(A a);//友元函数,可以让display使用类里面的函数
};
void display(A a)
{
a.output();
cout<<a.m<<endl;//不能使用类里面的数据
}
int main()
{
A a;
display(a);
return 0;
}
2、输入/输出运算符的重载
(1)输入运算符(放在类内)
friend istream& operator>>(istream& os,A& a/*(10)*/)//得有个引用&,得让值带回来 //小a后面不能像主函数a一样后面带参数
{
a.input();
return os;
}
(2)输出运算符(放在类内)
friend ostream& operator<<(ostream& os,A a)
{
a.output();
return os;
}
3、双目运算符的重载
(1)+
A operator+(/*A a1,*/A a)//双目运算符第一个数规定就是自己——写一个参数即可
{
A temp;
temp.a=m+a.m;
return temp;
}
(2)>
bool operator>(A a2)//双目运算符第一个数规定就是自己
{
return m>a2.m;
}
(3)<
bool operator<(A a2)//双目运算符第一个数规定就是自己
{
return m<a2.m;
}
4、单目运算符的重载(++、–)
(1)++i
A operator++()//++i
{
A temp;
temp.i=i+1; //表达式
i=i+1; //自身值
return temp;
}
(2)i++
A operator++(int w)//i++
//int w——为了与第一个区分开
{
A temp;
temp.i=i; //表达式
i=i+1; //自身值
return temp;
}
(3)this指针
A operator++()//++i
{
++m;
return *this;//this指针指向某个对象,在类里面使用//this是类的地址,*this是类
//*this返回自己的类
}
A operator++(int w/*为了与第一个区分开*/)//i++
{
A old(*this);//定义对象old并用*this类赋值——记住这个写法
//也可以写成A old = *this;
++(*this);
return old;
}
5、“赋值=”与“等于==”运算符的重载
operator==(point p)//前面加个bool也能运行
{
return (x==p.x && y==p.y);
}
operator=(point p)
{
x=p.x;
y=p.y;
}
/*或者将上面的函数写成这样
point& operator=(point& p)
{
this->x=p.x;
this->y=p.y;
return p;
}*/
/*或者将上面的函数写成这样
operator=(point& p)
{
this->x=p.x;
this->y=p.y;
}*/
七、容器
(一)vector容器
1、定义
vector<int> a;
vector<int>::iterator p;
2、添加元素
a.push_back(temp);
3、几个常用函数
(1)元素数量:a.size()
(2)首尾迭代器:a.begin()、a.end()
(3)删除元素:a.erase()
4、排序
static bool cmp(int n1,int n2)//跟容器元素类型匹配
{
n1>n2;
}
void Sort()
{
sort(a.begin(),a.end());cmp);//排序范围,排序规则
}
static bool cmp1(S s1,S s2)//跟容器元素类型匹配//学号排序
{
return s1.number>s2.number;//从大到小排序
}
static bool cmp2(S s1,S s2)//跟容器元素类型匹配//成绩排序
{
return s1.score>s2.score;//从大到小排序
}
void Sort()
{
sort(a.begin(),a.end(),cmp1);//排序范围,排序规则
}
5、查找
void Find(int m)//对应容器元素类型
{
p=a.begin();//开始前先把迭代器放到容器头!
while(true)
{
p=find(p,a.end(),m);//(用谁找,查找范围,查找谁)
if(p!=a.end())//检测到没到尾部
{
cout<<*p<<endl;
++p;//一定不要忘写++p;!!!!!!!!!!!!!!!!!!!!!!!!!!!!否则会无限循环
}
else break;
}
}
struct S
{
int index;
int number;
int score;
bool operator==(S s)//怎么处理Find函数//重载==
//s是第二运算数第一个是自己
{
if(s.index==1)
return number==s.number;
else if(s.index==2)
return score==s.score;
}
};
···
void Find(int m)//对应容器元素类型
{
p=a.begin();//开始前先把迭代器放到容器头!
t.index=2;//对成绩进行检索//1-学号
t.score=m;//t.number=m;
//上面两行一定要对应,否则结果不正确!!!
while(true)
{
p=find(p,a.end(),t);//(用谁找,查找范围,查找谁)
//需要解释查找的t是什么——要在结构体中重载==才能使用
if(p!=a.end())
{
cout<<p->number<<"-"<<p->score<<endl;
++p;//一定不要忘了++p否则会无限循环!!!!!!!!!!!!!!!!!!!
}
else break;
}
}
★实例
(1)简单元素
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
class test
{
private:
vector<int>a;
vector<int>::iterator p;
int n,i;
public:
test(int nn)
{
n=nn;
srand(time(0));
for(i=0;i<n;++i)//压入的时候是用i从0到n-1
{
a.push_back(