万网没备案怎么做网站seo高级优化技巧
一、基础知识
C++基础知识点的汇总涵盖了许多方面,包括但不限于以下内容:
1. 常量定义
-
使用
#define
宏定义常量:#define PI 3.14159265358979323846 // 宏定义一个浮点型常量PI
-
使用
const
关键字定义常量:const int MAX_SIZE = 100; // 常量MAX_SIZE不可修改 const double PI = 3.14159265358979323846;
2. 关键字
- C++中有特定的关键字,如
int
、double
、void
、if
、else
、for
、while
、switch
、case
、default
、class
、struct
、namespace
、const
、static
等,它们在程序中有特殊的含义和用途。
3. 标识符命名规则
-
标识符是用来给变量、函数、类等命名的。
-
规则包括:
- 必须以字母或下划线(_)开头。
- 后续字符可以是字母、数字或下划线。
- 关键字不能作为标识符使用。
- C++中标识符区分大小写。
4. 变量声明与定义
-
变量可以在需要时定义,并且可以指定初始值。
int age = 25; // 定义并初始化一个整型变量age
5. 数据类型
- C++支持多种数据类型,包括基本类型(如int, char, float, double)、枚举类型、指针类型以及用户自定义类型(通过结构体、联合体和类实现)。
6. 控制结构
-
条件语句:
if (age >= 18) {cout << "Adult"; } else {cout << "Minor"; }
-
循环语句:
for (int i = 0; i < 10; ++i) {cout << i << " "; } while (condition) {// 循环体 }
7. 函数
-
函数定义和调用:
// 函数定义 int add(int a, int b) {return a + b; }// 函数调用 int result = add(3, 5);
8. 数组和字符串
-
数组声明和使用:
int numbers[5] = {1, 2, 3, 4, 5};
-
字符串通常通过
std::string
来处理:std::string name = "John Doe";
9. 指针与引用
-
指针示例:
int value = 10; int* ptr = &value; // 指针ptr指向变量value *ptr = 20; // 通过指针修改值
-
引用示例:
int num = 15; int& ref = num; // 引用ref绑定到num ref = 30; // 通过引用修改num的值
10. 命名空间
-
命名空间用于避免标识符冲突:
namespace MyNamespace {int func() {return 42;} } using namespace MyNamespace; // 引入整个命名空间 // 或者 MyNamespace::func(); // 显式指定命名空间
11. 面向对象编程
-
类定义:
class Animal { public:Animal(const std::string& name) : name_(name) {}void speak() const {cout << "Animal speaks!" << endl;} private:std::string name_; };int main() {Animal dog("Rex");dog.speak();return 0; }
二、 进阶
当然,以下是这些C++高级概念的详细说明以及代码示例:
1. 模板(Templates)
说明:模板允许你定义泛型类或函数,即适用于多种数据类型的类和函数。它们在编译时进行实例化,生成具体的数据类型版本。
代码示例:
// 函数模板
template <typename T>
T max(T a, T b) {return (a > b) ? a : b;
}int main() {cout << max<int>(5, 10) << endl; // 直接指定类型为intcout << max(3.14, 2.71) << endl; // 编译器自动推断为double类型
}// 类模板
template <typename T>
class Stack {
public:void push(T item) { ... }T pop() { ... }
private:std::vector<T> data;
};int main() {Stack<int> intStack;intStack.push(10);Stack<std::string> strStack;strStack.push("Hello");
}
2. 异常处理
说明:C++提供了一种结构化的错误处理机制,通过try
、catch
和throw
关键字来捕获和抛出异常。
代码示例:
#include <stdexcept>void divide(int a, int b) {if (b == 0) {throw std::invalid_argument("Divisor cannot be zero!"); // 抛出异常}int result = a / b;// ...
}int main() {try {divide(10, 0); // 可能引发异常的操作} catch (const std::invalid_argument& e) {std::cerr << "Caught exception: " << e.what() << '\n';}return 0;
}
3. STL容器
说明:标准模板库(STL)提供了多种容器,如std::vector
、std::list
、std::map
等,用于存储和管理数据。
代码示例:
#include <vector>
#include <list>
#include <map>int main() {// 容器使用示例std::vector<int> vec{1, 2, 3, 4, 5}; // 动态数组std::list<int> lst{6, 7, 8, 9, 10}; // 双向链表std::map<std::string, int> m{{"apple", 1}, {"banana", 2}}; // 关联容器(键值对)vec.push_back(6);lst.push_back(11);for (const auto& item : vec) {std::cout << item << ' ';}std::cout << '\n';return 0;
}// 使用迭代器访问容器元素
for (auto it = vec.begin(); it != vec.end(); ++it) {std::cout << *it << ' '; // 输出vector中的每个元素
}
4. 迭代器(Iterators)
说明:迭代器是访问容器内元素的一种通用接口,就像指针一样可以遍历容器内的元素。
代码示例:
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {std::cout << *it << ' ';
} // 输出1 2 3 4 5// C++11之后推荐使用范围基础迭代器语法
for (auto it : numbers) {std::cout << it << ' ';
}
5. 智能指针(Smart Pointers)
说明:智能指针是一种对象,它存储指向动态分配的对象的指针,并在其生命周期结束时自动释放该对象,避免内存泄漏。常见的智能指针有std::unique_ptr
、std::shared_ptr
和std::weak_ptr
。
代码示例:
#include <memory>class MyClass {
public:MyClass() { /* 构造函数 */ }~MyClass() { /* 析构函数 */ }
};int main() {// unique_ptr 示例std::unique_ptr<MyClass> uptr(new MyClass());// 当uptr超出作用域时,其内部的MyClass对象会被自动删除// shared_ptr 示例std::shared_ptr<MyClass> sptr(new MyClass());// 多个shared_ptr可以共享同一个对象,所有shared_ptr销毁后才释放对象return 0;
}
6. RAII(Resource Acquisition Is Initialization)原则
说明:RAII是一种编程技术,资源获取时机与初始化同步,确保了资源的生命周期与对应的对象绑定在一起,使得资源在对象析构时自动释放。例如,智能指针就是RAII的一个典型应用。
7. 多态性(Polymorphism)
说明:多态性允许不同类的对象响应相同的调用请求,实现不同的行为。主要通过虚函数(virtual functions)和抽象基类(abstract base classes)实现。
代码示例:
class Animal {
public:virtual void speak() const = 0; // 纯虚函数,声明一个抽象基类
};class Dog : public Animal {
public:void speak() const override {std::cout << "Woof!" << std::endl;}
};class Cat : public Animal {
public:void speak() const override {std::cout << "Meow!" << std::endl;}
};int main() {Animal* dog = new Dog();Animal* cat = new Cat();dog->speak(); // 输出"Woof!"cat->speak(); // 输出"Meow!"delete dog;delete cat;return 0;
}