目录
DAY_1
-
类是一种规范,它描述新型的数据格式
-
对象是根据这种规范所构造的特定数据结构
-
C++ 通常使用自下向上编程方法,与 C 自顶向下的编程方法相反
-
泛型编程可以认为是一种不指定数据类型的实现方法,使用泛型编程的代码一般被称为模板,由模板生成实际代码的过程称为模板的具体实现。
-
C++ 最新的标准是 ISO/IEC 14882:2020,于2020年发布,该标准引入了许多新特性,如协程,模块等,可以查看c语言最新标准c22,C++20标准(c++标准手册) 官方最新版PDF_柳编的博客-CSDN博客
-
C++ 的输入与输出
/* * 不同于C语言的stdio.h库,C++使用iostream库作为交互库 * C++使用函数 cout << "string" 进行内容输出 * cout << endl 可以建立新行,同样的,也可以使用转义符'\n' * 当然C++不仅能输出字符串,也能够输出数值类型 */ #include <iostream> using namespace std; int main() { // //你可以使用cout输出字符串,可以连续输出 // cout << "因为还跟着输出了endl,所以会新起一行!" << endl; // cout << "好帅啊!" << endl; // //当然也可以单独输出 // cout << "你真的好帅啊!"; // cout << endl; // cout << "你真的好帅啊12!"; // //你也可以使用C语言中的'\n' // cout << "\n"; // cout << "你真的好帅啊13!"; // //同样的,你可以用来输出变量,乃至于后面,你可以输出自定义数据类型 // int carrots = 25; // cout << carrots << endl; // cout << "carrots." << endl; // cout << "Now I have " << carrots << " carrots" << endl; // //现在我们来看看cin的用法,与cout相反,我们使用 cin >> 变量 作为输入 // int carrots; // cout << "Please input number of carrots:"; // cin >> carrots; // cout << "I have " << carrots << " carrots."; return 0; }
-
C++ 的函数使用
/* * C++中使用函数的方法与C中类似 * 你可以使用标准库中的函数,当然你也可以使用自己的函数 */ #include <iostream> #include <cmath> using namespace std; // Function Definition void simon(int); int calculate(int); int main(){ // //求平方根函数 // double area; // cout << "Please input a number:"; // cin >> area; // double side; // side = sqrt(area); // cout << "side is " << side << endl; // //使用自己定义的函数,在main前声明,在main后实现,在main中调用 // simon(3); // // int result; // result = calculate(5); // cout << result; return 0; } void simon(int n){ cout << "This function printf " << n << endl; } int calculate(int n){ int result; result = n*n; return result; }
-
C++ 中的变量命名一般使用下划线或者小驼峰,如name_of_student,或者nameOfStudent,并且C++ 的变量名不区分大小写,但是请不要这样使用,易读性和统一是命名变量唯二的标准
-
你也可以使用前缀n,c,str,b,p 等来说明变量的类型,增强可读性
-
C++ 中的数值类型
// // C++中的数值类型 // C++对比C语言,其变量的初始化多出了一种方式,int value(32); // 更为常用的,C++标准引入了新的变量初始化方式,即使用大括号 // int value = {num}; 将value初始化值设为num,若不写num而保留空括号,则默认初始化为0 // // #include <iostream> #include <climits> using namespace std; int main(){ // int n_int = INT_MAX; // short n_short = SHRT_MAX; // long n_long = LONG_MAX; // long long n_llong = LONG_LONG_MAX; // // cout << "int is " << sizeof (int ) << " bytes" << endl; // cout << "short is " << sizeof (short ) << " bytes" << endl; // cout << "long is " << sizeof (long ) << " bytes" << endl; // cout << "long long is " << sizeof (long long) << " bytes" << endl; // // cout << "Maxinum values:" << endl; // cout << "int: " << n_int << endl; // cout << "short: " << n_short << endl; // cout << "long: " << n_long << endl; // cout << "long long: " << n_llong << endl; // // cout << "Minium int values = " << INT_MIN << endl; // cout << "Bits per byte = " << CHAR_BIT << endl; // int value(32); // cout << value << endl; // //char 类型的有无符号根据实现定义 // char ch; // unsigned char u_ch; // signed char s_ch; // char16_t ch1 = u'q'; // char32_t ch2 = U'\U0000222B'; // // cout << UCHAR_MAX << endl; // cout << SCHAR_MAX << endl; // cout << ch1 << endl; // cout << ch2 << endl; // //浮点数分为32位float,64位double,常用的128位long double // //他们的默认小数点保留位可以在头文件cfloat中查看 // float fnum1 = 1.23e3; // double fnum2 = 1.25e10; // long double fnum3 = 1.26e18; // // //浮点常量的定义,默认定义为double // float a = 1.25f; //使用后缀f来定义为float类型 // long double b = 1.2l; //使用后缀l来定义long double类型 // //运算符的重载 // cout << 9/5 << endl; // cout << 9.0f/5.0f << endl; // cout << 9l/5l << endl; // cout << 9.0/5.0 << endl; return 0; }
-
cout函数输出同一个数值的不同数据形式
#include <iostream> using namespace std; int main() { // cout输出同一个数的不同格式 cout << hex << 42 << endl; cout << oct << 42 << endl; cout << dec << 42 << endl; return 0; }
-
cout函数的put方法输出字符
#include <iostream> using namespace std; int main() { // cout的put方法 char ch = 'K'; cout.put(ch); return 0; }
-
char的有无符号定义
#include <iostream> #include <climits> using namespace std; int main(){ //char 类型的有无符号根据实现定义 char ch; unsigned char u_ch; signed char s_ch; char16_t ch1 = u'q'; char32_t ch2 = U'\U0000222B'; cout << UCHAR_MAX << endl; cout << SCHAR_MAX << endl; cout << ch1 << endl; cout << ch2 << endl; return 0; }
-
C++ 中的类型转换
#include <iostream> #include <climits> using namespace std; int main(){ float fnum = 53.2; //类型强制转换 int convert = static_cast <int> (fnum); cout << convert << endl; //使用强转显示ASCII值 int decchac = static_cast <int> ('O'); cout << decchac << endl; return 0; }
DAY_2
-
C++ 中的其他类型
// // 除去数值类型以外的其他类型数据 // #include <iostream> #include <cstring> using namespace std; int main(){ // // 数组的定义 // int array[12]; //定义一个int型数组,数组名array,数组元素数量12 // // 数组的初始化 // int array_init[3] = {1, 2, 3}; // int array_zero[100] = {0}; // int array_nofull[5] = {1, 2}; // int array_nonum[] = {1, 2, 3, 4}; // int array_nothing[] = {}; // // char tlifs[4] = {'h', 'i', 112, '\0'}; // //char slifs[4] = {'h', 'i', 1122211, '\0'}; //Error, 1122211越界 // char str[] = {'h', 'e', 'l', 'l', 'o', '\0'}; // // cout << strlen(str) << endl; // // // cin的按行输入 // char name[20] = {}; // // 第一种方式 // cout << "Please input your name:"; // cin.getline(name, 20); // cout << name << endl; // // 第二种方式 // cout << "Please input your name:"; // cin.get(name, 20).get(); // cout << name << endl; // // C++中的string类 // string str1; // string str2 = "Hello world!"; // // cout << "Please input your name:"; // cin >> str1; // cout << str2[2] << endl; // cout << str1 << endl; // // string str3 = str1 + str2; // cout << str3 << endl; // // // 其他类型的字符串字面值 // wchar_t title[] = L"Chief"; // char16_t name[] = u"Snake"; // char32_t car[] = U"Tesla"; // // // C++的原始字符串 // // 使用R来标识,不代表转义字符等,而直接就是字符串 // cout << R"(JIm "King" Tutt use "\n" instead of me)" << endl; // // 使用+*来代表字符串的开头和结尾,以使用() // cout << R"+*("(Who wouldn't?)" , she whispered.)+*" << endl; // // 结构体 // struct student{ // string name; // int age{}; // float grade{}; // }; // // student me; // me.age = 17; // me.name = "KillerDJ"; // me.grade = 98.5; // // cout << me.grade << me.name << me.age << endl; // // // 结构体的初始化 // student you = { // "赵子龙", // 17, // 95.5 // }; // // student him = {}; // 全部置0 // // 结构体成员赋值在C++中是允许的 // him = you; // cout << him.age << endl; // // // 结构数组的定义 // student others[100] = { // {"赵丽龙", 15, 89}, // {"彼得", 14, 66}, // }; // // cout << others[0].age << endl; // cout << others[2].age << endl; // // // 结构中的位字段 // struct reg{ // unsigned int SN : 4; // unsigned int : 4; // bool goodIn : 1; // }; // // reg stm32 = {14, true}; // cout << stm32.goodIn << endl; // // 联合 // // 联合可以定义多种不同的数据类型,但是只能存储其中一种 // //多用于单片机应用中 // union type{ // unsigned int itype; // float ftype; // char ctype; // }; // // type one{}; // one.itype = 100; // one.ftype = 120.5; // one.ctype = 'H'; // // cout << one.itype << endl; // cout << one.ftype << endl; // cout << one.ctype << endl; // // 枚举 // // 可以不定义变量名,只是用enum,省略week // enum week{ // Mond, // Tues, // Wenne, // Thurs, // Fri, // Satur, // Sun // }; // // week day; // day = Mond; // cout << day << endl; // // 因为Jan不是枚举中的值,所以不能定义 // //day = Jan; // day = Satur; // cout << day << endl; return 0; }
DAY_3
- C++ 中的指针
//
// 指针是一个变量,其存储的不是具体的值,而是该值所在内存空间的地址
// 指针存储在Stack中
//
#include <iostream>
#include <vector>
#include <array>
using namespace std;
int main(){
// 获取变量的指针
int a = 5;
int* p1 = &a; //定义一个整形指针变量,其值为变量a的地址
// C 程序员多使用下面这种书写方式,二者没有任何区别
//int *p1 = &a; //定义一个整形指针变量,其值为变量a的地址
cout << p1 << endl;
cout << *p1 << endl; //使用*p1表示取指针值,这叫做解引用
// // 请尽量不要使用下面这种方式
// int *p2; // 此处而不对其进行指向地址的初始化
// *p2 = 5; // 那么这个5到底存储到哪一块内存了呢,虽然他不会报错
// 那么你可以手动给指针p设置地址吗?答案是可以的
int* p3;
//p3 = 0xB8000000; // 此处报错,原因是类型不匹配
p3 = (int *)0xB8000000; //现在可以了
cout << p3 << endl;
// 使用new来分配内存
int* p4 = new int; // 我们把这块变量叫做数据对象
// 通用内存分配,从Heap分配,指针存储在Stack中
//typeName* pointer_name = new typeName;
*p4 = 1000;
cout << p4 << endl;
cout << *p4 << endl;
// 使用delete来释放内存,注意他只能释放new分配的内存
delete p4;
// 虽然内存被释放,但是指针还存在,此时p4是空指针
cout << p4 << endl;
cout << *p4 << endl;
// 使用new来创建动态数组
// psome返回数组第一个元素的地址
int* psome = new int[10];
psome[0] = 1;
psome[1] = 2;
psome[2] = 3;
psome[3] = 4;
psome[4] = 5;
psome[5] = 6;
cout << psome[0] << endl;
cout << psome[1] << endl;
cout << psome[2] << endl;
cout << psome[3] << endl;
cout << psome[4] << endl;
cout << psome[5] << endl;
cout << "**************" << endl;
cout << psome << endl;
cout << *psome << endl;
// 指针增加是跳转到下一个值的地址处,与传统的加一不同,减少同理
psome++;
cout << psome << endl;
cout << *psome << endl;
// 使用完动态数组时,要对其进行释放
// 注意,如果要进行释放,你要从最开始定义处的地址开始释放
psome--;
delete [] psome;
// 指针数组
short x = 4, y = 5, z = 6;
short *p[3] = {&x, &y, &z};
cout << "**************" << endl;
cout << p << endl;
cout << p[0] << endl;
cout << p[1] << endl;
cout << p[2] << endl;
cout << *p[0] << endl;
cout << *p[1] << endl;
cout << *p[2] << endl;
// 数组指针
short tell[10] = {10, 2, 3};
short (*pas)[10] = &tell;
cout << *pas[0] << endl;
// 动态数组vector
vector<int> vi(5);
// 模板类array
array<int, 5> ai = {};
array<int, 5> ai2 = ai;
cout << ai2[2] << endl;
ai2.at(2) = 456;
cout << ai2[2] << endl;
return 0;
}
DAY_4
- C++ 中的循环语句
//
// C++ 中的那些循环语句(迭代语句)
//
#include <iostream>
using namespace std;
int main(){
// for循环
for (int i = 0; i < 5; i++){
cout << i << endl;
}
// 使用for循环来输入值
{
int x;
cout << "请输入x的值(当x=0时将会无限循环):";
for (cin >> x; x == 0; cin >> x){
printf("你输入的是0,请继续输入\n");
cout << "Please input x:";
}
}
//下面是一个for死循环
// for (;;){
// cout << "死循环" << endl;
// }
// 注意变量i是在for循环体中初始化的,它的作用域只在for循环体中,所以你不能在外部作用域使用它
// 如果你想继续使用变量i,请在外部作用域定义该变量
// cout << i << endl;
// while循环
// 当while括号中的值为true才回执行块语句
{
int i = 0;
while(i < 5){
cout << i << endl;
i++;
}
// 下面是一个while死循环
// while(1){
// cout << "无尽循环" << endl;
// }
}
// do..while循环
// 当while括号中的语句为真时才会继续执行块语句
// 其与while循环不同的是,do..while循环至少会执行一次
// 所以它常常用作某些程序实现的入口
{
int i = 0;
do{
cout << i << endl;
i++;
}while(i < 5);
}
return 0;
}
DAY_5
-
C++ 中的条件运算符
// // C++ 中的条件运算符 // #include <iostream> #include <cctype> #include <fstream> using namespace std; int main(){ // if else if else 运算符 // 省略,不作说明 // cctype的使用 char ch; cout << "Input:" << endl; cin.get(ch); if (isalpha(ch)){ cout << "Yes" << endl; } else{ cout << "No" << endl; } // swtich语句,省略 // continue, break 省略 // 把数据写入文件 ofstream outfile; outfile.open("fish.txt"); string str = "HHHHHHHHLLLLELELEIQOJE"; outfile << str; outfile.close(); //把文本数据读入 ifstream infile; infile.open("fish.txt"); string in_str; infile >> in_str; cout << in_str << endl; infile.close() return 0; }
DAY_6
- C++ 的函数与指针
#include <iostream>
using namespace std;
// Function Definition
int mprint(const int*, const int*);
int main(){
// 使用数组区间指针来作为参数
int month[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int total = mprint(month, month+12);
cout << total << endl;
// 常量指针
int value = 5;
const int* p1 = &value; // 这定义了一个常量指针
*p1 = 6; // 这代表我们不能通过该常量指针修改其指向的值
p1++; // 但是指针是可变的
// 指针常量
int* const p2 = &value; //这定义了一个指针常量
p2++; // 这个指针永远指向已经value的地址,他不能被改变
*p2 = 6; // 但是可以通过*p2修改值
// 函数指针
// 函数指针比较复杂,此处不作详细说明,后续说明
return 0;
}
int mprint(const int* begin, const int* end){
const int* p;
int total = 0;
for(p = begin; p!= end; p++)
total = total + *p;
return total;
}