栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++个人笔记

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++个人笔记

C++个人笔记

文章目录

C++个人笔记

1. 基础性了解

引用与隐形变量inline函数auto函数vectorconst+指针 与 指针操作动态内存分配输出格式控制 2. 基本操作(六大内置函数)

面试题六大成员函数
大一新生,随缘更新
写笔记软件 typora
上传平台: https://gitee.com/Allorry/cloud-notes

1. 基础性了解

 ```c++
 #include
 #include
 namespace bit {
 	int scanf = 10;
 	int strlen = 20;
 }
 int main()
 {
 	printf("%dn", bit::scanf);
 	printf("%dn", bit::strlen);
 }
 ```

1.     stu::cout<< a < 
引用与隐形变量 
int a=0; int& b=a; int& c=a; int& d=b;   b=10; 则abcd全为10
    
const int a=10; // int& b=a 报错  引用b属于权限放大,能读能写
const int& b=a; // 在a不可修改的情况下,必须承诺别名b也不可能修改

int c=10;  const int& d =c; //不报错, 引用d属于权限缩小,只读但不写

int* p=NULL; int*& q=p; //q是p的别名
int c=10; double d=1.1;  (产生临时变量double e=?) d=c;  (e=c; d=e;)
临时变量具有常性,引用时不可修改。所以引用时要加const:
const double &f=c;  (e=c; f=e)

以下介绍一个传引用返回,但注意,传回来的是一个已经销毁的变量的别名,实际上是不合法的

**但若把 int c 改为 static int c = a+b; c存储在静态区,则程序合法 **

inline函数
inline int ADD(int a, int b){     //在常用函数前加inline,没有栈帧开销
    return a+b;
}
auto函数
int a=0; auto b=a;  //自动推理出b是int
vector
#include  //以下均为举例,只可意会不可言传
    vector v1;
	vector v2(v1);
	vector v3(10); //开10个空间,默认全是0  vectorv3(10,0);
	vector v8(3, "hi");
	v3.push_back(4);
    
    int a[]={1,2,3,4,5,6};
    vector va(a,a+6);
    void Sort(vector &v);

//sort排序
    sort(v.begin(), v.end());
//函数引用
  int a[]={1,2,3,4,5,6};
    vector va(a,a+6);
    void Sort(vector &v);
    vector& Add(vector& a, vector& b){
        return a;
    }

	vector v6{ ("my","name","is","fuck you") };
	for (vector::iterator i = v6.begin(); i != v6.end(); i++) {
		cout << *i << endl;
	}   //还可以反转打印
	for (vector::reverse_iterator i = v6.rbegin(); i != v6.rend(); i++) {
		cout << *i << endl;
	}

//二级向量
	vector< vector >vv(3, vector (4));  //三行四列
	vector< vector >vv;
	for (int i = 0; i < 4; i++) {
		vv.push_back(vector(i,5));  //每一行开辟的空间数量都不一样
	}
const+指针 与 指针操作
const char* p1 = "wode";  //等同于 char const *p1; 不可修改指向的东西的值
int c = 1;
int* const p2 = &c;   //不可修改指向啥东西


void f(int* a, int* b, int(*p)(int));
int main()
{
    int** data;
    data = new int* [3];
    for (int i = 0; i < 3; i++) {
        data[i] = new int[4];
    }


    int fact(int);
    int i = 5, j = 6;
    int(*gp)(int) = fact;
    f(&i, &j, gp);
    f(&i, &j, fact);
  
}
动态内存分配
typedef int Type;
	Type* pointer = new Type;
	//...
	delete pointer;
    delete[] pointer;

	Type* pointer = new Type[10];
	Type** arr = new int* [10];
	for (int i = 0; i < 10; i++) {
		arr[i] = new int[10];
	}
	//...
	
输出格式控制
#include
    cout << (2 > 3) << boolalpha <<' '<<(2>3) << endl;
	cout << hex << 18 << ' ' << showbase << 18 << endl;
	cout << dec << 255 << ' ' << uppercase << 255 << endl;
	cout << showpos << 12 << endl;    //显示符号位+
	cout << scientific << 123456.78 << endl;


	cout << setw(8) << "C++" << setw(6) << 101 << endl;  //设定宽度
	double num = 1.23456;
	cout << setprecision(2) << num << endl;  //指定一个浮点数的精度,2是总位数
	cout << fixed << 123456.78 << endl;   //强制数值不以科学技术法显示,即只用小数形式显示
	cout << showpoint << 1.23 << endl;//t强制小数后面以0显示

	cout << left;
	cout << setw(8) << 1.23 <<'#'<< endl;
	cout << setw(8) << 1.23456 << endl;
2. 基本操作(六大内置函数) 面试题
class A                              //问:此代码能否正确运行?
{
public:
    void printA(){                   //答:不能。p为空,this接收为空,this->_a出错。对象存储公共变量;
        cout<<_a<show()等等函数位于公共代码段,无需解引用,show()不会出错
    }
    void show(){
        cout<<"show()"<printA();
    p->show();
}
六大成员函数
#include
#include
#include
using namespace std;
class Date {
public:
	//构造函数,对内置类型完成赋值,不管自定义类型,大部分情况下我们需要自己写
	Date(int year = 2022, int month = 4, int day = 1) {
		_year = year;
		_day = day;
		_month = month;
	}
	//拷贝构造函数,只完成浅拷贝
	Date(Date& d) {
		_year = d._year;
		_month = d._month;
		_year = d._day;
	}
	//赋值运算符重载,为了满足连等需求,不用void返回
	Date& operator=(const Date& d) {
		if (this != &d) {
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
private:
	int _year;
	int _month;
	int _day;
};


class Stack {
public:
	Stack(int capacity = 4) {
		if (capacity == 0) {
			_a = nullptr;
			_size = _capacity = 0;
		}
		else {
			_a = (int*)malloc(sizeof(int) * capacity);
			_capacity = capacity;
			_size = 0;
		}
	}
	//析构函数,清理数据
	~Stack() {
		free(_a);
		_capacity = _size = 0;
		_a = nullptr;
	}
	//拷贝构造函数,需要自己写
	Stack(Stack& s) {
		_size = s._size;
		_capacity = s._capacity;
		int* t = (int*)malloc(sizeof(int) * _capacity);
		memcpy(t, s._a, sizeof(int) * _size);
	}

private:
	int* _a;
	int _size;
	int _capacity;
};
int main()
{
	Date d1(2020, 5, 31);
	Date d2;

}

tack(Stack& s) {
_size = s._size;
_capacity = s._capacity;
int* t = (int*)malloc(sizeof(int) * _capacity);
memcpy(t, s._a, sizeof(int) * _size);
}

private:
int* _a;
int _size;
int _capacity;
};
int main()
{
Date d1(2020, 5, 31);
Date d2;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号