std

C++多线程Multithreading std::condition_variable

多线程Multithreading #include <iostream> #include <thread> #include <mutex> #include <condition_variable> std::mutex mtx; std::condition_variable cv; boo ......

库函数 | C++17 std::filesystem文件系统 用法指北

本文将针对常用的场景,对 std::filesystem 的使用逐一进行验证: 判断文件夹是否存在 创建单层目录 逐级创建多层目录 创建多级目录 当前文件路径 创建文件"from.dat" 获取相对于base的绝对路径 文件拷贝 移动文件或重命名 创建文件 “example.dat” 获取文件大小 ......
filesystem 函数 文件 系统 std

原子操作量 std::atomic<bool>

相较于普通的bool,std::atomic<bool>有以下优点 `std::atomic<bool>` 和普通的 `bool` 之间有几个重要区别,主要涉及多线程环境下的并发访问和修改: 1. 原子性操作: `std::atomic<bool>` 提供了原子性操作,这意味着在多线程环境下,它的读 ......
原子 atomic bool std lt

C++11中std::ref()与&

C++11中std::ref()与& 引言 最近看到一个多线程代码如下: typedef unsigned long long ULL; void accumulator_function(const std::vector<int> &v, ULL &acm, unsigned int begin ......
std ref amp 11

C++ | 完美转发:std::forward

引用折叠 template <class T> void func(T && arg); 若一个右值引用(&&)参数被一个左值或左值引用初始化,那么引用将折叠为左值引用。(即:T&& & –> T&) int a = 1; func(a); // func()中 参数arg 变成 int &类型 i ......
forward std

C++ std::this_thread知识整理

std::this_thread::get_id() std::this_thread::yield()std::this_thread::sleep_for ()std::this_thread::sleep_until () 文章目录前言一、this_thread 是什么?二、使用步骤1.引入库 ......
this_thread 知识 thread this std

C++完美转发为什么必须要有std::forward?

先看一种情况,它的输出结果是什么? #include <iostream> using namespace std; void F(const int &a) { cout << "int: " << a << endl; } void F(int &&a) { cout << "int &&: " ......
forward std

std list多线程使用

#include <iostream> #include <list> #include <thread> #include <mutex> #include <condition_variable> #include <unistd.h> std::list<int> my_list; std:: ......
线程 list std

取得std::ifstream对象的文件描述符

使用C++标准库无法取得std::ifstream对象的文件描述符,但GNU libstdc++库可以取得: ```cpp #include #include #include int main() { std::ifstream ifs("test.txt"); if (!ifs) { std:: ......
ifstream 对象 文件 std

std::copy与std::back_inserter引发的惨案

#include <iostream> #include <vector> #include <numeric> #include <sstream> int main() { std::vector v{1, 2, 3, 4, 5}; std::copy(begin(v), end(v), std ......
惨案 back_inserter std inserter copy

[C++] std::optional与RVO:最高效的std::optional实践与探究

## 返回值优化RVO 在cppreference中,是这么介绍RVO的 `In a return statement, when the operand is the name of a non-volatile object with automatic storage duration, wh ......
optional std RVO

【紧急科普】关于 std::vector 和 std::list 谁性能好的讲解

很多面试官。。。唉。不是一个年代的人吧。八股文当中现在倾向于说 list 中间插入性能更好。 1,std::vector 和 std::list 同属逻辑线性表。 2,std::vector 在内存当中连续,std::list 在内存当中不连续。 3,std::vector 因为在内存当中连续,随机 ......
科普 std 性能 vector list

std::for_each易忽略点

以下代码为修改vector内部的每一个元素,使其每个元素大小变为原来的平方。 std::vector v1{1, 2, 4, 2}; std::for_each(begin(v1), end(v1), [](auto& n) { return n * n; }); for (const auto& ......
for_each each std for

std模版库 队列、优先队列、双端队列

queue为单端队列 deque为双端队列 priority_queue为优先队列 #include #include priority_queue, less> // 最大堆 默认为对大堆 也即和 priority_queue 等价 priority_queue, greater> // 最小堆 ......
队列 模版 std

C++11 右值引用&&、移动语义std::move、完美转发std::forward

参考:https://blog.csdn.net/HR_Reborn/article/details/130363997 #pragma once class Array { public: Array() : size_(0), data_(nullptr){ } Array(int size) ......
语义 amp std forward move

c++ stl std::sort使用例子

class User { public: int32_t m_fight_power; private: int32_t m_level; }; bool CenterData::compare(const User *left, const User *right) { if(left->m_fi ......
例子 sort stl std

std::allocator

设计主要目的:减少malloc的cookie的开销 设计主要方法:减少malloc的次数,用数据结构管理已经分配的内存。 核心数据结构:free_list[16]存储16根链表头,free_list[i],0~15 ,free_list[i]管理(i + 1)*16字节型区块。 客户所需要内存块大小 ......
allocator std

C++里std::enable_shared_from_this是干什么用的?

std::enable_shared_from_this使用场景 在很多场合,经常会遇到一种情况,如何安全的获取对象的this指针,一般来说我们不建议直接返回this指针,可以想象下有这么一种情况,返回的this指针保存在外部一个局部/全局变量,当对象已经被析构了,但是外部变量并不知道指针指向的对象 ......

C++ 多线程详解之异步编程 std::packaged_task

std::packaged_task 将任何可调用对象(比如函数、lambda 表达式等等)封装成一个 task,可以异步执行。执行结果可以使用 std::future 获取。 比如下面的例子,构造一个 std::packaged_task 后,get_future() 函数返回一个 std::fu ......
线程 packaged_task packaged task std

关于callback和std::bind的那些事

## 前言 使用callback常常需要绑定类的具体函数,哪些可以绑定哪些不能? ## 分析 callback不同与普通函数,其入参也是一个函数,具体行为由入参决定 我们看这样一段代码 https://godbolt.org/z/4YTKs567j ```cpp #include #include ......
callback bind std

关于 std::vector 容器初始化特殊长度导致的 Segmentation Fault 错误

当我们设置 std::vector 的长度时, 常这样书写: ```cpp std::vector vec(length); ``` 这样做一般不会出问题, 编译可正常通过, 然而当把 length 设置为 0 时, 执行有报错: ```bash Segmentation fault ``` 程序发 ......
Segmentation 容器 长度 错误 vector

c++ std::to_string实现原理

写这篇的起因是看到 MSVC STL 的一个[issue](https://github.com/microsoft/STL/issues/3857),里面提到```to_string```的实现,正常人的思维是直接除10拿到每位, 其实有个更高效的查表法 # 字符串转数字 除100拿到两位,并查表 ......
to_string 原理 string std to

std::condition_variable 练习(多线程任务序列化)

#include <functional> #include <map> #include <random> #include <chrono> #include <iostream> #include <format> #include <cmath> #include <thread> #inc ......

std::condition_variable 练习(多线程任务序列化)

#include <functional> #include <map> #include <random> #include <chrono> #include <iostream> #include <format> #include <cmath> #include <thread> #inc ......

C++ 字符串拼接技巧(stringstream、字符串迭代器、字符串的加法运算符、std::accumulate、boost库join)

在C++中,经常需要将多个字符串拼接成一个大字符串。这个过程很容易出错,但有一些技巧可以帮助我们轻松地实现这个目标。本文将介绍一些C++中join字符串的技巧。 一、使用stringstream stringstream是一个流。使用它可以将多个字符串连接起来,然后将它们转换为一个字符串。可以使用' ......

函数指针、std::function、std::bind

# 函数指针、std::function、std::bind ## 函数指针: - C++语法中可以直接将函数名作为指针, ```cpp void fun(int a, int b); ``` 在这个函数声明中,函数指针即为`fun`,传入要被调用的地方时只需要传入`fun`就可以。 但是这个函数指 ......
指针 函数 std function bind

c++ std::hash<std::string> 字符串哈希函数

## msvc 采用了[FNV-1a](http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param)的哈希算法 ``` // 众所周知 std::string 就是一个 basic_string template struct has ......
字符串 函数 std 字符 string

双通道MIL-STD-1553B总线通讯模块

* 双通道MIL-STD-1553B总线通讯模块 * 32bi,33 MHz CPCI/PCI/总线* 每个通道为A、B双冗余总线* 单功能可设置BC/RT/BM一种工作模式* 数据传输率: 4Mbps* 支持32位时标, 时标精度0.25微秒* 软件可设詈应答超时: 0-32767µs* 大容量的 ......
总线 模块 通道 MIL-STD 通讯

std::shared_ptr 线程安全方面的思考

一直惦记着 std::shared_ptr 线程安全的问题,看了些文章后,又怕过段时间忘记了,遂记录下来 std::shared_ptr 的线程安全问题主要有以下两种: 引用计数的加减操作是否线程安全 std::shared_ptr 修改指向时是否线程安全 第一个问题的答案: 是线程安全的,因为是原 ......
线程 shared_ptr 方面 shared std

c++11 std::condition_variable

# std::condition_variable * 需要配合unique_lock使用,`wait(unique_lock&)` * notify_one()调用时,只有随机一个wait()线程会得到通知 * notify_all(),所有wait()线程会被通知并得到执行 * wait()调用 ......
condition_variable condition variable std 11