背景
每一个cpp expression都有一个type 和 value category 属性 前者大家都比较了解,但是后者却常常被忽视
Value Categories in the c language
cpp是由c语言发展而来,因此这里先介绍c 语言中的value category
阅读更多概述
std::shared_ptr是智能指针的一种,在modern c++中被广泛使用(
甚至滥用)虽然天天使用,但是有些细节还不是100%清楚,因此来整理一下 为了方便表述,下文只写shared_ptr,不在写std的namespace.
阅读更多背景
一种很常见的背景是,需要表示未知类型的数据。 比如可能是用户提供的数据,比如是一个Cache的实现, value想支持任意类型的数据
对于这种场景,c语言的出身的开发者通常会使用void*来实现
阅读更多把std::async,std::packaged_task,std::promise三个放在一起来说,是因为他们都可以返回一个std::future对象.简单来说,当某个线程需要等待一个特定的一次性事件(one-off event),它可以用一个"future"来表示这个事件.
阅读更多condition_variable 类是同步原语,能用于阻塞一个线程,或同时阻塞多个线程,直至另一线程修改共享变量(条件)并通知 condition_variable 。
用人话来说,condition_variable,也就是条件变量,是线程间通信的一种方式。
阅读更多背景
move semantics是modern cpp中非常重要的特性,有必要详细了解一下。
updateime: 2022年7月2日
move semantic
基本的内容大家都很熟悉,就不说了
std::move 做了什么
std::move没有move任何内容,只是简单把传进来转换为对应的rvalue reference
阅读更多起因是在看《CplusplusConcurrencyInAction_PracticalMultithreading》的时候,里面讲到初始化std::thread的时候,如果thread funtion的参数列表中有引用,需要传入std::ref才可以得到符合预期的结果。
阅读更多最近在学习node.js,里面讲到node.js的事件机制使用了观察者模式,因此来学习一下。
观察者模式的目的是定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
阅读更多
用人话就是,主线程传给附属线程一个promise Object,然后主线程想要获取附属线程set给promise Object的值(也就是该线程返回的某个结果),需要通过主线程中的promise object 得到对应的future object(每个promise 对应一个 future),然后调用future 的get方法。如果附属线程没有执行作为参数传入的promise的set方法去返回结果,那么程序就会block住。
阅读更多1 std::vector<unsigned char> readFromFile1(const char* filePath) { 2 FILE* file = fopen(filePath, "rb"); 3 std::vector<unsigned char> result; 4 if (file == nullptr) { 5 return result; 6 } 7 8 // 获取文件大小,尽量一次读完 9 size_t fileSize = …
阅读更多先放资料:
How to use boost::property_tree to load and write JSON
How to iterate a boost property tree?
不出现key的方法遍历一个json文件:
1 /* *********************************************** 2 Author :111qqz 3 mail: renkuanze@sensetime.com 4 Created Time :2018年08月17日 星期五 15时29分23秒 5 File Name :ptree.cpp 6 …
阅读更多C++11 std::function 是一种通用、多态的函数封装,它的实例可以对任何可 以调用的目标实体进行存储、复制和调用操作
见下面的例子
1 /* *********************************************** 2 Author :111qqz 3 mail: renkuanze@sensetime.com 4 Created Time :2018年07月19日 星期四 17时41分00秒 5 File Name :bind.cpp 6 ************************************************ */ 7 #include …
阅读更多以前用的办法太老土啦
看到一个since C++11的方法,我觉得比较优雅
1 #include <iostream> 2 #include <chrono> 3 //#include <ratio> 4 #include <thread> 5 6 void f() 7 { 8 std::this_thread::sleep_for(std::chrono::seconds(1)); 9 } 10 11 int main() 12 { 13 auto t1 = std::chrono:: …
阅读更多资料推荐这个:MySQL C API programming tutorial
环境为ubuntu 14.04 lts
需要安装mysql 和mysql 开发包
sudo apt-get install libmysqlclient15-dev mysql-server mysql-client
先在mysql 中建立test数据库和test表格
mysql>create database test; mysql>use test; //切换到test数据库中 mysql> create table test(name varchar(255),num int(10) ); //创 …
阅读更多迫于拙劣的cpp水平,这次来记录一些关于STL算法部分的内容。
参考内容是CS106L的course reader
Iterator Categories
Iterators分为以下五种:
* Output Iterators:可以使用"++";可以用*myItr = value,不能用value = *myItr * Input Iterators:可以使用"++";可以用value = *myItr,不能用*myItr = value * Forward Iterators: 可以使用"++",可以同时用value = *myItr和*myItr = …
阅读更多迫于拙劣的cpp水平,来补补以前忽略掉的cpp细节。
老规矩,先放资料。
参考资料:
A Gentle Introduction to C++ IO Streams
"Designing and implementing a general input/output facility for a programming language is notoriously difficult" - Bjarne Stroustrup
Stream的基本认识
说说我的理解。stream(流)可以看做输入输出的抽象。我们通过流可以忽略掉device的细节,采取同样的输入输出方式。
阅读更多