Lambda 表达式

auto f1 = [](int x, int y) { return x + y; };
cout << f1(2, 3) << endl;

int n = [] (int x, int y) { return x + y; }(5, 4);

Lambda 的类型是个不具名function object

#include <functional>

std::function<int(int,int)> returnLambda() //返回类型为lambda

{

  return [](int x, int y){

    return x*y;

    };

}

auto lf = returnLambda();

cout << lf(6,7) <<endl;

int var;
decltype(var) var1; int

const int&& fx();
decltype(fx()); const int&&

templeate<typename T1, typename T2>
decltype(x+y) add(T1 x, T2 y); //错误x,y未在作用域

templeate<typename T1, typename T2>
auto add(T1 x, T2 y) -> decltype(x+y); //OK

通用工具
Pair <utility>

pair<T1,T2> p;
pair<T1,T2> p(val1,val2);
p.first;
p.second;
get<0>(p); //p.first
get<1>(p); //p.second
p1.swap(p2);
swap(p1,p2);
make_pair(val1,val2);

Touple 扩展pair,建议至少10个 <tuple>

tuple<int,float,string> t1(41,6.3,"nico");
get<0>(t1);

make_tuple(41,6.3,"nico");

元素可以是reference
string s;
tuple<string&> t(s);
get<0>(t) = "hello";

ref()、cref() <functional>

string s;
make_tuple(ref(s));

tuple<int,float,string> t(77,1.1,"s");
int i;
float f;
string s;
tie(i,f,s) = t; //建立一个内含reference的tuple

智能指针 <memory>

unique_ptr 独占式拥有
unique_ptr<int> up(new string("nico"));
(*up)[0] = 'N';
up->...

不允许赋值
unique_ptr<int> up = new int; //ERROR
必须直接初始化
unique_ptr<int> up(new int); //OK

unique_ptr<string> up;
up = nullptr;或up.reset(); //其内delete且赋值为nullptr

unique_ptr<string> up(new string("nico"));
string *sp = up.release();

if (up != nullptr)

if (up.get() != nullptr)

转移拥有权
string *sp = new string("hello");
unique_ptr<string> up1(sp);
unique_ptr<string> up2(sp);//ERROR
unique_ptr<string> up3(std::move(up1));

函数参数是unique_ptr,则参数需要move

void sink(unique_ptr<ClassA> up)
{}

unique_ptr<ClassA> up(new ClassA);
sink(std::move(up));//调用之后up为空
//若想调用之后up也有效,则参数设为引用unique_ptr<ClassA> &up

函数返回unique_ptr,其拥有权移至调用端场景
unique_ptr<ClassA> source()
{
unique_ptr<ClassA> ptr(new ClassA);
...
return ptr;
}

针对Array

unique_ptr<string> up(new string[10]);//ERROR,能编译

偏特化版本 自行调用delete[]
unique_ptr<string[]> up(new string[10]);
此版本不提供* 和 ->操作符,该而提供[]
*up; //ERROR
up[0];

当所指对象要求的不只是调用delete或delete[],就必须具体制定自己的deleter
class ClassADeleter
{
public:
void operator () (ClassA* p){
cout << "call delete for ClassA object" << endl;
delete p;
}
};

unique_ptr<ClassA,ClassADeleter> up(new ClassA());

如果你给的是个函数或lambda,就必须声明deleter的类型为void(*)(T*)或std::function<void(T*)>, (头文件<functional>)
要不就使用decltype

unique_ptr<int, void(*)(int*)> up(new int[10],
[](int* p){
...
delete[] p;
});

unique_ptr<int, std::function<void(int*)>> up(new int[10],
[](int* p){
...
delete[] p;
});

auto l = [](int* p){
...
delete[] p;
};
unique_ptr<int,decltype(l)>> up(new int[10], l);

shared_ptr 可以赋值、拷贝、比较

shared_ptr<string> pNico(new string("nico"));
shared_ptr<string> pNico{new string("nico")};

