我需要在最短时间捡起 C++,以备刷题

简介

  • 应用:系统、图形用户界面、嵌入式系统、游戏开发

  • C 是 C++ 的子集(可粗略这样看)

  • C++ 为程序员提供了对系统资源和内存的高级控制

IDE: vscode,Code::Blocks,Eclipse,Visual Studio

**编译器:**gcc(g++)

vscode 配置环境

参见《命令行》Linux实践,配置过程完全一样

  • 提前下好编译器(网上博客很多推荐的)
  • 除了连接wsl其他都一样

基础

语法

输出:cout << (不会在输出末尾插入新行)

输入:cin >>

插入新行:\nendl

注释:

  • 单行://
  • 多行:/**/

常量:const

数据类型

整型:

类型 大小 范围
int 4B $-2^{31}$ ~ $2^{31}-1$:大约$2.1\times10^{9}$
unsigned int 4B $0$ ~ $2^{32}-1$:大约 $4.2\times10^{9}$
long long 8B $-2^{63}$ ~ $2^{63}-1$:大约$9.2\times10^{18}$
unsigned long long 8B $0$ ~ $2^{64}-1$:大约$1.8\times10^{19}$

字符型:

类型 大小 范围
char 1B $-128$ ~ $127$

布尔型:

类型 大小 范围
bool 1B true/false

浮点型:

类型 大小 范围
double 8B 大约$10^{307}$,精度15位有效数字
float 4B 大约$10^{31}$,精度6位有效数字

字符串型

运算符

运算符 描述
&= 按位与赋值
` =`
^= 按位异或赋值
>>= 右移赋值
<<= 左移赋值

字符串型

C++ 中的字符串实际上是一个对象

头文件:#include <string>

拼接:

  • +
  • append()

长度:length()size()

访问、更改:

  • 索引 []
  • .at()

转义:\'\"\\

  • \n:换行
  • \t:Tab

输入:

  • cin >>:将空格(空格、制表符等)视为终止字符
  • getline(cin, fullname):读取一行文本

省略命名空间:using namespace std; 竞赛必用

C样式字符串:char greeting2[] = "Hello";

数组

声明:

  • string cars[4]; 省略数组元素
  • string cars[] = {"Volvo", "BMW", "Ford"}; 省略数组大小
  • int myNum[3] = {10, 20, 30};

大小:

  • sizeof() 返回 字节
  • 多少个元素:sizeof(myNumbers) / sizeof(myNumbers[0]

结构体

创建:struct + 结构体名

1
2
3
4
struct {             // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
} myStruct1, myStruct2, myStruct3; // Structure variable

访问:.

命名结构:

1
2
3
4
5
6
struct myDataType { // This structure is named "myDataType"
int myNum;
string myString;
};

myDataType myVar;

enum

enum 表示一组常量(不可更改的值)

创建:

1
2
3
4
5
enum Level {
LOW,
MEDIUM,
HIGH
};

声明:enum Level myVar = HIGH;

访问:

  • enum 第一项为0,第二项为1,以此类推
  • 可以更改值

引用

&:对现有变量的“引用”

引用变量:

1
2
3
4
5
string food = "Pizza";
string &meal = food;

cout << food << "\n"; // Outputs Pizza
cout << meal << "\n"; // Outputs Pizza

获取地址:

1
2
3
string food = "Pizza";

cout << &food; // Outputs 0x6dfed4

指针

指针:将内存地址存储为其值的变量,指向相同类型的数据类型

创建:*

1
2
3
4
5
6
7
8
9
10
string food = "Pizza";  
string* ptr = &food;

cout << food << "\n"; // Pizza
cout << &food << "\n"; // 0x5ffe80
cout << ptr << "\n"; // 0x5ffe80
cout << *ptr << "\n"; // Pizza

*ptr = "Hamburger";
cout << *ptr << "\n"; // Hamburger

解引用:*

  • 使用指针来获取变量的值
  • 修改原始变量的值

函数

**重载:**相同的名称但具有不同的参数(数量和、类型)

1
2
3
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)

**局部变量:**函数内

**全局变量:**都可访问(同名:1个函数内用 - 1个函数外用)

创建:class

访问说明符:public

1
2
3
4
5
class MyClass {      
public:
int myNum;
string myString;
};

方法

  • 内部定义:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    class MyClass {        
    public:
    void myMethod() {
    cout << "Hello World!";
    }
    };
    int main() {
    MyClass myObj;
    myObj.myMethod(); d
    return 0;
    }
  • 外部定义:::

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class MyClass {        
    public:
    void myMethod();
    };
    void MyClass::myMethod() {
    cout << "Hello World!";
    }
    int main() {
    MyClass myObj;
    myObj.myMethod();
    return 0;
    }

构造函数

可以在类外部定义

  • 无参构造
  • 含参构造

访问说明符

  • public:成员可从类外部访问
  • private:无法从类外部访问(默认)
  • protected:不能从类外部访问,可以在继承的类中访问

封装

  • 类的声明:private
  • getset 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

class Employee {
private:
int salary;

public:
void setSalary(int s) {
salary = s;
}
int getSalary() {
return salary;
}
};

int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}

继承

:

继承方式:

  • public:父类的 public 成员仍然是 publicprotected 仍然是 protected
  • protected:父类的 public 成员变为 protectedprotected 保持不变
  • private:父类的 publicprotected 成员变为 private

父类的 private :仍然存在于子类中,但无法直接访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};
class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};

class MyChildClass: public MyClass, public MyOtherClass {
};
int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
}

多态

方法重写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n";
}
};

class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n";
}
};

class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n";
}
};

文件

调用库:#include <iostream>#include <fstream>

描述
ofstream 创建写入文件
ifstream 读取文件
fstream 创建读取写入文件

创建:ofstreamfstream

写入:MyFile << "...";

关闭:MyFile.close();

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
using namespace std;

int main() {
ofstream MyFile("filename.txt");

MyFile << "Files can be tricky, but it is fun enough!";

MyFile.close();
}

读取:

  • ifstreamfstream
  • while + getline
1
2
3
4
5
6
7
8
9
string myText;

ifstream MyReadFile("filename.txt");

while (getline (MyReadFile, myText)) {
cout << myText;
}

MyReadFile.close();

异常

  • try:定义代码块,在执行代码块时对其进行错误测试
  • throw:检测到问题时引发异常
  • catch:在 try 块中发生错误时要执行的代码块(与 try 成对出现)
1
2
3
4
5
6
7
try {

throw exception;
}
catch () {

}

日期