基本语法
1. 基本结构
1 2 3 4 5 6 7
| #include <iostream> using namespace std;
int main() { cout << "Hello, World!" << endl; return 0; }
|
2. 变量与数据类型
常见数据类型:
- 整型:
int
- 浮点型:
float
, double
- 字符型:
char
- 布尔型:
bool
- 字符串:
string
(需要包含<string>
头文件)
示例:
1 2 3 4 5
| int a = 10; float b = 3.14f; char c = 'A'; bool isTrue = true; string str = "C++";
|
3. 输入输出
标准输入输出:
1 2 3 4 5 6 7 8 9 10
| #include <iostream> using namespace std;
int main() { int x; cout << "输入一个数字:"; cin >> x; cout << "你输入的数字是:" << x << endl; return 0; }
|
4. 运算符
- 算术运算符:
+
, -
, *
, /
, %
- 比较运算符:
==
, !=
, <
, >
, <=
, >=
- 逻辑运算符:
&&
(与), ||
(或), !
(非)
- 赋值运算符:
=
, +=
, -=
, *=
, /=
, %=
- 自增/自减:
++
, --
5. 条件语句
if-else 语句:
1 2 3 4 5 6 7
| if (条件) { } else if (条件) { } else { }
|
示例:
1 2 3 4 5
| if (a > b) { cout << "a 大于 b" << endl; } else { cout << "a 小于或等于 b" << endl; }
|
switch 语句:
1 2 3 4 5 6 7 8 9 10
| switch (变量) { case 值1: break; case 值2: break; default: }
|
6. 循环语句
for 循环:
1 2 3
| for (int i = 0; i < 10; i++) { cout << i << " "; }
|
while 循环:
1 2 3 4 5
| int i = 0; while (i < 10) { cout << i << " "; i++; }
|
do-while 循环:
1 2 3 4 5
| int i = 0; do { cout << i << " "; i++; } while (i < 10);
|
7. 函数
定义和调用:
1 2 3 4 5 6 7 8 9 10 11 12
| #include <iostream> using namespace std;
int add(int x, int y) { return x + y; }
int main() { int result = add(5, 3); cout << "结果是:" << result << endl; return 0; }
|
8. 数组与指针
数组:
1 2
| int arr[5] = {1, 2, 3, 4, 5}; cout << arr[0] << endl;
|
指针:
1 2 3
| int x = 10; int *p = &x; cout << *p << endl;
|
9. 类与对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <iostream> using namespace std;
class Person { public: string name; int age;
void sayHello() { cout << "Hello, my name is " << name << endl; } };
int main() { Person p1; p1.name = "Alice"; p1.age = 25; p1.sayHello(); return 0; }
|
10. 文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <fstream> #include <iostream> using namespace std;
int main() { ofstream outfile("example.txt"); outfile << "Hello, File!" << endl; outfile.close();
ifstream infile("example.txt"); string content; while (getline(infile, content)) { cout << content << endl; } infile.close();
return 0; }
|
高级语法
1. 面向对象编程
C++支持类、继承、多态、封装等面向对象编程的特性。
类与对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Person { private: string name; int age;
public: Person(string n, int a) : name(n), age(a) {}
void introduce() { cout << "Name: " << name << ", Age: " << age << endl; } };
int main() { Person p("Alice", 25); p.introduce(); return 0; }
|
继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Animal { public: void eat() { cout << "I can eat!" << endl; } };
class Dog : public Animal { public: void bark() { cout << "I can bark!" << endl; } };
int main() { Dog d; d.eat(); d.bark(); return 0; }
|
多态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Animal { public: virtual void speak() { cout << "Animal speaks!" << endl; } };
class Dog : public Animal { public: void speak() override { cout << "Dog barks!" << endl; } };
int main() { Animal *a = new Dog(); a->speak(); delete a; return 0; }
|
2. 模板
模板用于实现泛型编程,可以定义通用函数和类。
函数模板
1 2 3 4 5 6 7 8 9 10
| template <typename T> T add(T a, T b) { return a + b; }
int main() { cout << add<int>(3, 5) << endl; cout << add<double>(2.5, 4.5) << endl; return 0; }
|
类模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| template <typename T> class Box { private: T value;
public: Box(T v) : value(v) {} T getValue() { return value; } };
int main() { Box<int> intBox(123); Box<string> strBox("Hello"); cout << intBox.getValue() << endl; cout << strBox.getValue() << endl; return 0; }
|
3. 智能指针
智能指针是C++11中引入的,用于安全管理动态内存。
std::unique_ptr
1 2 3 4 5 6
| #include <memory> int main() { auto ptr = std::make_unique<int>(42); cout << *ptr << endl; return 0; }
|
std::shared_ptr
1 2 3 4 5 6 7
| #include <memory> int main() { auto sp1 = std::make_shared<int>(10); auto sp2 = sp1; cout << *sp1 << ", Count: " << sp1.use_count() << endl; return 0; }
|
4. Lambda 表达式
Lambda 是C++11引入的匿名函数。
基本用法
1 2
| auto add = [](int a, int b) -> int { return a + b; }; cout << add(3, 4) << endl;
|
捕获变量
1 2 3
| int x = 10; auto print = [x]() { cout << "x: " << x << endl; }; print();
|
5. STL(标准模板库)
STL提供了容器、迭代器、算法等强大工具。
常用容器
vector
动态数组
map
键值对存储
set
集合
list
双向链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <vector> #include <map> #include <iostream> using namespace std;
int main() { vector<int> v = {1, 2, 3}; for (int x : v) cout << x << " ";
map<string, int> m; m["Alice"] = 30; m["Bob"] = 25; cout << m["Alice"] << endl;
return 0; }
|
常用算法
STL 提供了一些常用算法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <algorithm> #include <vector> using namespace std;
int main() { vector<int> v = {3, 1, 4, 1, 5}; sort(v.begin(), v.end()); for (int x : v) cout << x << " ";
auto it = find(v.begin(), v.end(), 3); if (it != v.end()) cout << "Found: " << *it << endl;
return 0; }
|
6. 异常处理
C++使用try-catch
机制处理异常。
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <iostream> using namespace std;
int main() { try { int a = 10, b = 0; if (b == 0) throw runtime_error("除数不能为零"); cout << a / b << endl; } catch (const exception &e) { cout << "捕获异常: " << e.what() << endl; } return 0; }
|
7. 并发编程
C++11 引入了多线程支持。
创建线程
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <thread> #include <iostream> using namespace std;
void print() { cout << "Hello from thread!" << endl; }
int main() { thread t(print); t.join(); return 0; }
|
线程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <mutex> #include <thread> #include <iostream> using namespace std;
mutex mtx;
void print(int x) { lock_guard<mutex> lock(mtx); cout << "Value: " << x << endl; }
int main() { thread t1(print, 1); thread t2(print, 2);
t1.join(); t2.join(); return 0; }
|