shared_ptr shared std ptr

std::make_pair & std::make_tuple 的区别和联系

std::make_pair 和 std::make_tuple 都是用于创建对应类型的对象的 C++ 标准库函数模板。 它们的区别主要在于它们所处理的数据类型和返回类型。 std::make_pair: std::make_pair 用于创建一个 std::pair 对象,std::pair 是一 ......
make make_tuple std make_pair tuple

C++(std::shared_ptr 的成员函数 reset)

std::shared_ptr 是 C++ 标准库提供的智能指针类型之一,用于管理动态分配的资源。reset 是 std::shared_ptr 类的成员函数之一,用于重新分配或重置 std::shared_ptr 所拥有的指针。 以下是 reset 成员函数的基本解释: 函数签名: templat ......
shared_ptr 函数 成员 shared reset

C++ 用 std::get<> 访问元组

C++ 用 std::get<> 访问元组 #include <iostream> #include <tuple> int main() { // Creating a tuple std::tuple<int, double, std::string> myTuple(42, 3.14, "He ......
std get lt gt

C++(using namespace std;)

using namespace std; 是 C++ 中的一条指令,用于指示编译器使用标准命名空间 std 中的所有标识符。这意味着在代码中可以直接使用标准库中的各种类、函数和对象,而无需在每个标识符前面添加 std:: 前缀。 以下是关于这条指令的一些解释: using 关键字: using 是一 ......
namespace using std

C++(std::vector)

这段代码定义了一个 std::vector 对象,该对象的元素类型为 float。std::vector 是 C++ 标准库中的动态数组容器,可以存储多个元素,并且大小可以动态调整。 std::vector<float> 具体解释如下: std::vector: 这是 C++ 标准库中的一个模板类, ......
vector std

C++(std::unique_ptr)

std::unique_ptr 是 C++ 标准库中的一种智能指针,用于管理动态分配的内存。它是一种独占拥有权(exclusive ownership)的智能指针,保证同一时刻只有一个 std::unique_ptr 指向特定的资源。当 std::unique_ptr 被销毁时,它所拥有的资源也会被 ......
unique_ptr unique std ptr

std::move

在使用std::move时,有几个重要的注意事项: std::move并不会真正地移动对象,真正的移动操作是在移动构造函数、移动赋值函数等完成的,std::move只是将参数转换为右值引用1。 std::move将一个左值强制转化为右值引用,以用于移动语义。移动语义,允许直接转移对象的资产和属性的所 ......
move std

C++(std::shared_ptr)

std::shared_ptr 是 C++ 标准库中的一个智能指针类,用于管理动态分配的内存。它提供了一种共享拥有权(shared ownership)的机制,通过引用计数来跟踪指向同一块内存的所有 std::shared_ptr 实例。当最后一个 std::shared_ptr 被销毁时,它会自动 ......
shared_ptr shared std ptr

使用 std::string_view 提升字符串处理性能

C++标准库提供了一个非常优秀的字符串处理类std::string,我们可以通过该类完成各种字符串操作。但是std::string有一个缺点,它的很多操作都是针对字符串实体,存在不必要的内存拷贝的代码,导致字符串的处理性能不尽如人意。 针对这种情况C++17标准引入了std::string_view ......
字符串 string_view 字符 性能 string

灵魂拷问std::enable_shared_from_this,揭秘实现原理

灵魂拷问std::enable_shared_from_this,揭秘实现原理 引言 在C++编程中,使用智能指针是一种安全管理对象生命周期的方式。std::shared_ptr是一种允许多个指针共享对象所有权的智能指针。然而,当一个对象需要获取对自身的shared_ptr时,传统的方法可能导致未定 ......

unique_ptr 和 shared_ptr

