![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++知识点整理
c++
Binary Oracle
一名热爱开源和技术的Coder , 开源框架spring committer , golang开源网络库netpoll committer; (脱产备战25考研中,停更一年)
展开
-
静态库的优缺点和动态库的实现
静态库的优缺点:静态库会在函数编译时把静态库里面所有的函数声明和实现都拷贝到.exe文件中,这样的删除.lib文件,.exe文件一样可以运行,但是如果.lib文件占用内存很大,会造成.exe文件很大,浪费内存和资源windows下的动态库的创建和使用比如动态库里面有500个函数的声明和实现,当程序运行时用到第一个和第五个函数的时候,便会去动态库里面找第一个和第五个函数的的实现,然后进行链接,将第一个和第五个函数的实现放入.exe文件中。如果是静态库,便会在程序编译时,把静态库里面500个函数原创 2021-02-27 14:23:36 · 274 阅读 · 0 评论 -
静态库和动态库
库的基本概念:windows下静态库的创建和使用1.静态库的创建步骤:总结:原创 2021-02-27 13:21:31 · 113 阅读 · 0 评论 -
预处理
1.预处理基本概念:2.文件包含指令(#inlcude)文件包含处理:#include < > 和include’'的区别:原创 2021-02-27 09:53:17 · 133 阅读 · 0 评论 -
宏定义
1.无参数的宏定义#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>//宏 常量//1.不重视作用域//2.可以用undef卸载宏//3.宏常量 没有数据类型void test(){#define MAX 100}int main(){ test(); int a = MAX; printf("%d", a); ret原创 2021-02-27 10:10:48 · 143 阅读 · 0 评论 -
#ifdef宏名和#ifndef宏名
#ifdef宏名:#include<iostream>#include<conio.h>using namespace std;#define dhy //定义宏名int main(){#ifdef dhy //如果宏名定义了执行ifdef代码 cout << "你好,世界" << endl;#else //未定义宏名则会执行else后面的代码 cout << "再见,世界" << endl;#endif /原创 2021-01-31 10:03:03 · 324 阅读 · 1 评论 -
函数指针
形式:数据类型(*指针变量名字)(参数的数据类型1,参数数据类型2…)=函数名(或者强制类型转换后的函数地址)下面为Qt中的c语言代码:函数名地址,变量地址等在vs中会一直变,因为每次启动程序都要重新分配内存,在qt中不会变,所以可以直接对函数名地址进行强制类型转换#include "mainwindow.h"#include<stdio.h>#include <QApplication>#include<QDebug>void func(int a, in原创 2021-02-23 12:23:00 · 143 阅读 · 0 评论 -
函数指针的定义方式,和指针函数的区别,函数指针数组
可以类比数组指针:int(*p)[10]方式1:#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>void fun1(int a, int b){ printf("%d+%d", a, b);}void test(){ //函数指针定义的三种方式 //1.先定义出函数类型,再通过函数类型定义指针变量 typedef void(FUNC_TYPE)(int, int); FUNC_TYPE* func = fun1; fun原创 2021-02-23 14:09:56 · 190 阅读 · 0 评论 -
关系仿函数
function:实现关系对比仿函数原型:#include<iostream>using namespace std;#include<functional>#include<vector>#include<algorithm>void p(const vector<int>& v){ for (int i = 0; i < v.size(); i++) { cout << v[i] <原创 2021-02-15 11:37:54 · 314 阅读 · 0 评论 -
内建函数对象---算术仿函数
概念:stl内建了一些函数对象分类:算术仿函数关系仿函数逻辑仿函数用法:这些仿函数所产生的对象,用法和一般函数完全相同使用内建函数对象,需要引入头文件 #include < functional >算术仿函数功能描述:仿函数原型:#include<iostream>using namespace std;#include<functional>void test(){ //算术仿函数 //1. plus<int>原创 2021-02-15 11:20:50 · 228 阅读 · 2 评论 -
逻辑仿函数
功能:实现逻辑运算函数原型:#include<iostream>using namespace std;#include<functional>#include<vector>#include<algorithm>void p(const vector<bool>& v){ for (int i = 0; i < v.size(); i++) { cout << v[i] << "原创 2021-02-15 11:53:58 · 245 阅读 · 0 评论 -
函数对象--仿函数
概念:重载调用函数操作符的类,其对象称为函数对象函数对象使用重载()时,行为类似函数调用,也叫仿函数函数对象本质是一个类,不是一个函数函数对象使用:features:1.函数对象在调用时,可以像普通函数一样调用,可以有参数,可以有返回值#include<iostream>using namespace std;#include<string>//函数对象//1.函数对象在调用时,可以像普通函数一样调用,可以有参数,可以有返回值class add {pu原创 2021-02-14 19:04:57 · 244 阅读 · 0 评论 -
二元谓词
二元谓词— 谓词–>bool二元----仿函数里面的参数个数#include<iostream>using namespace std;#include<vector>#include<algorithm>//二元谓词--- 谓词-->bool 二元----仿函数里面的参数个数class compare {public: bool operator()(int v1,int v2) { return v1 > v2;原创 2021-02-14 20:32:24 · 985 阅读 · 3 评论 -
一元谓词
概念:输出vector容器中大于60的数字#include<iostream>using namespace std;#include<vector>#include<algorithm>class overSixty {public: bool operator()(int val) { return val > 60; }};void test(){ vector<int> v; v.push_back(74)原创 2021-02-14 20:17:12 · 217 阅读 · 0 评论 -
运算符重载之左移运算符重载
左移运算符重载#include<iostream>using namespace std;class wood { friend ostream& operator<<(ostream& cout, wood& d);public: wood(int num):num(num){}private: int num;};//左移运算符重载只能用全局函数ostream& operator<<(ostream& co原创 2021-01-31 18:36:22 · 277 阅读 · 0 评论 -
指针运算符重载,智能指针
#include<iostream>using namespace std;class person {public: person() { cout << "无参构造的调用" << endl; } void show() { cout << "年龄为:"<<18 << endl; } ~person() { cout << "析构函数的调用" << endl; }};voi原创 2021-03-17 22:35:36 · 167 阅读 · 0 评论 -
关系运算符重载(>,<,==,!=)
关系运算符重载#include<iostream>using namespace std;class wood {public: int num; int height; //==运算符重载 bool operator==(wood w) { if (num == w.num && height == w.height) { return true; } return false; } //!=运算符重载 bool operator!原创 2021-02-01 11:59:28 · 670 阅读 · 0 评论 -
运算符重载之加号运算符重载
加号运算符重载运算符重载:对已有运算符的重新定义,赋予其另一种功能,以适应不同的数据类型#include<iostream>using namespace std;class pig{public: int kilogram; //重量 int price; //价钱 //成员函数重载+号运算符 //pig operator+(pig& p) //{ // pig temp; // temp.kilogram = this->kilogram +原创 2021-01-30 11:00:43 · 471 阅读 · 0 评论 -
运算符重载之递增运算符重载
递增运算符重载#include <iostream>using namespace std;//声明类class wood{ friend ostream& operator<<(ostream& cout, wood myint);public: //默认构造函数将m_Num初始化为0 wood() { m_Num = 0; } //前置递增++ //如果不用引用,就是调用重载函数,调用完之后就销毁(因为放在栈区) //引用是原创 2021-01-31 21:56:04 · 292 阅读 · 0 评论 -
[]运算符重载
为什么要重载[]因为自定义数据类型数组进行[]方式访问,会报错,因为编译器不知道该如何访问,需要对[]进行重载才可以重载[]运算符#include<iostream>using namespace std;class wood {public: int *num; int operator[](int index) { return this->num[index]; }};void test(){ wood d; d.num = new int[3];原创 2021-03-18 13:41:28 · 600 阅读 · 0 评论 -
函数调用运算符重载---()重载小括号
函数调用运算符重载----重载小括号()#include<iostream>using namespace std;class wood {public: //函数调用运算符重载用于类中-------重载小括号 void operator()(int num) { cout << "木头数量为" << num << endl; } int operator()(int num1, int num2) { ret原创 2021-02-01 12:15:49 · 407 阅读 · 0 评论 -
赋值运算符重载
赋值运算符重载#include<iostream>using namespace std;class wood {public: wood(int num) { this->num = new int(num); cout << "构造函数调用" << endl; } //赋值运算符重载(赋值拷贝) wood& operator=(wood& w1) //w1是要进行拷贝的值 { //浅拷贝 //num = w.nu原创 2021-02-01 11:37:37 · 912 阅读 · 0 评论 -
c++模板学习10之类模板分文件编写
类模板分文件编写问题:类模板中成员创建时机是在调用阶段,导致分文件编写时链接不到解决方式1:直接包含.cpp源文件p.h#pragma once //防止头文件重复包含#include<iostream>using namespace std;//类模板与继承template<class T>class Baba{public: void fun();};p.cpp#include"p.h"//成员函数类外实现//第二种写法template&原创 2021-02-06 17:38:33 · 384 阅读 · 0 评论 -
c++模板学习05之类模板
类模板作用:建立一个通用类,类中的成员 数据类型可以不具体制定,用一个虚拟类型代表语法:template< class T >类例子:#include<iostream>#include<string>using namespace std;//类模板template<class Name,class Age>class Person {public: Name name; Age age; Person(Name name,原创 2021-02-05 22:10:45 · 86 阅读 · 0 评论 -
c++模板学习03之普通函数与模板函数区别
普通函数与函数模板的区别1.普通函数调用可以发生隐式类型转换#include<iostream>using namespace std;//1.普通函数调用可以发生隐式类型转换int add(int a, int b){ return a + b;}int main(){ int a = 10; int b = 20; char c = 'a'; //a对应的ascall码为97 //将字符c转化为ascall对应的数值与变量a相加 cout << ad原创 2021-02-05 16:12:27 · 133 阅读 · 0 评论 -
c++模板学习07之类模板中成员函数创建时机
普通类中成员函数一开始就创建类模板中成员函数在调用时才创建#include<iostream>#include<string>using namespace std;//类模板与函数模板的区别class person1 {public: void f1() { cout << "f1函数调用" << endl; } };class person2 {public: void f2() { cout << "f原创 2021-02-06 10:58:28 · 188 阅读 · 0 评论 -
c++模板学习11之类模板与友元
类模板与友元#include<iostream>#include<string>using namespace std;//提前让编译器知道person类存在template<class T1,class T2>class person;//2.全局函数在类外实现template<class T1, class T2>void printPerson1(person<T1, T2>& p){ cout <<原创 2021-02-06 19:18:01 · 185 阅读 · 0 评论 -
c++模板学习08之类模板与继承
类模板与继承当类模板碰到继承时,需要注意以下问题:1.当子类继承的父类是一个类模板时,子类在声明的时候,需要指定出父类中T的类型2.如果不指定,编译器无法给子类分配内存3.如果想灵活指定出父类中的T类型,子类也需要变为类模板#include<iostream>#include<string>using namespace std;//类模板与继承template<class T>class Baba {public: T m; void f原创 2021-02-06 13:49:53 · 183 阅读 · 0 评论 -
c++模板篇01之函数模板
函数模板语法:函数模板template< typename T > 或者可以写template< class T >函数声明或定义解释:1.template----声明创建模板2.typename-------表明其后面的符号是一种数据类型,可以用class代替3.T----------通用的数据类型,名称可以替换,通常为大写下面写一个交换两个数字的函数模板:#include<iostream>using namespace std;//两原创 2021-02-05 15:22:05 · 147 阅读 · 0 评论 -
c++模板学习08之类模板对象做函数参数
#include<iostream>#include<string>using namespace std;//类模板对象做函数参数template<class T1,class T2>class Person {public: Person(T1 name, T2 age) :name(name),age(age){} T1 name; T2 age; void showPerson() { cout << "姓名: " <原创 2021-02-06 13:23:58 · 87 阅读 · 0 评论 -
c++模板学习04之普通函数与模板函数调用规则
普通函数与模板函数调用规则调用规则如下:1.如果函数模板和普通函数都可以实现,优先调用普通函数#include<iostream>using namespace std;//1.如果函数模板和普通函数都可以实现,优先调用普通函数//普通函数void func(int a, int b){ cout << "大忽悠到此一游!" << endl;}//模板函数template<class T>void func(T a, T b){原创 2021-02-05 19:17:34 · 273 阅读 · 0 评论 -
c++模板学习09之类模板函数类外实现
类模板函数类外类模板的构造函数在类外实现#include<iostream>#include<string>using namespace std;//类模板与继承template<class T>class Baba {public: Baba();};//Baba类模板的构造函数在类外实现//第一种写法Baba<int>::Baba(){ cout << "Baba的继承函数调用" << endl;原创 2021-02-06 14:20:28 · 647 阅读 · 0 评论 -
c++模板学习12之通用数组类模板案例封装
通用数组类模板案例封装arr.hpp#pragma once#include<iostream>using namespace std;//通用的数组模板类template<class T>class myarray{private: //数组指针,指向自定义T类型的数组首地址 T* arr; //数组容量 int capicity; //数组元素个数 int size;public: //构造函数初始化 myarray(int cap)原创 2021-02-07 11:08:57 · 342 阅读 · 0 评论 -
c++模板学习06之类模板与函数模板区别
1.类模板没有自动类型推导的使用方式#include<iostream>#include<string>using namespace std;//类模板template<class Name,class Age>class Person {public: Name name; Age age; Person(Name name, Age age) :name(name), age(age) { cout << "姓名:" <&l原创 2021-02-05 22:30:44 · 123 阅读 · 0 评论 -
c++模板篇02函数模板案例----排序函数
函数模板案例----排序函数任务:用选择排序对不同类型的数组进行排序#include<iostream>using namespace std;//交换函数template<class t>void myswap(t& a, t& b){ t temp = a; a = b; b = temp;}//排序函数template<class T>void test(T &array,int len){ for (int原创 2021-02-05 15:54:09 · 185 阅读 · 0 评论 -
c++文件操作4之二进制读取文件
二进制文件之读取文件二进制方式读取文件主要利用流对象调用成员函数read函数原型:istream& read(char buffer, int len);*参数解释:内存指针buffer指向内存中的一段存储空间,len是读写的字节数读取二进制文件步骤:1、包含头文件:#include< fstream >2.创建流对象:ifstream ifs;3.打开文件:ifs.open(“perosn.txt”,ios::in | ios::binary)4.判断文件是否打开成功:原创 2021-02-02 10:03:57 · 3145 阅读 · 0 评论 -
c++文件操作1之文本文件的写入操作
c++文件操作文件类型分两种:1.文本文件:文本文件的ASCALL形式存储在计算机中2.二进制文件:文件以文本的二进制形式存储在计算机中,用户一般难以读懂操作文件的三大类:1.ofstream:写操作2.ifstream读操作3.fstream读写操作文本文件:写文件步骤:1.包含头文件:#include2.创建流对象:ofstream ofs;3.打开文件:ofs.open(“文件路径”,打开方式)4.写数据:ofs<<“写入的数据”<<endl;5.关原创 2021-02-01 19:25:15 · 2500 阅读 · 2 评论 -
c++文件操作3之二进制写文件
二进制文件之写文件打开方式要指定为:ios::binary二进制方式写文件主要利用流对象调用函数write函数原型:ostream& write(const char buffer,int len);参数解释:内存指针buffer指向内存中一段存储空间,len是读写的字数二进制文件写文件步骤:1.包含头文件:#include< fstream >2.创建流对象:ofstream ofs3.打开文件:ofs.open(“person.txt”,ios::out | ios:原创 2021-02-02 09:48:30 · 1261 阅读 · 0 评论 -
c++文件操作2之文本文件的读取操作
文本文件读取操作读取文件操作:1.包含头文件:#include< fstream >2.创建流对象:ifstream ifs3.打开文件,判断文件是否打开成功:ifs.open(“打开路径”,打开方式)4.读取数据:有四种方式,下文用代码方式展现5.关闭文件:ifs.close();#include<iostream>using namespace std;#include<fstream>#include<string>void tes原创 2021-02-01 19:51:22 · 345 阅读 · 0 评论 -
回调函数案例
内置数据类型:#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>//回调函数---通用数组型打印案例//参数1:数组首地址 参数2:数组中每个元素的大小 参数3:数组元素个数 参数4:回调函数对数组内容进行输出void printArray(void* array, int size, int len, void(*MyPrint)(void *)){//void*没有数组的概念,因此如果想通用可以用char*,如果用int*的话原创 2021-02-26 16:06:25 · 206 阅读 · 0 评论 -
函数指针做函数参数(回调函数)
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>//回调函数//第一个参数是一个万能指针,用来接收用户输入的数据------因为我们不知道用户会输入什么样的数据类型,因此用万能指针//第二个参数是一个函数指针-------将data数据放入函数中进行操作void PrintText(void* data,void(*myPrint)(void* )){ myPrint(data);}//做回调函数的第二个参数void myPri原创 2021-02-26 14:21:43 · 842 阅读 · 2 评论