
Morden C++11/17
c++
爱上解放晚晚
开启大神之路
展开
-
c++ 实用语句
//swap交换函数释放内存:vector<T>().swap(X); vector<int>().swap(myvector); //.clear()不能释放内存原创 2021-10-11 13:40:57 · 85 阅读 · 0 评论 -
lambda的用法
#include <iostream> #include <memory> #include <utility> //值捕获 void lambda_value_capture() { int value = 1; auto copy_value = [value] { return value; }; value = 100; auto stored_value = copy_value(); std::cout << "stored_va原创 2021-10-10 11:40:43 · 100 阅读 · 0 评论 -
C++11 容器
1.线性容器 #include <iostream> #include <array> #include <vector> void foo(int *p, int len) { return; } int main() { //vector std::vector<int> v; std::cout << "size:" << v.size() << std::endl; std::cout <<原创 2021-10-09 17:08:13 · 203 阅读 · 0 评论 -
智能指针与内存管理
std::shared_ptr std::shared_ptr 是一种智能指针,它能够记录多少个 shared_ptr 共同指向一个对象,从而消除显式的调用 delete,当引用计数变为零的时候就会将对象自动删除。 但还不够,因为使用 std::shared_ptr 仍然需要使用 new 来调用,这使得代码出现了某种程度上的不对称。 std::make_shared 就能够用来消除显式的使用 new,所以std::make_shared 会分配创建传入参数中的对象, 并返回这个对象类型的std::sha.原创 2021-10-09 16:40:57 · 172 阅读 · 0 评论 -
原始字面量、内存对齐
noexcept的修饰和操作 #include <iostream> void may_throw() { throw true; } auto non_block_throw = [] { may_throw(); }; void no_throw() noexcept { return; } auto block_throw = []() noexcept { no_throw(); }; int main() { std::cout << std::.原创 2021-10-09 15:44:00 · 77 阅读 · 0 评论 -
C++并行并发
1.并行、互斥量、临界区 #include <iostream> #include <thread> #include <mutex> int v = 1; void critical_section(int change_v) { static std::mutex mtx; //std::lock_guard<std::mutex> lock(mtx); std::unique_lock<std::mutex> lock(mtx);原创 2021-10-09 13:23:37 · 462 阅读 · 0 评论