C++11 引入了许多重要的新特性,以下是一些关键特性及其对应的示例代码,用于体现这些特性的用法和优势。
1. 自动类型推导 (auto
)
auto
关键字允许编译器自动推导变量的类型,简化代码书写。
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// 使用 auto 自动推导迭代器类型
for (auto it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it << " ";
}
// 使用 auto 推导元素类型
for (auto& value : vec) {
std::cout << value << " ";
}
return 0;
}
2. 范围基于的 for
循环
范围 for
循环简化了对容器或数组的遍历。
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// 使用范围 for 循环遍历容器
for (const auto& value : vec) {
std::cout << value << " ";
}
return 0;
}
3. Lambda 表达式
Lambda 表达式允许在代码中定义匿名函数,常用于简化回调函数或算法中的函数对象。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// 使用 Lambda 表达式作为谓词
std::sort(vec.begin(), vec.end(), [](int a, int b) {
return a > b; // 降序排序
});
for (const auto& value : vec) {
std::cout << value << " ";
}
return 0;
}
4. 右值引用和移动语义 (std::move
)
右值引用和移动语义允许高效地转移资源,避免不必要的拷贝。
#include <iostream>
#include <string>
class MyString {
public:
MyString(const char* str) {
std::cout << "Constructor called!" << std::endl;
size = strlen(str);
data = new char[size + 1];
memcpy(data, str, size + 1);
}
// 移动构造函数
MyString(MyString&& other) noexcept {
std::cout << "Move constructor called!" << std::endl;
data = other.data;
size = other.size;
other.data = nullptr;
other.size = 0;
}
~MyString() {
delete[] data;
}
void print() const {
std::cout << data << std::endl;
}
private:
char* data;
size_t size;
};
int main() {
MyString s1("Hello");
MyString s2 = std::move(s1); // 调用移动构造函数
s2.print(); // 输出: Hello
// s1.print(); // 错误:s1 的资源已被转移
return 0;
}
5. 智能指针 (std::unique_ptr
, std::shared_ptr
)
智能指针用于自动管理动态内存,避免内存泄漏。
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() {
std::cout << "MyClass constructed!" << std::endl;
}
~MyClass() {
std::cout << "MyClass destroyed!" << std::endl;
}
void doSomething() {
std::cout << "Doing something!" << std::endl;
}
};
int main() {
// 使用 std::unique_ptr
std::unique_ptr<MyClass> ptr1(new MyClass());
ptr1->doSomething();
// 使用 std::shared_ptr
std::shared_ptr<MyClass> ptr2 = std::make_shared<MyClass>();
ptr2->doSomething();
return 0;
}
6. nullptr
关键字
nullptr
用于替代 NULL
,表示空指针,避免类型歧义。
#include <iostream>
void foo(int* ptr) {
if (ptr) {
std::cout << "Pointer is not null!" << std::endl;
} else {
std::cout << "Pointer is null!" << std::endl;
}
}
int main() {
int* ptr = nullptr;
foo(ptr); // 输出: Pointer is null!
int x = 10;
ptr = &x;
foo(ptr); // 输出: Pointer is not null!
return 0;
}
7. std::thread
多线程支持
C++11 引入了标准库对多线程的支持。
#include <iostream>
#include <thread>
void threadFunction() {
std::cout << "Hello from thread!" << std::endl;
}
int main() {
std::thread t(threadFunction);
t.join(); // 等待线程结束
std::cout << "Main thread finished!" << std::endl;
return 0;
}
8. std::function
和 std::bind
std::function
用于存储可调用对象,std::bind
用于绑定参数。
#include <iostream>
#include <functional>
void printSum(int a, int b) {
std::cout << "Sum: " << a + b << std::endl;
}
int main() {
// 使用 std::function 存储可调用对象
std::function<void(int, int)> func = printSum;
func(2, 3); // 输出: Sum: 5
// 使用 std::bind 绑定参数
auto boundFunc = std::bind(printSum, 10, std::placeholders::_1);
boundFunc(5); // 输出: Sum: 15
return 0;
}
9. constexpr
常量表达式
constexpr
用于在编译时计算常量表达式。
#include <iostream>
constexpr int factorial(int n) {
return (n <= 1) ? 1 : n * factorial(n - 1);
}
int main() {
constexpr int result = factorial(5); // 编译时计算
std::cout << "Factorial of 5: " << result << std::endl; // 输出: 120
return 0;
}
10. std::array
和 std::tuple
std::array
是固定大小的数组容器,std::tuple
用于存储多个不同类型的值。
#include <iostream>
#include <array>
#include <tuple>
int main() {
// 使用 std::array
std::array<int, 3> arr = {1, 2, 3};
for (const auto& value : arr) {
std::cout << value << " ";
}
std::cout << std::endl;
// 使用 std::tuple
std::tuple<int, double, std::string> tup(10, 3.14, "Hello");
std::cout << std::get<0>(tup) << " " << std::get<1>(tup) << " " << std::get<2>(tup) << std::endl;
return 0;
}
总结
以上示例展示了 C++11 中一些重要的新特性,包括自动类型推导、范围 for
循环、Lambda 表达式、移动语义、智能指针、nullptr
、多线程支持、std::function
和 std::bind
、constexpr
以及 std::array
和 std::tuple
。这些特性极大地提高了 C++ 的现代化编程能力。