STD

c++ 智能指针std::shared_ptr

c++11中常用的智能指针有shared_ptr,unique_ptr与weak_ptr。 unique_ptr是独占型指针,其计数永远为1,无拷贝构造函数,但可以使用std::move转移资源所有权。 weak_ptr没有资源所有权,一般用来辅助shared_ptr使用,多使用于多线程,循环等场景 ......
指针 shared_ptr 智能 shared std

关于 using namespace std

我刚接触c++,写Hello, World 是这个样子的 #include <bits/stdc++.h> using namespace std; int main() { cout << "Hello, World" << endl; return 0; } 但是一直令我不解的是 using n ......
namespace using std

Qt 中将std::cout 重定向到 qDebug

#include <QtCore>#include <iostream> void customMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg){ QByteArray loca ......
中将 qDebug cout std Qt

使用 PostMessage 函数来发送带有 std::vector 作为参数的消息

使用 PostMessage 函数来发送带有 std::vector 作为参数的消息,您可以将 std::vector 的地址封装进 LPARAM 类型的指针中,并将其传递给 PostMessage 函数的 lParam 参数。在接收方,您需要使用 reinterpret_cast 将 LPARAM ......
PostMessage 函数 参数 消息 vector

[C++特性]对std::move和std::forward的理解

左值、右值、左值引用以及右值引用 std::move和std::forward这两个API主要服务于左值引用和右值引用的转化和转发,因此再了解这两个API之前,需要先弄清楚这几个概念。 左值:一般指的是在内存中有对应的存储单元的值,最常见的就是程序中创建的变量 右值:和左值相反,一般指的是没有对应存 ......
std 特性 forward move

cpp multi thread std::lock_guard,mutex

#include <atomic> #include <chrono> #include <cmath> #include <condition_variable> #include <ctime> #include <fstream> #include <functional> #include ......
lock_guard thread multi guard mutex

cpp multi thread sync via std::atomic<bool>

#include <atomic> #include <chrono> #include <cmath> #include <condition_variable> #include <ctime> #include <fstream> #include <functional> #include ......
atomic thread multi bool sync

c++11:std::forward,完美转发