shared_ptr<string> pNico = new string("nico");//ERROR

shared_ptr<string> pNico = make_shared<string>("nico");

shared_ptr<string> pNico;
pNico = new string("nico");//ERROR
pNico.reset(new string("nico"));

pNico.use_count();//某个对象的当前拥有者数量

定义自己的Deleter
shared_ptr<string> pNico(new string("nico"),
[](string* p){
cout << "delete " << *p << endl;
delete p;
});

针对Array
shared_ptr<int> p(new int[10]);//ERROR,能编译

shared_ptr<int> p(new int[10],
[](int* p){
delete[] p;
});

shared_ptr<int> p(new int[10],
std::default_delete<int[]>());

此与unique_ptr不同
unique_ptr<int> p(new int[10]);//能编译
shared_ptr<int> p(new int[10]);//ERROR,不能编译

shared_ptr不提供operator []

p.get()[2] = 2;
等同于
(&*p)[2] = 2;

误用
int* p = new int;
shared_ptr<int> sp1(p);
shared_ptr<int> sp2(p);
ERROR,sp1、sp2都会在丢失p的拥有权时释放相应资源即delete

shared_ptr<int> sp1(new int);
shared_ptr<int> sp2(sp1);

sp.reset();//放弃拥有权并重新初始化,使它像是empty
sp.reset(ptr);//放弃拥有权并重新初始化(拥有*ptr),使用默认delete
sp.reset(ptr,del);

make_shared(...);//
auto sp3 = make_shared<int>(1);

sp.get();//返回存储的pointer
*sp
sp->
sp.use_count()
if (sp) //判断sp是否为empty

挑选最大最小值 <algorithm>
min(a,b);
min(a,b,cmp);
min(initlist)
min(initlist,cmp)
max...
minmax(a,b) //返回pair<>,其first是最小值,second是最大值
minmax(a,b,cmp)
minmax(initlist)
minmax(initlist,cmp)

swap() <utility>

namespace std{
template<typename T>
inline void swap(T& a, T& b)...{
T tmp(std::move(a));
a = std::move(b);
b = std::move(tmp);
}
}

Class ratio<>的编译期分数运算 <ratio>

ratio<5,3>; 3分之5
ratio<5,3>::num 5
ratio<5,3>::den 3

ratio<0>; //0, den默认为1

Clock和Timer <chrono>

duration 时间段 = tick(片刻数)*时间单位(分数) 1.5个1/3秒

timepoint 时间点 = 一个duration和一个opoch(起始点)

std::chrono::duration<int> twentySeconds(20);
std::chrono::duration<double, std::ratio<60>> halfAMinute(0.5);
std::chrono::duration<long, std::ratio<1,1000>> oneMillisecond(1);

std::chrono::huors
std::chrono::minutes
std::chrono::seconds twentySeconds(20);
std::chrono::milliseconds
std::chrono::microseconds
std::chrono::nanoseconds

隐式转换可以转换至较精准的单位类型
可以将小时转换为秒,反之则不行

duration d
d.count() //返回d的tick数量
duration_cast<D>(d) //显示转换
duration::zero() //获得长度为0的duration
duration::max() //
duration::min()
duration::rep() //获得tick的类型
duration::period() //获得单位类型的类型

Clock 和 Timepoint

Clock 定义一个epoch 和一个tick周期
clock::duration 获得clock的duration类型
clock::rep 获得tick类型 <=> clock::duration::rep
clock::period 获得单位类型的类型 <=> clock::duration::period
clock::time_point 获得clock的timepoint类型
clock::is_steady 如果clock是steady则为TRUE
clock::now() 获得一个表示目前时间的time_point

C++标准库提供了三个clock

1.system_clock 系统的即时时钟 tick: 0.000100 milliseconds
is_steady:false
to_time_t()
form_time_t()

2.steady_clock tick: 1.000000 milliseconds

3.high_resolution_clock 当前系统中带有最短tick周期的clock tick: 0.000100 milliseconds
is_steady:true

