现代C++
2025/11/7大约 3 分钟
现代C++
什么是现代C++?现代C++是指C++11及其后续版本,这些版本引入了许多新的语言特性和库,使得C++编程变得更加简洁、高效和强大。现代C++的主要特点包括:类型推导、智能指针、lambda表达式、并发编程、正则表达式、文件系统操作、模板元编程等。
先不提C++14、C++17、C++20等版本,单说C++11就已经带来了翻天覆地的变化。下面我们来看看C++11的一些重要特性。
1. 类型推导
C++11引入了auto关键字,可以让编译器根据初始化表达式自动推导变量的类型。这使得代码更加简洁,尤其是在处理复杂类型时。
#include <iostream>
#include <vector>
int main() {
auto x = 10; // x的类型被推导为int
auto y = 3.14; // y的类型被推导为double
auto z = std::vector<int>{1, 2, 3, 4, 5}; // z的类型被推导为std::vector<int>
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
std::cout << "z = ";
for (const auto& num : z) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}2. 智能指针
C++11引入了智能指针,包括std::unique_ptr、std::shared_ptr和std::weak_ptr,用于管理动态分配的内存,避免内存泄漏和悬空指针问题。
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() { std::cout << "MyClass constructor" << std::endl; }
~MyClass() { std::cout << "MyClass destructor" << std::endl; }
};
int main() {
{
std::unique_ptr<MyClass> ptr1(new MyClass()); // 使用unique_ptr管理MyClass对象
} // ptr1超出作用域,MyClass对象被自动销毁
{
std::shared_ptr<MyClass> ptr2(new MyClass()); // 使用shared_ptr管理MyClass对象
{
std::shared_ptr<MyClass> ptr3 = ptr2; // 共享所有权
std::cout << "Reference count: " << ptr2.use_count() << std::endl; // 输出引用计数
} // ptr3超出作用域,引用计数减1
std::cout << "Reference count: " << ptr2.use_count() << std::endl; // 输出引用计数
} // ptr2超出作用域,MyClass对象被自动销毁
return 0;
}3. lambda表达式
C++11引入了lambda表达式,用于定义匿名函数,使得代码更加简洁和灵活。
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5};
// 使用lambda表达式对vector中的元素进行排序
std::sort(nums.begin(), nums.end(), [](int a, int b) {
return a > b;
});
// 使用lambda表达式打印vector中的元素
std::for_each(nums.begin(), nums.end(), [](int num) {
std::cout << num << " ";
});
std::cout << std::endl;
return 0;
}4. 并发编程
C++11引入了多线程编程的支持,包括std::thread、std::mutex、std::condition_variable等,使得并发编程变得更加简单和高效。
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx;
void printHello() {
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
}
int main() {
std::thread t1(printHello);
std::thread t2(printHello);
t1.join();
t2.join();
return 0;
}5. 正则表达式
C++11引入了正则表达式库<regex>,使得在C++中进行字符串匹配和替换变得更加方便。
#include <iostream>
#include <regex>
int main() {
std::string str = "Hello, world!";
std::regex regex("world");
std::smatch match; // 用于存储匹配结果
if (std::regex_search(str, match, regex)) {
std::cout << "Match found: " << match.str() << std::endl;
} else {
std::cout << "No match found" << std::endl;
}
return 0;
}6. 文件系统操作
C++11引入了文件系统库<filesystem>,使得在C++中进行文件和目录操作变得更加简单和方便。
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
fs::path path = "/path/to/directory";
// 检查路径是否存在
if (fs::exists(path)) {
std::cout << "Path exists." << std::endl;
} else {
std::cout << "Path does not exist." << std::endl;
}
return 0;
}7. 模板元编程(比较抽象)
C++11引入了模板元编程的支持,使得在编译时进行计算和逻辑判断成为可能。
#include <iostream>
template <int N>
struct Factorial {
static const int value = N * Factorial<N - 1>::value; // 递归计算阶乘
};
template <>
struct Factorial<0> {
static const int value = 1; // 0的阶乘为1
};
int main() {
std::cout << "Factorial of 5 is " << Factorial<5>::value << std::endl;
return 0;
}以上只是现代C++的一部分重要特性,C++11及其后续版本还引入了许多其他有用的特性,如右值引用、范围for循环、枚举类、静态断言等。现代C++使得C++编程变得更加高效和便捷,是C++程序员必备的知识。