`unique_ptr` 和 `shared_ptr` 是 C++ 标准库中的智能指针,用于管理动态分配的对象的生命周期,以避免内存泄漏和手动资源管理的问题。 1. **`unique_ptr`:** - `std::unique_ptr` 是一个独占所有权的智能指针,确保在任何时候只有一个 `un ......
unique_ptr shared_ptr ptr unique shared

std::io::Error, thiserror和anyhow

std::io::Error, thiserror和anyhow 读到一篇非常好的文章baoyachi大佬的<细说Rust错误处理>从Rust中怎么处理错误, 讲到怎么定义自己的错误类型, 再到如何简化错误处理流程, 再到如何统一错误处理的形式. 但是这些都是基于标准库提供功能实现的, 需要手动写一 ......
thiserror anyhow Error std io

std:forward

std::forward 在 C++ 中的主要用途是实现完美转发。它的主要好处有以下几点: 保持值类别:std::forward 能够保持函数参数的值类别(即,它是左值还是右值)。这对于保持潜在的移动语义非常重要2。 优化性能:在模板函数中,我们通常使用 std::forward 来转发函数参数,以 ......
forward std

原生并行版std::accumulate

原生并行版std::accumulate ​ 代码来自《c++并发编程实战》 #include <iostream> #include <numeric> #include <algorithm> #include <thread> #include <functional> #include <v ......
accumulate std

std::is_trivially_destructible的作用

template <class Ty> void destroy(Ty* pointer) { destroy_one(pointer, std::is_trivially_destructible<Ty>{}); } 这样设计的好处主要体现在对泛型编程和内存管理的灵活性上。下面是一些可能的好处: ......

nmap: error while loading shared libraries: libpcap.so.1: cannot open shared object file: No such file or directory解决方法

nmap: error while loading shared libraries: libpcap.so.1: cannot open shared object file: No such file or directory解决方法 nmap运行报错解决方法 在centos7里面直接安装lib ......
shared file directory libraries loading

Rust std fs 比 Python 慢!真的吗!?

作者:Xuanwo Databend Labs 成员,数据库研发工程师 https://github.com/xuanwo 我即将分享一个冗长的故事,从 OpenDAL 的 op.read()开始,以一个意想不到的转折结束。这个过程对我来说非常有启发性,我希望你也能感受到。我会尽力重现这个经历,并附 ......
Python Rust std fs

DDP-mp.set_sharing_strategy('file_system')导致程序不能正常退出

ddp训练结束后,发现进程不终止,显存和cpu仍在占用,尝试 torch.distributed.destroy_process_group() torch.cuda.empty_cache() if dist.get_rank()==0: exit() 等清理方法均无效,主进程不能退出,程序只能手 ......

C++标准库类std::packaged_task

std::packaged_task是C++11引入的标准库类,用于封装可调用对象,如函数等,并将封装对象作为异步任务进行管理,通过与std::future结合使用,完成异步任务结果的获取。 #include <iostream> #include <thread> #include <future ......
packaged_task packaged 标准 task std

C++标准库类std::shared_future

std::shared_future是C++11的标准库类,其与std::future的不同是允许多个线程使用,多个线程可以同步共享,同时其又不会阻塞等待异步任务的完成。std::shared_future同样也提供get()方法用于获取异步执行的结果。 #include <iostream> #i ......
shared_future 标准 shared future std

Modern C++ Starter Kits Share

初入 C++ 搬砖,在团队中开发实际上很少亲自从零开始搭建C++的完整项目,99%时候都是使用基础架构团队或者CI/CD团队搭建好的框架。这里分享几个配置C++项目的模版,它们一般会涉及三方库管理、格式校准、单元测试、静态检查、Sanitizer配置、CI/CD配置等方面,内容比较完善。 Build ......
Starter Modern Share Kits

C++标准库函数std::async

1、std::async std::async是C++11的标准库函数,用于创建执行异步任务并返回std::future对象来获取异步执行的结果状态。该函数最简单的用法如下所示: #include <iostream> #include <thread> #include <future> std: ......
函数 标准 async std

std::future与std::promise在C++多线程同步与数据共享中的应用

1、std::promise与std::future std::promise与std::future通过配合使用完成数据的同步与共享,两者均是模板类;std::promise存储异步执行的值或异常;std::future提供可供访问的异步执行结果。二者配合使用伪码如下: std::promise< ......
线程 std promise 数据 future

【Cxx 20】使用 std::span 代替数组指针传参

我们知道std::string_view可以创建std::string的一个视图,视图本身并不拥有实例,它只是保持视图映射的状态。在不修改实例的情况下,使用std::string_view会让字符串处理的性能大幅提升。实际上,对于那些连续的序列对象我们都可以创建这样一份视图,对于std::vecto ......
数组 指针 span Cxx std

不要轻易定义指向std::vector中的元素的指针

类应该是被封装的,类的用户通过接口使用类提供的功能,而不必关心类的内部如何实现。然而,C++标准库容器 std::vector 的实现渗透到了接口中来。对于以下代码: const int pushNum = 10; std::vector<int> v = { 1,2,3 }; int* p = & ......
指针 指向 元素 vector std

弄清using namespace std的作用

⭐C++标准为了和C区别开!为了正确地使用命名空间,规定头文件不使用后缀.h。 例如当我们使用<iostream.h>时,相当于在C中调用库函数。 使用using namespace std例如 1 #include 2 #include 3 #include 4 using namespace s ......
namespace 作用 using std

C++11 多线程(std::thread)实例

C++11的std::thread在C中已经有一个叫做pthread的东西来进行多线程编程,但是并不好用 (如果你认为句柄、回调式编程很实用,那请当我没说),所以c++11标准库中出现了一个叫作std::thread的东西。 std::thread常用成员函数构造&析构函数 举个栗子 例一:thre ......
线程 实例 thread std 11

std::thread方法join与detach

1、std::join std::join是std::thread类的成员函数之一,用于等待线程的执行完成。 #include <iostream> #include <utility> #include <thread> #include <chrono> #include <atomic> vo ......
方法 detach thread join std

std::function 与 std::bind解决类成员函数作为回调函数的问题

1、std::function std::function是一个模板类,其可对C++可调用的对象进行封装,比如,成员函数、静态函数等;它的基本作用是简化调用的复杂程度,归一化调用方式。 std::function<int(int, int)> int_function:声明方式为<返回值类型(参数类 ......
函数 std function 成员 问题

C++ lambda 内 std::move 失效问题的思考

最近在学习 C++ Move 时,有看到这样一个代码需求:在 lambda 中,将一个捕获参数 move 给另外一个变量。 看似一个很简单常规的操作,然而这个 move 动作却没有生效。 具体代码如下 std::vector<int> vec = {1,2,3}; auto func = [=]() ......
lambda 问题 move std
共380篇  :2/13页 首页上一页2下一页尾页