c++系列

C++ 基本语法

变量和类型

int a = 10;
double b = 20.5;
char c = 'A';

条件语句

if (a > b) {
// 代码块
} else {
// 代码块
}

循环语句

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++ 中有三种基本的作用域类型:

  1. 局部变量
  2. 全局变量
  3. 静态变量

局部变量

局部变量是在函数或代码块内部声明的变量,只能在该函数或代码块内部访问。

#include <iostream>
using namespace std;
void myFunction() {
int myNum = 10; // 局部变量
cout << "myNum inside function: " << myNum << endl;
}
int main() {
myFunction();
// cout << myNum; // 错误: myNum 在此处不可访问
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++ 中有几种不同类型的字面量:

  1. 整数字面量
  2. 浮点数字面量
  3. 字符字面量
  4. 字符串字面量
  5. 布尔字面量

整数字面量

整数字面量可以是十进制、八进制或十六进制。

#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;
}

布尔字面量

布尔字面量只有两个值:truefalse

#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++ 提供了几种修饰符类型,用于修改基本数据类型的属性。常见的修饰符包括:

  1. signed - 有符号类型
  2. unsigned - 无符号类型
  3. short - 短类型
  4. 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 循环遍历向量
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; // 指针变量,存储 myNum 的地址
// 输出 myNum 的值
cout << "Value of myNum: " << myNum << endl;
// 输出指针 ptr 的值(即 myNum 的地址)
cout << "Address stored in ptr: " << ptr << endl;
// 通过指针访问 myNum 的值
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); // 传递 myNum 的地址
cout << "Value of myNum after increment: " << myNum << endl;
return 0;
}

动态内存分配

C++ 提供了 newdelete 操作符,用于动态分配和释放内存。

动态分配单个变量

#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;
}

动态内存分配允许在运行时根据需要分配和释放内存,提高了程序的灵活性。

posted @   学^以致用  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示