目录 1、不完美转发 2、完美转发 2.1、引用折叠 2.2、std::forward 1、不完美转发所谓完美转发,是指在函数模板中,完全按照模板的参数的类型,将参数传递给函数模板中调用的另一个函数。比如: template <typename T>void IamForwording(T t){ ......
forward std 11

C++-std::this_thread::get_id()-获取线程id

C++-std::this_thread::get_id()-获取线程id std::this_thread::get_id() 头文件:<thread> 函数:std::this_thread::get_id() 用例:std::thread::id thread_id = std::this_t ......
线程 this_thread get_id thread this

c++11 std::forward使用场景以及作用

不使用 std::forward时,下述代码G不管传入什么类型的参数,只会最终调用 void F(int& a); using namespace std; void F(int& a) { cout << "int& version " <<a <<endl; } void F(int&& a) ......
场景 作用 forward std 11

【c&c++】C++ 关于编译出现“undefined reference to `std::cout‘“的问题

1、问题概述 在使用gcc编译c++代码时会出现undefined reference to `std::cout',如编译如下代码: #include<iostream> using namespace std; int main() { cout<<"Hello world!"; return ......
undefined reference 问题 cout amp

【c&c++】std::string::npos的使用

std::string::nposstd::string::npos是一个常数,它等于size_type类型可以表示的最大值,用来表示一个不存在的位置,类型一般是std::container_type::size_type。 定义static const size_type npos = -1; # ......
string npos amp std

std::initialize_list<T>作为返回值

正常情况,我们应该返回一个 vector<T>,但是呢,因为我这是嵌入式,我不想动态开辟。于是我突发其想,返回个intialize_list,在写这个代码时,我就觉得可能不正确,因为intialize_list类中没有个数。抱着试一试的心态,我还是写了这个代码。结果,果然是不正确的。 原因是这样的, ......
initialize_list initialize list std gt

C++11之std::future对象的基本用法

1、 // future example #include <iostream> // std::cout #include <future> // std::async, std::future #include <chrono> // std::chrono::milliseconds // a ......
对象 future std 11

std::string对象被释放后仍然访问std::string::c_str()返回的指针(访问已经释放掉的内存)的一些总结

一个值得注意的事情 今天在调试程序,发现一个严重又很难排查的问题! 有关如何正确使用函数的”返回值“。 先看一下简单代码: #include <iostream> using namespace std; const char *test1() { std::string str = "hello" ......
string 指针 std 对象 内存

std::any

#include <iostream> #include <any> int main() { std::any a = 10; std::cout << a.type().name() << std::endl; std::cout << std::any_cast<int>(a) << std: ......
std any

如何将 std::queue 封装成线程安全的队列

仅使用互斥保护每个成员函数 问题 1 尽管运用互斥保护共享数据,条件竞争仍然无法避免,考虑如下代码: if (!q.empty()) { const int value = q.front(); q.pop(); do_something(value); } 在 empty() 和 front() ......
队列 线程 queue std

C++ std::thread join()的理解

转自:https://www.cnblogs.com/adorkable/p/12722209.html 在学习C++11的std::thread时,起初非常不理解join()函数的作用以及使用场景,官方的解释又比较晦涩难懂,总觉得get不到关键点。看了很多文章后加上自己的理解,才觉得有了一点眉目, ......
thread join std

cpp test for and while loop time cost respectively while std::chrono::high_resolution_clock

#include <chrono> #include <condition_variable> #include <ctime> #include <fstream> #include <future> #include <iomanip> #include <iostream> #include ......

cpp std::this_thread::sleep_for(std::chrono::seconds(sleep_seconds)) for thread execution duration

#include <chrono> #include <condition_variable> #include <ctime> #include <fstream> #include <future> #include <iomanip> #include <iostream> #include ......

time_t now=time(NULL); std::cout<<ctime(&another_time);tm* ltm = localtime(&now);

#include <iostream> #include <iomanip> #include <ctime> #include<windows.h> int main() { time_t now = time(NULL); tm* ltm = localtime(&now); std::cout ......
time another_time now amp localtime

慎用std::move

编译arm版本成功, 空跑正常, 跑业务崩溃在了如下函数: deliverPacket_internal_(std::move(video_packet), false, video_packet->big) x86机器运行正常. 怀疑点: 1、依赖库问题? 所有的lib都copy到了运行环境, 大 ......
move std

std::cout 的各种输出格式

在C++中,setf()是流操作符中的一个函数,用于设置格式化标志。它有多种参数,每个参数都代表着一种特定的标志。下面是一些常见的参数及其含义: ios::scientific: 使用科学计数法表示浮点数。 ios::fixed: 使用固定点表示法表示浮点数。 ios::left: 左对齐输出。 i ......
格式 cout std

关于std::vector<bool>

一、引子 std::vector<bool> 经常应用在leetcode刷题中。 但是effective stl 不建议使用std::vector<bool>,原因是: 严格意义上讲,vector<bool> 并不是一个 STL 容器; vector<bool> 底层存储的并不是 bool 类型值。 ......
vector bool std

【segmentation fault】std::string析构崩溃

今天写了一个小工具,运行时发生segmentation fault,现象如下 第一步:review崩溃附近代码,产生疑惑,崩溃的地方居然是变量定义的地方 std::string accessToken; 崩溃在这个地方,我直接懵了,只是变量定义为啥会报错,没有任何思路,打算单步调试。 第二步:单步调 ......
segmentation string fault std

c++ std::string_view

std::string_view系C++17标准发布后新增的内容。 C++17中我们可以使用std::string_view来获取一个字符串的视图,字符串视图并不真正的创建或者拷贝字符串,而只是拥有一个字符串的查看功能。std::string_view比std::string的性能要高很多,因为每个 ......
string_view string view std

c++ std::variant

std::variant 是c++17 引入的一个类型,其作用类似于C语言中的Union,但是比Union 的功能强大的多。C语言中一个联合体Union 可以储存多种类型数据,但缺点有很多。比如:1 没有可用的方法来判断Union中真实储存的类型,获取值时也是内存拷贝的结果,可能会存在问题。这就只能 ......
variant std

std::minmax_element的简单用法

获取一个数组中的最大值和最小值,通过匿名函数声明自定义比较策略。 #include <iostream> #include <vector> #include <algorithm> #include <string> #define BUFSIZE 6 using namespace std; t ......
minmax_element element minmax std

c++ std::package_task,task.get_future()

#include <iostream> #include <future> #include <thread> int countdown(int from,int to) { for(int i=from;i!=to;--i) { std::cout<<i<<std::endl; std::thi ......
task package_task get_future package future

C++17:新特性之std::optional

考虑一个问题,C++如何实现返回多个值?如何标记其中一个bool返回值用于记录函数运行状态? 我们可以通过pair或tuple实现,有以下代码: #include <iostream> #include <string> using namespace std; struct ss { string ......
optional 特性 std 17