C++学习笔记系列(一)

    xiaoxiao2022-07-05  158

    第一章

    1、在C++中,可不显示地指定变量类型,使用关键字auto 例如:auto flag=true。这里将指定变量flag的类型交给了编译器,编译器会自动确定变量应为什么类型。PS:auto时必须将变量初始化,否则会出现编译错误。2、使用enum来定义变量只有一组特定的取值 例如:

    enum RainbowColors { violet=0, Indigo, Blue, Green, Yellow, Orange, Red };

    RainbowColors MyWorldsColor = blue ; #声明了常量MyWorldColor,这个常量只能取RainbowColors的值,声明枚举常量时,编译器把枚举值(voilet等)转化为整数,每个枚举值都比前一个大1.可以自己指定初始值,没有指定的话初始值为0.3、为减少内存的占用,可以用std::vector来定义动态数组。需要包含头文件#include<vector> 例如:简要代码如下

    #include<iostream> #include<vector> using namespace std; int main() { vector<int> DynArrNums(3) #数组初始长度为3 int AnotherNum=0; cin>>AnotherNum; DynArrNums.push_back(AnotherNum) #使用这个函数将这个数字压入到矢量中 }

    4、C++字符串的使用 需要使用头文件#include<string>才能使用string 变量名定义字符串变量。 例如:

    #include<iostream> #include<string> using namespace std; int main() { string Greetings("Hello,std:string!"); cout << Greetings << endl; cout << "enter a line o text:" << endl; string FirstLine; getline(cin, FirstLine); cout << "enter another" << endl; string SecLine; getline(cin, SecLine); cout << "concatenation:" << endl; string Contact = FirstLine + " " + SecLine; cout << Contact << endl; return 0; }

    5、语句、运算符 要将一条放到两行中,可在第一行末尾添加反斜杠(\),也可将字符串字面量分成两个,如下例:

    cout << "hello \ world" << endl; 或是: cout << "Hello" "world" <<endl;

    使用后缀运算时,先将值赋给左值,再将右值递增或递减,左值都为执行前的旧值;使用前缀运算就相反,先将值递增或递减,再将结果赋给左值。

    ++变量名 一般优于 变量名++

    #include<bitset> cin>>InputNum; bitset<8> InputBits (InputNum) #转换为二进制 bitset<8> BitwiseNOT = (~InputNum) #按位运算符Not bitset<8> BitwiseAND = () #AND运算 BitwiseOR = () BitwiseXOR=() #异或运算

    6、控制流程序 字符初始化一般用:char userselection= '\0; 死循环一般用来检测操作系统USB接口是否连接了设备,只要系统一直在运行,这种活动就不会停止。 一个函数可以包含多条return语句。7、函数重载 名称和返回类型相同,参数不同的函数称为重载函数。在应用程序中,如果使用不同的参数调用具有特定名称和返回类型的函数,重载函数将很有用。按引用传递函数 即不是以返回值的方式而是以引用参数的方式提供给函数,如下所示:

    #include<iostream> #include<string> using namespace std; const double Pi = 3.1416; void Area(double radius, double &result) { result = Pi * radius *radius; } int main() { cout << "enter radius:"; double radius = 0; cin >> radius; double AreaFet = 0; Area(radius, AreaFet); cout << "the area is:" << AreaFet << endl; return 0; }

    8、内联函数 当定义一个函数时,执行函数的开销有可能非常高,所以使用关键字inline可以节省内存空间,将函数的内容直接放到它调用的地方,以提高代码的执行速度。但是应尽量少用关键字inline。

    inline long DoubleNum(int InputNum) { description; }

    9、lambda函数 lambda函数语法如下:[optional parameters] (parameter list){statements;} 代码如下:

    #include<iostream> #include<vector> #include<algorithm> using namespace std; void DisplayNums(vector<int>&DynArray) { for_each(DynArray.begin(), DynArray.end(), \ [](int Element) {cout << Element << " "; }); cout << endl; } int main() { vector<int>MyNumbers; MyNumbers.push_back(501); MyNumbers.push_back(-1); MyNumbers.push_back(25); MyNumbers.push_back(-35); DisplayNums(MyNumbers); cout << "sorting them in descend orders" << endl; sort(MyNumbers.begin(), MyNumbers.end(), \ [](int Num1, int Num2) {return (Num2 < Num1); }); DisplayNums(MyNumbers); return 0; }

    当定义函数提供形式参数时,要将所有有默认参数值的参数放在列表末尾,要么给所有参数都指定默认值。

    最新回复(0)