
C++11
Lasuerte
这个作者很懒,什么都没留下…
展开
-
C++11 线程池
#include <cstdlib>#include <string>#include <iostream>#include <memory>#include <thread>#include <vector>#include <queue>#include <mutex>#include <condition_variable>#include <atomic>#i.原创 2020-06-13 14:42:59 · 329 阅读 · 0 评论 -
类模板内的static成员变量
类模板内的static成员变量是在需要用的时候才会去调用类外初始化,否则实例化子类后,直接调用b的成员函数等时,会报错undefined reference#include <iostream>using namespace std;template <typename T>class CTest {public: class B { public: B() { cout << "construct B" &l原创 2020-05-21 17:13:53 · 537 阅读 · 0 评论 -
C++ 委托学习笔记
class A{public: void Func(int){...}};要取得Func函数指针,void (A::*pFunc)(int)=&A::Func;::*是一个特殊操作符,表示pFunc是一个指针,指向A的成员。获取成员函数的地址不能通过类对象来获取,必须通过类名获取,而且要加上取地址操作。那么如果通过指针来调用该函数,成员函数都隐含了一个this参数,表示函数要操作的对象,我们只获取了函数的指针,还缺少一个对象作为this参数,为了这个目的...原创 2020-05-13 11:17:11 · 279 阅读 · 0 评论 -
C++11中的右值引用
个人理解就是用一个变量引用一个右值,比如将亡值,以延长其生命周期。变量引用的是右值,不过变量本身还是个左值。#include <iostream>struct MyString{ MyString() { m_szStr = 0; m_size = 0; } MyString(char* str) ...原创 2019-05-07 20:59:41 · 420 阅读 · 0 评论 -
type_traits学习
获取T的原始类型,我们通过std::remove_reference移除引用,需要获取智能指针指向的对象时需要对原始类型U添加左值引用。#include <iostream>#include <type_traits>#include <memory>template <typename T>struct Const...原创 2019-05-06 17:30:50 · 461 阅读 · 0 评论 -
lambda表达式中的参数列表
首先,与普通函数的参数列表一致。如果不需要参数传递,则可以连同括号“()”一起省略。可以在lambda表达式末尾用括号添加参数列表的参数,例子如下:void TestLambda(){ int a = 1; int b = 2; auto funcAdd = [=, &b](int c, int d) { return b += ...原创 2019-02-14 11:59:00 · 2290 阅读 · 0 评论 -
三个线程按顺序打印ABC
首先思路是一个线程在工作时,需要阻塞另外两个线程,这样需要三个线程共用一个互斥锁,但问题是怎样指定顺序呢。想到的办法是通过一个全局变量nFlag,以此判断下一个应该打印哪一个字母。但是如何指定呢,比如A打印完成后,nFlag指定B,而C线程继续等待。可以在进入互斥锁前设定一个死循环,没有轮到的字母一直阻塞在这里,而轮到的线程进入工作状态。所以代码如下// InOrderABC...原创 2019-01-24 11:48:27 · 1188 阅读 · 0 评论 -
C++11 wstring、string、utf-8、utf-16之间的相互转换
// C++11_wstring_string.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <locale>#include <codecvt>#include <iostream>#include <string>#include <windows.h>std...原创 2018-09-06 11:51:22 · 7255 阅读 · 0 评论 -
auto 遍历二维数组
int TestArray(){ int iA[3][4] = { { 1, 2, 3 }, { 6, 5, 4 }, { 8, 9, 7 } }; int ia[3][4]; size_t cnt = 0; for (auto& row : ia) { for (auto& col : row) { ...原创 2018-09-03 14:13:01 · 2658 阅读 · 1 评论 -
C++11 shared_ptr与unique_ptr练习
// shared_ptr练习.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <memory>#include <iostream>void process(std::shared_ptr<int> sp){ int n = sp.use_count(); std::cou...原创 2018-08-31 16:10:51 · 372 阅读 · 0 评论 -
C++:智能指针之shared_ptr
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.youkuaiyun.com/sixdaycoder/article/details/457877131.智能指针C++中用new来动态分配内存,delete手动释放内存来达到动态管理内存的目的。因为保证在正确的时间释放内存是非常困难的,忘记释放内存就会产生内存泄露。为了更安全、便捷的使用动态内存,C++11标准库提供...转载 2018-08-24 10:53:42 · 373 阅读 · 0 评论 -
C++11之lambda表达式
[capture](parameters) mutable ->return-type{statement}1.[capture]:捕捉列表。捕捉列表总是出现在Lambda函数的开始处。实际上,[]是Lambda引出符。编译器根据该引出符判断接下来的代码是否是Lambda函数。捕捉列表能够捕捉上下文中的变量以供Lambda函数使用;2.(parameters):参数列表。与普通函数的参数列表...原创 2018-05-30 16:20:46 · 300 阅读 · 0 评论 -
C++11学习之Thread——Mutex
// C++11_Mutex.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream> // std::cout#include <thread> // std::thread#include <mutex> // std::mutexv...原创 2018-05-17 17:37:02 · 439 阅读 · 0 评论