#include <chrono>
#include <ctime>
#include <string>
#include <iostream>

using namespace std;
using namespace std::chrono;

string asString(const system_clock::time_point& tp)
{
time_t t = system_clock::to_time_t(tp);
string ts;// = ctime(&t); //转换为日历时间
char arr[100];
memset(arr, 0, 100);
ctime_s(arr, 100, &t);
ts = arr;
if (ts.size() > 0)
ts.resize(ts.size() - 1); //移除末端的newline字符
return ts;
}

int main(int argc, _TCHAR* argv[])
{
//epoch:由clock的time_point的默认构造函数产出
system_clock::time_point tp;
//等价于
time_point<system_clock> tp1;
cout << "epoch: " << asString(tp) << endl;

tp = system_clock::now();
cout << "now: " << asString(tp) << endl;

tp = system_clock::time_point::min(); //出错??
cout << "min: " << asString(tp) << endl;

tp = system_clock::time_point::max(); //出错??
cout << "max: " << asString(tp) << endl;

return 0;
}

C 提供的 Date/Time函数

<time.h> --> <ctime>

clock_t 数值类型 long ,表示elapsed CPU time,由clock()返回
time_t 数值类型,表现timepoint
struct tm 被解开之日历时间的类型

clock() 获得elapsed CPU time,该程序从启动到函数调用占用CPU的时间,
单位是1/CLOCKS_PER_SEC秒 1/1000 即 毫秒
time() 获得当前时间,是个数值time_t t; time(&t);//same as:t = time(NULL);
difftime() 获得2个time_t之间的差值,double,单位秒

localtime() 转换time_t成为一个struct tm,考虑时区
gmtime() 转换time_t成为一个struct tm,不考虑时区
asctime() 转换struct tm成为一个标准日历时间字符串
strftime() 转换struct tm成为一个用户自定义的日历时间字符串
ctime() 转换time_t成为一个标准日历时间字符串,考虑时区
相当于asctime(localtime(t))
mktime() 转换struct tm成为一个time_t并查询其为星期中的哪一天,和一年中的第几天

struct tm
{
int tm_sec; // seconds after the minute - [0, 60] including leap second
int tm_min; // minutes after the hour - [0, 59]
int tm_hour; // hours since midnight - [0, 23]
int tm_mday; // day of the month - [1, 31]
int tm_mon; // months since January - [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday - [0, 6]
int tm_yday; // days since January 1 - [0, 365]
int tm_isdst; // daylight savings time flag
};

_time64和time_t默认情况下,等效于 __time64_t
如果需要强制编译器将解释time_t为旧的 32 位time_t,你可以定义 _USE_32BIT_TIME_T。
不建议这样做,因为应用程序可能会在 2038 年 1 月 18 日后失效;64 位平台上不允许使用此宏。

clock_t为long,一个带符号的 32 位整数,和CLOCKS_PER_SEC宏定义为 1000
这样,最多时钟函数返回值为 2147483.647 秒,或大约 24.8 天
所以一般不使用此函数

