西嘎嘎学习-day 1
C++ 标识符
C++标识符是用于命名程序元素(变量、函数、类、对象等)的名称,必须遵循一定的命名规则。
1.组成规则:
标识符必须以字母(a-z、A-Z)、下划线_、或 $ 开头。
剩下的字符可以是字母、数字(0-9)、下划线、$。
不能用关键字来作为标识符。
2.大小写敏感:
C++区分大小写,所以 MyVar 和 myvar 是不同的标识符。
3.命名建议:
通常,变量名用小写字母,类名用大写字母开头,函数名通常也以大写字母开头。
4.长度限制:
虽然C++标准没有规定标识符的长度限制,但编译器可能会对标识符的长度有所限制,所以标识符不要太长。
5.有效标识符实例:
myVariable 、 _privateVar 、 Var123 、 CLassName
6.无效标识符实例:
123var(以数字开头)、my var(包含空格) 、int (关键字)
C++关键字
C++关键字是C++系统中预定义的、在语言或编译系统的实现中具有特殊含义的单词。
1.asm
用途:允许在 C++ 程序中嵌入汇编代码。
示例:
asm("mov eax, 5")
2.auto
用途:自动推断变量类型(C++ 及以后)。
示例:
auto x = 5; // x是 int 的类型
auto y = 3.14; // y是 double 的类型
3.bool
用途:布尔类型,表示 true 或 false。
示例:
bool flag = true;
if (flag)
{// 执行代码
}
4.break
用途:跳出循环或 switch 语句。
示例:
for (int i = 0; i < 10; ++i)
{if(i == 5){break; // 当 i = 5 时跳出循环}
}
5.case
用途:switch 语句中的情况匹配。
示例:
switch (day)
{case 1: cout << "Monday" ; break;case 2: cout << "Tuesday" ; break;// 其他情况
}
6.catch
用途:与 try 一起用于异常处理。
示例:
try
{// 这里是可能抛出异常的代码
} catch (const exception& e) {// 处理异常}
7.char
用途:字符类型,存储单个字符。
示例:
char c = 'A';
8.class
用途:定义类,面向对象编程的基础。
示例:
class MyClass
{public:void myMethod();
};
9.const
用途:声明变量,防止修改。
示例:
const int MAX = 100;
10.const_cast
用途:修改 const 或 volatile 属性。
示例:
const int* ptr = new int(5);
int* nonConstPtr = const_cast<int*>(ptr);
11.continue
用途:跳过当前循环迭代。
示例:
for (int i = 0; i < 10; ++i)
{if(i % 2 == 0){continue; //相当于只处理奇数}
}
12.default
用途:switch 语句的默认情况。
示例:
switch (day)
{case 1: cout << "Monday"; break;default: cout << "Unknown day"; break;
}
13.delete
用途:释放动态分配的内存
示例:
int* ptr = new int;
delete ptr;
14.do
用途:do-while 循环,保证至少执行一次。
示例:
do {//循环体
} while (condition);
15.double
用途:双精度浮点数。
示例:
double pi = 3.14159;
16.dynamic_cast
用途:运行时类型转换
示例:
Base* basePtr = new Derived();
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);
17.else
用途:与 if 一起处理条件不成立的情况。
示例:
if (condition)
{//处理条件为真的情况
} else {//处理条件为假的情况
}
18.enum
用途:定义枚举类型。
示例:
enum Direction { Up, Down, Left, Right };
19.explicit
用途:禁止隐式转换。
示例:
class MyClass {
public:explicit MyClass(int x) { /* ... */}
};
20.export
用途:导出模板,允许在其他编译单元中使用。
示例:
export template class MyTemplate<int>;
21.extern
用途:声明外部变量或函数。
示例:
extern int globalVar; // 这是声明
int globalVar; // 这是定义
22.false
用途:布尔假值。
示例:
bool flag = false;
23.float
用途:单精度浮点数。
示例:
float f = 3.14f;
24.for
用途:循环结构,适用于已知迭代次数。
示例:
for (int i = 0; i < 10; ++i)
{// 循环体
}
25.friend
用途:声明友元,允许访问私有成员。
示例:
class MyClass {friend void myFriendFunc();
};
26.goto
用途:无条件跳转。
示例:
goto label;
label:
// 跳转到此处
27.if
用途:条件判断。
示例:
if (condition)
{// 处理条件为真
}
28.inline
用途:建议编译器内联函数。
示例:
inline int add(int a, int b)
{return a + b;
}
29.int
用途:整数类型。
示例:
int num = 100;
30.long
用途:长整数类型。
示例:
long longNum = 100000L;
31.mutable
用途:允许修改 const 对象的成员变量。
示例:
class MyClass {
public:mutable int count;
};
32.namespace
用途:组织代码,避免命名冲突。
示例:
namespace MyNameSpace {void myFunc(){/* ... */}
}
33.new
用途:动态分配内存。
示例:
int* ptr = new int;
34.operator
用途:重载运算符
示例:
class MyClass {
pubilc:MyClass operator+(const MyClass& other){/* ... */}
};
35.private
用途:私有访问控制符。
示例:
class MyClass {
private:int data;
};
36.protected
用途:受保护的访问控制符。
示例:
class Base {
protected:int data;
};
37.public
用途:公有访问控制符。
示例:
class MyClass {
public:int data;
};
38.register
用途:建议变量存储在寄存器中。
示例:
register int i;
39.reinterpret_cast
用途:重新解释变量的类型。
示例:
int* ptr = reinterpret_cast<int*>(somePointer);
40.return
用途:从函数返回值。
示例:
int add(int a, int b)
{return a + b;
}
41.short
用途:短整型,占 2 字节,适用于内存有限的情况。
示例:
short s = 32767; // 短整型
42.signed
用途:默认有符号数,通常无需显示声明。
示例:
signed int si = -100; // 有符号整数
43.sizeof
用途:获取类型或变量的字节数,用于内存管理。
示例:
int arr[5];
cout << sizeof(arr) << endl; //输出 20 (int 占 4字节)
44.static
用途:静态变量或函数,作用域受限,适用于共享数据。
示例:
static int count = 0; // 静态全局变量
void func()
{static int x = 0; // 静态局部变量
}
45.static_cast
用途:类型转换,无运行时检查,需谨慎使用。
示例:
int i = 5;
char c = static_cast<char>(i); //将 int 转为 char
46.struct
用途:结构体,用于数据聚合,类似于类。
示例:
struct Point {int x;int y;
};
Point p = {1, 2};
47.switch
用途:多分支语句,简洁高效,处理整数类型。
示例:
int case1 = 2;
switch (case1) {case 1: cout << "Case 1"; break;case 2: cout << "Case 2"; break;case 3: cout << "Case 3"; break;default: cout << "Default";
}
48.template
用途:泛型编程,创建函数或类模板。
示例:
template<typename T>
T max(T a, T b)
{return a > b ? a : b;
}
49.this
用途:指向当前对象,用于成员函数。
示例:
#include <iostream>
using namespace std;class Person {
private:string name;int age;public:// 构造函数Person(string name, int age) {this->name = name; // 使用 this 来区分参数和成员变量this->age = age;}// 显示信息void showInfo() {cout << "Name: " << name << ", Age: " << age << endl;}// 链式调用示例Person& setName(string name) {this->name = name;return *this; // 返回当前对象的引用}Person& setAge(int age) {this->age = age;return *this;}
};int main() {Person p("Alice", 20);p.showInfo(); // 输出: Name: Alice, Age: 20// 使用链式调用p.setName("Bob").setAge(25).showInfo(); // 输出: Name: Bob, Age: 25return 0;
}
50.throw 和 try
用途:
throw:抛出异常,配合 try-catch 使用。
try:异常处理,包裹可能抛出异常的代码。
示例:
try
{throw "Error";
} catch (const char* e) {cout << e << endl;
}
51.typedef
用途:定义类型别名,简化复杂类型名。
示例:
typedef unsigned int uint;
int number = 100;
52.typeid
用途:获取对象的类型信息。
示例:
int x = 5;
cout << typeid(x).name() << endl; // 输出 int
53.typename
用途:告诉编译器名称是类型,用于模板。
示例:
template<typename T>
void func(T::iterator it)
{// 使用迭代器
}
54.union
用途:允许在同一内存位置存储不同的数据类型,与 struct 类似,但所有成员共享同一块内存空间,所以 union 的大小是其最大成员的大小。
示例:
union Data {int i;float f;char str[20];
};int main()
{Data data;data.i = 10;cout << "data.i:" << data.i << endl;data.f = 220.5;cout << "data.f:" << data.f << endl;strcpy(data.str, "Hello");cout << "data.str: " << data.str << endl;return 0;
}
55.using
用途:用于引入命名空间(namespace)中的内容,避免每次都要写完整的命名空间路径。
示例:
using namespace std;
56.virtual
用途:用于实现多态,通常用于类的成员函数,表示该函数可以在派生类中被重写。
示例:
class Base {
public:virtual void show() {cout << "Base class" << endl;}
};class Derived : public Base {
public:void show() voerride {cout << "Derived class" << endl;}
};int main()
{Base* b = new Deriver();b->show(); // 输出 "Derived class"return 0;
}
57.volatile
用途:告诉编译器该变量可能会被外部因素(硬件、中断、多线程)修改,因此不能进行优化。
示例:
volatile int flag = 0;int main()
{while (flag == 0){// 等待 flag 被外部修改}cout << "Flag changed! " << endl;return 0;
}
58.wchar_t
用途:宽字符类型,表示 Unicode 字符,通常用于支持多语言字符(如中文)。
示例:
#include <cwchar>int main()
{wchar_t ch = L '汉';wcout << L "字符是: " << ch << endl;return 0;
}
C++三字符组
1 定义
C++中的三字符组(Trigraphs)是一种用于在源代码中表示某些特殊字符的机制。由于某些键盘或字符编码不支持某些ASCII字符(如#
、^
、[
、]
等),C++标准引入了三字符组,允许开发者用三个连续的字符来替代这些特殊字符。
2 示例
三字符组由三个连续的问号???
开头,后跟一个字母,表示一个特定的字符。例如:
三字符组 | 表示字符 |
---|---|
???= | # |
???/ | \ |
???< | [ |
??> | ] |
??? | ^ |
???- | ~ |
`??? | ` |
??? | { |
???} | } |
3 场景
- 在某些老旧的终端或编辑器中,无法直接输入某些特殊字符。
- 在跨平台开发中,为了兼容不同编码环境。
- 在某些嵌入式系统或特殊硬件中,键盘布局限制了字符输入。
4 现况
在现代C++开发中,三字符组的使用已经非常少见,因为大多数现代编辑器和编译器都支持完整的ASCII字符集。