C++ 基本语法
变量和类型
| int a = 10; |
| double b = 20.5; |
| char c = 'A'; |
条件语句
循环语句
| for (int i = 0; i < 10; i++) { |
| |
| } |
| |
| while (a < b) { |
| |
| } |
函数
| int add(int x, int y) { |
| return x + y; |
| } |
类和对象
| class MyClass { |
| public: |
| int myNumber; |
| void myFunction() { |
| |
| } |
| }; |
| ```# C++ 示例说明基本语法 |
| |
| 本文档介绍了 C++ 的基本语法示例,帮助初学者快速上手 C++ 编程。 |
| |
| ## 变量和数据类型 |
| |
| ```cpp |
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| int myNum = 5; |
| double myFloatNum = 5.99; |
| char myLetter = 'D'; |
| string myText = "Hello"; |
| bool myBoolean = true; |
| |
| cout << myNum << endl; |
| cout << myFloatNum << endl; |
| cout << myLetter << endl; |
| cout << myText << endl; |
| cout << myBoolean << endl; |
| |
| return 0; |
| } |
条件语句
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| int x = 20; |
| int y = 18; |
| if (x > y) { |
| cout << "x is greater than y"; |
| } else { |
| cout << "x is not greater than y"; |
| } |
| return 0; |
| } |
循环
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| for (int i = 0; i < 5; i++) { |
| cout << i << "\n"; |
| } |
| return 0; |
| } |
函数
| #include <iostream> |
| using namespace std; |
| |
| void myFunction() { |
| cout << "I just got executed!"; |
| } |
| |
| int main() { |
| myFunction(); |
| return 0; |
| } |
类和对象
| #include <iostream> |
| using namespace std; |
| |
| class MyClass { |
| public: |
| int myNum; |
| string myString; |
| }; |
| |
| int main() { |
| MyClass myObj; |
| myObj.myNum = 15; |
| myObj.myString = "Some text"; |
| |
| cout << myObj.myNum << "\n"; |
| cout << myObj.myString; |
| return 0; |
| } |
数据类型
C++ 提供了多种数据类型,用于存储不同类型的值。以下是一些常见的数据类型:
数据类型 |
关键字 |
描述 |
整型 |
int |
存储整数(不带小数) |
浮点型 |
float |
存储单精度浮点数(带小数) |
双精度型 |
double |
存储双精度浮点数(带小数) |
字符型 |
char |
存储单个字符 |
布尔型 |
bool |
存储布尔值(true 或 false) |
字符串型 |
string |
存储字符串(需要包含 <string> 头文件) |
示例
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| int myNum = 10; |
| float myFloat = 5.75; |
| double myDouble = 19.99; |
| char myChar = 'A'; |
| bool myBool = true; |
| string myString = "Hello"; |
| |
| cout << "int: " << myNum << endl; |
| cout << "float: " << myFloat << endl; |
| cout << "double: " << myDouble << endl; |
| cout << "char: " << myChar << endl; |
| cout << "bool: " << myBool << endl; |
| cout << "string: " << myString << endl; |
| |
| return 0; |
| } |
常量
常量是固定值,在程序执行期间不会改变。这些固定值称为字面量。常量可以是任何基本数据类型的常量。
常量的定义
在 C++ 中,可以使用 const
关键字来定义常量:
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| const int myNum = 10; |
| const float myFloat = 5.75; |
| const char myChar = 'A'; |
| const string myString = "Hello"; |
| |
| cout << "int: " << myNum << endl; |
| cout << "float: " << myFloat << endl; |
| cout << "char: " << myChar << endl; |
| cout << "string: " << myString << endl; |
| |
| return 0; |
| } |
宏定义常量
可以使用 #define
预处理器指令来定义常量:
| #include <iostream> |
| #define PI 3.14159 |
| #define NEWLINE '\n' |
| using namespace std; |
| |
| int main() { |
| cout << "Value of PI: " << PI << NEWLINE; |
| return 0; |
| } |
变量作用域
变量的作用域是程序中变量可被访问的区域。C++ 中有三种基本的作用域类型:
- 局部变量
- 全局变量
- 静态变量
局部变量
局部变量是在函数或代码块内部声明的变量,只能在该函数或代码块内部访问。
| #include <iostream> |
| using namespace std; |
| |
| void myFunction() { |
| int myNum = 10; |
| cout << "myNum inside function: " << myNum << endl; |
| } |
| |
| int main() { |
| myFunction(); |
| |
| return 0; |
| } |
全局变量
全局变量是在所有函数外部声明的变量,可以在整个程序中访问。
| #include <iostream> |
| using namespace std; |
| |
| int myNum = 10; |
| |
| void myFunction() { |
| cout << "myNum inside function: " << myNum << endl; |
| } |
| |
| int main() { |
| cout << "myNum inside main: " << myNum << endl; |
| myFunction(); |
| return 0; |
| } |
静态变量
静态变量是在函数内部声明的变量,但使用 static
关键字声明。静态变量在函数调用结束后不会被销毁,而是保持其值直到程序结束。
| #include <iostream> |
| using namespace std; |
| |
| void myFunction() { |
| static int myNum = 0; |
| myNum++; |
| cout << "myNum is " << myNum << endl; |
| } |
| |
| int main() { |
| myFunction(); |
| myFunction(); |
| myFunction(); |
| return 0; |
| } |
字面量
字面量是程序中直接使用的常量值。C++ 中有几种不同类型的字面量:
- 整数字面量
- 浮点数字面量
- 字符字面量
- 字符串字面量
- 布尔字面量
整数字面量
整数字面量可以是十进制、八进制或十六进制。
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| int dec = 101; |
| int oct = 0145; |
| int hex = 0x65; |
| |
| cout << "Decimal: " << dec << endl; |
| cout << "Octal: " << oct << endl; |
| cout << "Hexadecimal: " << hex << endl; |
| |
| return 0; |
| } |
浮点数字面量
浮点数字面量可以是小数或指数形式。
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| float f1 = 3.14; |
| float f2 = 3.14e2; |
| |
| cout << "Float 1: " << f1 << endl; |
| cout << "Float 2: " << f2 << endl; |
| |
| return 0; |
| } |
字符字面量
字符字面量是用单引号括起来的单个字符。
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| char ch = 'A'; |
| |
| cout << "Character: " << ch << endl; |
| |
| return 0; |
| } |
字符串字面量
字符串字面量是用双引号括起来的一系列字符。
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| string str = "Hello, World!"; |
| |
| cout << "String: " << str << endl; |
| |
| return 0; |
| } |
布尔字面量
布尔字面量只有两个值:true
和 false
。
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| bool b1 = true; |
| bool b2 = false; |
| |
| cout << "Boolean 1: " << b1 << endl; |
| cout << "Boolean 2: " << b2 << endl; |
| |
| return 0; |
| } |
修饰符类型
C++ 提供了几种修饰符类型,用于修改基本数据类型的属性。常见的修饰符包括:
signed
- 有符号类型
unsigned
- 无符号类型
short
- 短类型
long
- 长类型
示例
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| short int si; |
| long int li; |
| signed int si_signed; |
| unsigned int ui; |
| |
| si = 32767; |
| li = 2147483647; |
| si_signed = -32768; |
| ui = 4294967295; |
| |
| cout << "Short int: " << si << endl; |
| cout << "Long int: " << li << endl; |
| cout << "Signed int: " << si_signed << endl; |
| cout << "Unsigned int: " << ui << endl; |
| |
| return 0; |
| } |
修饰符可以与基本数据类型组合使用,以满足不同的存储需求。
向量(Vector)
向量是 C++ 标准模板库(STL)中的一种序列容器,类似于动态数组。向量可以根据需要自动调整其大小。
向量的声明
| #include <iostream> |
| #include <vector> |
| using namespace std; |
| |
| int main() { |
| vector<int> myVector; |
| return 0; |
| } |
向量的初始化
| #include <iostream> |
| #include <vector> |
| using namespace std; |
| |
| int main() { |
| vector<int> myVector = {1, 2, 3, 4, 5}; |
| return 0; |
| } |
向量的常用操作
| #include <iostream> |
| #include <vector> |
| using namespace std; |
| |
| int main() { |
| vector<int> myVector = {1, 2, 3, 4, 5}; |
| |
| |
| myVector.push_back(6); |
| |
| |
| cout << "Element at index 0: " << myVector[0] << endl; |
| |
| |
| myVector[0] = 10; |
| |
| |
| myVector.pop_back(); |
| |
| |
| cout << "Vector size: " << myVector.size() << endl; |
| |
| return 0; |
| } |
遍历向量
| #include <iostream> |
| #include <vector> |
| using namespace std; |
| |
| int main() { |
| vector<int> myVector = {1, 2, 3, 4, 5}; |
| |
| |
| for (int i : myVector) { |
| cout << i << " "; |
| } |
| cout << endl; |
| |
| |
| for (vector<int>::iterator it = myVector.begin(); it != myVector.end(); ++it) { |
| cout << *it << " "; |
| } |
| cout << endl; |
| |
| return 0; |
| } |
指针
指针是 C++ 中的一个重要概念,它是一个变量,用于存储另一个变量的内存地址。
指针的声明和使用
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| int myNum = 10; |
| int* ptr = &myNum; |
| |
| |
| cout << "Value of myNum: " << myNum << endl; |
| |
| |
| cout << "Address stored in ptr: " << ptr << endl; |
| |
| |
| cout << "Value pointed to by ptr: " << *ptr << endl; |
| |
| return 0; |
| } |
指针和数组
指针可以用于遍历数组。
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| int myArray[5] = {10, 20, 30, 40, 50}; |
| int* ptr = myArray; |
| |
| for (int i = 0; i < 5; i++) { |
| cout << "Element " << i << ": " << *(ptr + i) << endl; |
| } |
| |
| return 0; |
| } |
指针和函数
指针可以作为函数参数,用于传递数组或修改变量的值。
| #include <iostream> |
| using namespace std; |
| |
| void increment(int* ptr) { |
| (*ptr)++; |
| } |
| |
| int main() { |
| int myNum = 10; |
| increment(&myNum); |
| cout << "Value of myNum after increment: " << myNum << endl; |
| |
| return 0; |
| } |
动态内存分配
C++ 提供了 new
和 delete
操作符,用于动态分配和释放内存。
动态分配单个变量
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| int* ptr = new int; |
| *ptr = 10; |
| cout << "Value: " << *ptr << endl; |
| delete ptr; |
| |
| return 0; |
| } |
动态分配数组
| #include <iostream> |
| using namespace std; |
| |
| int main() { |
| int* ptr = new int[5]; |
| for (int i = 0; i < 5; i++) { |
| ptr[i] = i * 10; |
| } |
| |
| for (int i = 0; i < 5; i++) { |
| cout << "Element " << i << ": " << ptr[i] << endl; |
| } |
| |
| delete[] ptr; |
| |
| return 0; |
| } |
动态内存分配允许在运行时根据需要分配和释放内存,提高了程序的灵活性。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】