lambda、pair、智能指针及时间函数的更多相关文章

  1. C++解析(20):智能指针与类型转换函数

    0.目录 1.智能指针 2.转换构造函数 3.类型转换函数 4.小结 1.智能指针 内存泄漏(臭名昭著的Bug): 动态申请堆空间,用完后不归还 C++语言中没有垃圾回收机制 指针无法控制所指堆空间的 ...

  2. 详解C++11智能指针

    前言 C++里面的四个智能指针: auto_ptr, unique_ptr,shared_ptr, weak_ptr 其中后三个是C++11支持,并且第一个已经被C++11弃用. C++11智能指针介 ...

  3. [3] 智能指针std::auto_ptr

    [1]std::auto_ptr 对于编译器来说,智能指针实质是一个栈对象,而并非指针类型. 智能指针通过构造函数获取堆内存的管理所有权,而在其生命期结束时,再通过析构函数释放由它所管理的堆内存. 所 ...

  4. C++ 11 智能指针 lamda 以及一个 围棋程序

    lamda表达式使用 char* p = "Hello world"; ,nl = ; for_each(p,p+, [&](char i){ if(i=='e') ne+ ...

  5. C++智能指针的enable_shared_from_this和shared_from_this机制

    前言 之前学习muduo网络库的时候,看到作者陈硕用到了enable_shared_from_this和shared_from_this,一直对此概念是一个模糊的认识,隐约记着这个机制是在计数器智能指 ...

  6. C++ 11 智能指针(shared_ptr)类成员函数详解

    C++ 11 模板库的 <memory> 头文件中定义的智能指针,即 shared_ptr 模板类,用来管理指针的存储,提供有限的内存回收函数,可同时与其他对象共享该管理功能. share ...

  7. c++ 智能指针、函数指针和指针函数

    智能指针: 1.内存泄漏memory leak :是指程序在申请内存后,无法释放已申请的内存空间,一次内存泄漏似乎不会有大的影响,但内存泄漏堆积后的后果就是内存溢出. 2.内存溢出 out of me ...

  8. PCL智能指针疑云 <三> 智能指针作为函数的传值参数和传引用参数

    一 函数的参数传递可以简单分类为“传值”和“传引用”. 声明函数时,形参带引用“&”,则函数调用时,是把实参所在的内存直接传给函数所开辟的栈内存.在函数内对形参的修改相当于对实参也进行修改. ...

  9. Effective C++ .14 智能指针的拷贝与deleter函数

    #include <iostream> #include <cstdlib> #include <memory> using namespace std; clas ...

随机推荐

  1. HDU - 5521 Meeting (Dijkstra)

    思路: 看了好久才看懂题意,文中给了n个点,有m个集合,每个集合有s个点,集合内的每两个点之间有一个权值为t的边,现在有两个人,要从1号点,和n号点,走到同一个顶点,问最少花费以及花费最少的点. 那就 ...

  2. 桌面面板和内部窗体JDeskPane、JInternalFrame

    桌面面板和内部窗体JDeskPane.JInternalFrame,内部窗体必须在桌面面板里. import javax.swing.*; import java.awt.*; public clas ...

  3. 二维数组过滤,根据多个条件获取二维数组中指定的arr

    /** * 二维数组过滤,根据多个条件获取二维数组中指定的arr * @param $data_arr * @param $lm_number * @param $source_type * @par ...

  4. 7.Django

    1.遍历数据 2.正则表达式匹配数字 ##url超链接 ##配置url ##POST请求需要设置csrf_token

  5. linux服务器,发现大量TIME_WAIT

    linux服务器,发现大量TIME_WAIT 今天登陆linux服务器,发现大量TIME_WAIT参考资料:http://coolnull.com/3605.html 酷喃|coolnull| » 大 ...

  6. bzoj2333 离线 + 线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...

  7. 关于交叉熵(cross entropy),你了解哪些

    二分~多分~Softmax~理预 一.简介 在二分类问题中,你可以根据神经网络节点的输出,通过一个激活函数如Sigmoid,将其转换为属于某一类的概率,为了给出具体的分类结果,你可以取0.5作为阈值, ...

  8. Java中的XML

    XML是一种可扩展的标记语言,可扩展就是<>内的东西可以自己定义,可以随便写.标记语言就是加了<>符号的 .HTML是超文本标记语言,不可以拓展,因为你写个<p> ...

  9. JAVA核心技术I---JAVA基础知识(格式化相关类)

    一:格式化相关类 (一)java.text包java.text.Format的子类 –NumberFormat:数字格式化,抽象类 DecimalFormat –MessageFormat:字符串格式 ...

  10. Linux 内核里的数据结构:红黑树(rb-tree)

    转自:https://www.cnblogs.com/slgkaifa/p/6780299.html 作为一种数据结构.红黑树可谓不算朴素.由于各种宣传让它过于神奇,网上搜罗了一大堆的关于红黑树的文章 ...