资料库 – 中文知识库
—
by
引言 C++ STL(Standard Template Library)是一套强大的模板类库,它提供了多种数据结构和算法,极大地提高了C++编程的效率和可读性。本文将为您提供50个实用的C++ STL示例,帮助您快速入门并提升编程技能。 1. vector的使用 向量是STL中最常用的容器之一,适用于存储连续的数据序列。 #include #include
int main() { std::vector vec = {1, 2, 3, 4, 5}; std::cout << "First element: " << vec[0] << std::endl; return 0; } 2. list的使用 链表是一种非线性数据结构,适用于插入和删除频繁的场景。 #include #include int main() { std::list lst = {1, 2, 3, 4, 5}; lst.push_back(6); std::cout << "List elements: "; for (auto it = lst.begin(); it != lst.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; } 3. deque的使用 双端队列是一种允许在两端进行插入和删除的容器。 #include #include int main() { std::deque dq = {1, 2, 3, 4, 5}; dq.push_front(0); dq.pop_back(); std::cout << "Deque elements: "; for (auto it = dq.begin(); it != dq.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; } 4. stack的使用 栈是一种后进先出(LIFO)的数据结构。 #include #include int main() { std::stack stk; stk.push(1); stk.push(2); stk.push(3); std::cout << "Top element: " << stk.top() << std::endl; stk.pop(); std::cout << "Top element after pop: " << stk.top() << std::endl; return 0; } 5. queue的使用 队列是一种先进先出(FIFO)的数据结构。 #include #include int main() { std::queue q; q.push(1); q.push(2); q.push(3); std::cout << "Front element: " << q.front() << std::endl; q.pop(); std::cout << "Front element after pop: " << q.front() << std::endl; return 0; } 6. priority_queue的使用 优先队列是一种基于堆的数据结构,用于存储具有优先级的数据。 #include #include #include int main() { std::priority_queue pq; std::vector v = {4, 2, 6, 1, 3}; for (int i : v) { pq.push(i); } std::cout << "Elements in priority queue: "; while (!pq.empty()) { std::cout << pq.top() << " "; pq.pop(); } std::cout << std::endl; return 0; } 7. map的使用 映射是一种关联容器,用于存储键值对。 #include #include int main() { std::map m; m[1] = “One”; m[2] = “Two”; m[3] = “Three”; std::cout << "Value at key 2: " << m[2] << std::endl; return 0; } 8. set的使用 集合是一种不包含重复元素的关联容器。 #include #include int main() { std::set s = {1, 2, 2, 3, 4, 4, 5}; std::cout << "Set elements: "; for (int i : s) { std::cout << i << " "; } std::cout << std::endl; return 0; } 9. bitset的使用 位集合是一种用于存储位信息的容器。 #include #include int main() { std::bitset<8> bs = 0b10101010; std::cout << "Bitset value: " << bs << std::endl; return 0; } 10. auto_ptr的使用 自动指针是一种智能指针,用于自动管理内存。 #include #include int main() { std::auto_ptr ap(new int(10)); std::cout << "Value of ap: " << *ap << std::endl; return 0; } 11. unique_ptr的使用 独占指针是一种更安全的智能指针,用于自动管理内存。 #include #include int main() { std::unique_ptr up(new int(10)); std::cout << "Value of up: " << *up << std::endl; return 0; } 12. shared_ptr的使用 共享指针是一种允许多个指针共享同一块内存的智能指针。 #include #include int main() { std::shared_ptr sp1(new int(10)); std::shared_ptr sp2 = sp1; std::cout << "Value of sp1: " << *sp1 << ", ref count: " << sp1.use_count() << std::endl; std::cout << "Value of sp2: " << *sp2 << ", ref count: " << sp2.use_count() << std::endl; return 0; } 13. weak_ptr的使用 弱指针是一种不会增加引用计数的智能指针,用于防止内存泄漏。 #include #include int main() { std::shared_ptr sp(new int(10)); std::weak_ptr wp = sp; std::cout << "Value of wp: " << *wp << ", ref count: " << wp.use_count() << std::endl; return 0; } 14. unordered_map的使用 无序映射是一种基于哈希表的关联容器。 #include #include int main() { std::unordered_map um; um[1] = “One”; um[2] = “Two”; um[3] = “Three”; std::cout << "Value at key 2: " << um[2] << std::endl; return 0; } 15. unordered_set的使用 无序集合是一种基于哈希表的集合容器。 #include #include int main() { std::unordered_set us = {1, 2, 2, 3, 4, 4, 5}; std::cout << "Set elements: "; for (int i : us) { std::cout << i << " "; } std::cout << std::endl; return 0; } 16. pair的使用 对是一种包含两个元素的值对象。 #include #include int main() { std::pair p(1, “One”); std::cout << "Value of p: " << p.first << ", " << p.second << std::endl; return 0; } 17. tuple的使用 元组是一种包含多个元素的可变长容器。 #include #include int main() { std::tuple t(1, 3.14, “One”); std::cout << "Value of t: " << std::get<0>(t) << ", " << std::get<1>(t) << ", " << std::get<2>(t) << std::endl; return 0; } 18. iterator的使用 迭代器是一种用于遍历容器的对象。 #include #include #include int main() { std::vector v = {1, 2, 3, 4, 5}; std::cout << "Vector elements: "; std::copy(v.begin(), v.end(), std::ostream_iterator(std::cout, ” “)); std::cout << std::endl; return 0; } 19. algorithm的使用 算法是一系列可复用的代码块,用于在容器上进行操作。 #include #include #include int main() { std::vector v = {5, 2, 9, 1, 5, 6}; std::sort(v.begin(), v.end()); std::cout << "Sorted vector elements: "; for (int i : v) { std::cout << i << " "; } std::cout << std::endl; return 0; } 20. copy构造函数的使用 复制构造函数是一种用于复制对象的构造函数。 #include class MyClass { public: MyClass() { std::cout << "MyClass constructor called." << std::endl; } MyClass(const MyClass& other) { std::cout << "MyClass copy constructor called." << std::endl; } }; int main() { MyClass obj; MyClass obj_copy(obj); return 0; } 21. move构造函数的使用 移动构造函数是一种用于移动资源的构造函数。 #include #include class MyClass { public: MyClass() { std::cout << "MyClass constructor called." << std::endl; } MyClass(MyClass&& other) { std::cout << "MyClass move constructor called." << std::endl; } }; int main() { std::unique_ptr up1(new MyClass()); std::unique_ptr up2(std::move(up1)); return 0; } 22. custom allocator的使用 自定义分配器是一种用于管理内存分配的机制。 #include #include #include template class CustomAllocator { public: using value_type = T; using pointer = T*; using size_type = size_t; using difference_type = ptrdiff_t; pointer allocate(size_type n) { // Custom allocation logic here return nullptr; } void deallocate(pointer p, size_type n) { // Custom deallocation logic here } }; int main() { std::vector> v; v.push_back(1); v.push_back(2); v.push_back(3); std::cout << "Vector elements: "; for (int i : v) { std::cout << i << " "; } std::cout << std::endl; return 0; } 23. lambda表达式 lambda表达式是一种用于创建匿名函数的语法。 #include #include #include int main() { std::vector v = {1, 2, 3, 4, 5}; std::sort(v.begin(), v.end(), [](int a, int b) { return a < b; }); std::cout << "Sorted vector elements: "; for (int i : v) { std::cout << i << " "; } std::cout << std::endl; return 0; } 24. function的使用 function是一种用于存储和调用可调用对象的类型。 #include #include int main() { auto add = [](int a, int b) { return a + b; }; std::cout << "Sum: " << add(3, 4) << std::endl; return 0; } 25. bind的使用 bind是一种用于绑定函数参数的函数。 #include #include int main() { auto add = [](int a, int b) { return a + b; }; std::cout << "Sum: " << std::bind(add, 3, 4)() << std::endl; return 0; } 26. std::thread的使用 线程是一种用于并发执行的进程。 #include #include void print_numbers() { for (int i = 0; i < 5; ++i) { std::cout << "Number " << i << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t(print_numbers); t.join(); return 0; } 27. std::mutex的使用 互斥锁是一种用于同步访问共享资源的机制。 #include #include #include std::mutex mtx; void print_numbers() { for (int i = 0; i < 5; ++i) { mtx.lock(); std::cout << "Number " << i << std::endl; mtx.unlock(); std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t1(print_numbers); std::thread t2(print_numbers); t1.join(); t2.join(); return 0; } 28. std::atomic的使用 原子操作是一种用于确保操作在多线程环境下安全执行的机制。 #include #include #include std::atomic count(0); void increment() { for (int i = 0; i < 1000; ++i) { count.fetch_add(1, std::memory_order_relaxed); } } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Final count: " << count << std::endl; return 0; } 29. std::condition_variable的使用 条件变量是一种用于线程间同步的机制。 #include #include #include std::condition_variable cv; std::mutex mtx; void producer() { std::unique_lock lck(mtx); cv.wait(lck, [] { return ready; }); // Produce data } void consumer() { std::unique_lock lck(mtx); // Consume data cv.notify_one(); } int main() { std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); return 0; } 30. std::future的使用 future是一种用于异步执行可调用对象的机制。 #include #include #include int add(int a, int b) { std::this_thread::sleep_for(std::chrono::seconds(1)); return a + b; } int main() { auto fut = std::async(std::launch::async, add, 2, 3); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 31. std::promise的使用 promise是一种用于存储future对象的机制。 #include #include #include int add(int a, int b) { std::this_thread::sleep_for(std::chrono::seconds(1)); return a + b; } int main() { std::promise prom; auto fut = prom.get_future(); std::thread t(add, 2, 3); prom.set_value(5); t.join(); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 32. std::async的使用 async是一种用于异步执行可调用对象的函数。 #include #include #include int add(int a, int b) { std::this_thread::sleep_for(std::chrono::seconds(1)); return a + b; } int main() { auto fut = std::async(std::launch::async, add, 2, 3); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 33. std::thread的使用 线程是一种用于并发执行的进程。 #include #include #include void print_numbers() { for (int i = 0; i < 5; ++i) { std::cout << "Number " << i << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t(print_numbers); t.join(); return 0; } 34. std::atomic的使用 原子操作是一种用于确保操作在多线程环境下安全执行的机制。 #include #include #include std::atomic count(0); void increment() { for (int i = 0; i < 1000; ++i) { count.fetch_add(1, std::memory_order_relaxed); } } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Final count: " << count << std::endl; return 0; } 35. std::mutex的使用 互斥锁是一种用于同步访问共享资源的机制。 ”`cpp #include #include #include std::mutex mtx; void print_numbers() { for (int i = 0; i < 5; ++i) { mtx.lock(); std::cout << "Number " << i << std::endl; mtx.unlock(); std
int main() { std::list lst = {1, 2, 3, 4, 5}; lst.push_back(6); std::cout << "List elements: "; for (auto it = lst.begin(); it != lst.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; } 3. deque的使用 双端队列是一种允许在两端进行插入和删除的容器。 #include #include
int main() { std::deque dq = {1, 2, 3, 4, 5}; dq.push_front(0); dq.pop_back(); std::cout << "Deque elements: "; for (auto it = dq.begin(); it != dq.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; } 4. stack的使用 栈是一种后进先出(LIFO)的数据结构。 #include #include
int main() { std::stack stk; stk.push(1); stk.push(2); stk.push(3); std::cout << "Top element: " << stk.top() << std::endl; stk.pop(); std::cout << "Top element after pop: " << stk.top() << std::endl; return 0; } 5. queue的使用 队列是一种先进先出(FIFO)的数据结构。 #include #include
int main() { std::queue q; q.push(1); q.push(2); q.push(3); std::cout << "Front element: " << q.front() << std::endl; q.pop(); std::cout << "Front element after pop: " << q.front() << std::endl; return 0; } 6. priority_queue的使用 优先队列是一种基于堆的数据结构,用于存储具有优先级的数据。 #include #include #include
int main() { std::priority_queue pq; std::vector v = {4, 2, 6, 1, 3}; for (int i : v) { pq.push(i); } std::cout << "Elements in priority queue: "; while (!pq.empty()) { std::cout << pq.top() << " "; pq.pop(); } std::cout << std::endl; return 0; } 7. map的使用 映射是一种关联容器,用于存储键值对。 #include #include
int main() { std::map m; m[1] = “One”; m[2] = “Two”; m[3] = “Three”; std::cout << "Value at key 2: " << m[2] << std::endl; return 0; } 8. set的使用 集合是一种不包含重复元素的关联容器。 #include #include
int main() { std::set s = {1, 2, 2, 3, 4, 4, 5}; std::cout << "Set elements: "; for (int i : s) { std::cout << i << " "; } std::cout << std::endl; return 0; } 9. bitset的使用 位集合是一种用于存储位信息的容器。 #include #include
int main() { std::bitset<8> bs = 0b10101010; std::cout << "Bitset value: " << bs << std::endl; return 0; } 10. auto_ptr的使用 自动指针是一种智能指针,用于自动管理内存。 #include #include
int main() { std::auto_ptr ap(new int(10)); std::cout << "Value of ap: " << *ap << std::endl; return 0; } 11. unique_ptr的使用 独占指针是一种更安全的智能指针,用于自动管理内存。 #include #include
int main() { std::unique_ptr up(new int(10)); std::cout << "Value of up: " << *up << std::endl; return 0; } 12. shared_ptr的使用 共享指针是一种允许多个指针共享同一块内存的智能指针。 #include #include
int main() { std::shared_ptr sp1(new int(10)); std::shared_ptr sp2 = sp1; std::cout << "Value of sp1: " << *sp1 << ", ref count: " << sp1.use_count() << std::endl; std::cout << "Value of sp2: " << *sp2 << ", ref count: " << sp2.use_count() << std::endl; return 0; } 13. weak_ptr的使用 弱指针是一种不会增加引用计数的智能指针,用于防止内存泄漏。 #include #include
int main() { std::shared_ptr sp(new int(10)); std::weak_ptr wp = sp; std::cout << "Value of wp: " << *wp << ", ref count: " << wp.use_count() << std::endl; return 0; } 14. unordered_map的使用 无序映射是一种基于哈希表的关联容器。 #include #include
int main() { std::unordered_map um; um[1] = “One”; um[2] = “Two”; um[3] = “Three”; std::cout << "Value at key 2: " << um[2] << std::endl; return 0; } 15. unordered_set的使用 无序集合是一种基于哈希表的集合容器。 #include #include
int main() { std::unordered_set us = {1, 2, 2, 3, 4, 4, 5}; std::cout << "Set elements: "; for (int i : us) { std::cout << i << " "; } std::cout << std::endl; return 0; } 16. pair的使用 对是一种包含两个元素的值对象。 #include #include
int main() { std::pair p(1, “One”); std::cout << "Value of p: " << p.first << ", " << p.second << std::endl; return 0; } 17. tuple的使用 元组是一种包含多个元素的可变长容器。 #include #include
int main() { std::tuple t(1, 3.14, “One”); std::cout << "Value of t: " << std::get<0>(t) << ", " << std::get<1>(t) << ", " << std::get<2>(t) << std::endl; return 0; } 18. iterator的使用 迭代器是一种用于遍历容器的对象。 #include #include #include
int main() { std::vector v = {1, 2, 3, 4, 5}; std::cout << "Vector elements: "; std::copy(v.begin(), v.end(), std::ostream_iterator(std::cout, ” “)); std::cout << std::endl; return 0; } 19. algorithm的使用 算法是一系列可复用的代码块,用于在容器上进行操作。 #include #include #include
int main() { std::vector v = {5, 2, 9, 1, 5, 6}; std::sort(v.begin(), v.end()); std::cout << "Sorted vector elements: "; for (int i : v) { std::cout << i << " "; } std::cout << std::endl; return 0; } 20. copy构造函数的使用 复制构造函数是一种用于复制对象的构造函数。 #include
class MyClass { public: MyClass() { std::cout << "MyClass constructor called." << std::endl; } MyClass(const MyClass& other) { std::cout << "MyClass copy constructor called." << std::endl; } }; int main() { MyClass obj; MyClass obj_copy(obj); return 0; } 21. move构造函数的使用 移动构造函数是一种用于移动资源的构造函数。 #include #include
class MyClass { public: MyClass() { std::cout << "MyClass constructor called." << std::endl; } MyClass(MyClass&& other) { std::cout << "MyClass move constructor called." << std::endl; } }; int main() { std::unique_ptr up1(new MyClass()); std::unique_ptr up2(std::move(up1)); return 0; } 22. custom allocator的使用 自定义分配器是一种用于管理内存分配的机制。 #include #include #include
template class CustomAllocator { public: using value_type = T; using pointer = T*; using size_type = size_t; using difference_type = ptrdiff_t;
pointer allocate(size_type n) { // Custom allocation logic here return nullptr; }
void deallocate(pointer p, size_type n) { // Custom deallocation logic here } };
int main() { std::vector> v; v.push_back(1); v.push_back(2); v.push_back(3); std::cout << "Vector elements: "; for (int i : v) { std::cout << i << " "; } std::cout << std::endl; return 0; } 23. lambda表达式 lambda表达式是一种用于创建匿名函数的语法。 #include #include #include
int main() { std::vector v = {1, 2, 3, 4, 5}; std::sort(v.begin(), v.end(), [](int a, int b) { return a < b; }); std::cout << "Sorted vector elements: "; for (int i : v) { std::cout << i << " "; } std::cout << std::endl; return 0; } 24. function的使用 function是一种用于存储和调用可调用对象的类型。 #include #include
int main() { auto add = [](int a, int b) { return a + b; }; std::cout << "Sum: " << add(3, 4) << std::endl; return 0; } 25. bind的使用 bind是一种用于绑定函数参数的函数。 #include #include
int main() { auto add = [](int a, int b) { return a + b; }; std::cout << "Sum: " << std::bind(add, 3, 4)() << std::endl; return 0; } 26. std::thread的使用 线程是一种用于并发执行的进程。 #include #include void print_numbers() { for (int i = 0; i < 5; ++i) { std::cout << "Number " << i << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t(print_numbers); t.join(); return 0; } 27. std::mutex的使用 互斥锁是一种用于同步访问共享资源的机制。 #include #include #include std::mutex mtx; void print_numbers() { for (int i = 0; i < 5; ++i) { mtx.lock(); std::cout << "Number " << i << std::endl; mtx.unlock(); std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t1(print_numbers); std::thread t2(print_numbers); t1.join(); t2.join(); return 0; } 28. std::atomic的使用 原子操作是一种用于确保操作在多线程环境下安全执行的机制。 #include #include #include std::atomic count(0); void increment() { for (int i = 0; i < 1000; ++i) { count.fetch_add(1, std::memory_order_relaxed); } } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Final count: " << count << std::endl; return 0; } 29. std::condition_variable的使用 条件变量是一种用于线程间同步的机制。 #include #include #include std::condition_variable cv; std::mutex mtx; void producer() { std::unique_lock lck(mtx); cv.wait(lck, [] { return ready; }); // Produce data } void consumer() { std::unique_lock lck(mtx); // Consume data cv.notify_one(); } int main() { std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); return 0; } 30. std::future的使用 future是一种用于异步执行可调用对象的机制。 #include #include #include int add(int a, int b) { std::this_thread::sleep_for(std::chrono::seconds(1)); return a + b; } int main() { auto fut = std::async(std::launch::async, add, 2, 3); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 31. std::promise的使用 promise是一种用于存储future对象的机制。 #include #include #include int add(int a, int b) { std::this_thread::sleep_for(std::chrono::seconds(1)); return a + b; } int main() { std::promise prom; auto fut = prom.get_future(); std::thread t(add, 2, 3); prom.set_value(5); t.join(); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 32. std::async的使用 async是一种用于异步执行可调用对象的函数。 #include #include #include int add(int a, int b) { std::this_thread::sleep_for(std::chrono::seconds(1)); return a + b; } int main() { auto fut = std::async(std::launch::async, add, 2, 3); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 33. std::thread的使用 线程是一种用于并发执行的进程。 #include #include #include void print_numbers() { for (int i = 0; i < 5; ++i) { std::cout << "Number " << i << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t(print_numbers); t.join(); return 0; } 34. std::atomic的使用 原子操作是一种用于确保操作在多线程环境下安全执行的机制。 #include #include #include std::atomic count(0); void increment() { for (int i = 0; i < 1000; ++i) { count.fetch_add(1, std::memory_order_relaxed); } } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Final count: " << count << std::endl; return 0; } 35. std::mutex的使用 互斥锁是一种用于同步访问共享资源的机制。 ”`cpp #include #include #include std::mutex mtx; void print_numbers() { for (int i = 0; i < 5; ++i) { mtx.lock(); std::cout << "Number " << i << std::endl; mtx.unlock(); std
void print_numbers() { for (int i = 0; i < 5; ++i) { std::cout << "Number " << i << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t(print_numbers); t.join(); return 0; } 27. std::mutex的使用 互斥锁是一种用于同步访问共享资源的机制。 #include #include #include
std::mutex mtx;
void print_numbers() { for (int i = 0; i < 5; ++i) { mtx.lock(); std::cout << "Number " << i << std::endl; mtx.unlock(); std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t1(print_numbers); std::thread t2(print_numbers); t1.join(); t2.join(); return 0; } 28. std::atomic的使用 原子操作是一种用于确保操作在多线程环境下安全执行的机制。 #include #include #include
std::atomic count(0);
void increment() { for (int i = 0; i < 1000; ++i) { count.fetch_add(1, std::memory_order_relaxed); } } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Final count: " << count << std::endl; return 0; } 29. std::condition_variable的使用 条件变量是一种用于线程间同步的机制。 #include #include #include
std::condition_variable cv; std::mutex mtx;
void producer() { std::unique_lock lck(mtx); cv.wait(lck, [] { return ready; }); // Produce data }
void consumer() { std::unique_lock lck(mtx); // Consume data cv.notify_one(); }
int main() { std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); return 0; } 30. std::future的使用 future是一种用于异步执行可调用对象的机制。 #include #include #include
int add(int a, int b) { std::this_thread::sleep_for(std::chrono::seconds(1)); return a + b; }
int main() { auto fut = std::async(std::launch::async, add, 2, 3); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 31. std::promise的使用 promise是一种用于存储future对象的机制。 #include #include #include
int main() { std::promise prom; auto fut = prom.get_future(); std::thread t(add, 2, 3);
prom.set_value(5); t.join(); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 32. std::async的使用 async是一种用于异步执行可调用对象的函数。 #include #include #include
int main() { auto fut = std::async(std::launch::async, add, 2, 3); std::cout << "Sum: " << fut.get() << std::endl; return 0; } 33. std::thread的使用 线程是一种用于并发执行的进程。 #include #include #include
void print_numbers() { for (int i = 0; i < 5; ++i) { std::cout << "Number " << i << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t(print_numbers); t.join(); return 0; } 34. std::atomic的使用 原子操作是一种用于确保操作在多线程环境下安全执行的机制。 #include #include #include
void increment() { for (int i = 0; i < 1000; ++i) { count.fetch_add(1, std::memory_order_relaxed); } } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Final count: " << count << std::endl; return 0; } 35. std::mutex的使用 互斥锁是一种用于同步访问共享资源的机制。 ”`cpp #include #include #include std::mutex mtx; void print_numbers() { for (int i = 0; i < 5; ++i) { mtx.lock(); std::cout << "Number " << i << std::endl; mtx.unlock(); std