逐个使用C++11新特性
C++11
auto & decltype
auto:根据变量初始值来推导变量类型,并用右值初始化变量。
int main()
{
map<string, vector<string>> family;
family.insert(pair<string, vector<string>>("陈", {"澄", "尘"}));
family.insert(pair<string, vector<string>>("朱", {"珠", "茱"}));
for(const auto &mp : family)
{
cout << mp.first << endl;
for(const auto &str : mp.second)
cout << str << " ";
cout << endl;
}
return ;
}
decltype:从表达式推导出类型,并将变量定义为该类型,但不用表达式的值初始化该变量。
这里要注意下:decltype(i)--是i的类型,而decltype((i))就是引用了。就像上面例子中的x 就是int &x;
右值引用
新标准在拷贝构造函数、拷贝赋值运算符和析构函数之外,还增加了移动构造函数和移动赋值运算符,而这二位就需要右值引用的支持。
1. 延长将亡值的生命。
//右值引用
int a = ;
int &b = a;
int &&bb = ;//右值引用只能绑定将亡值
//int &&bb = a;//错,a是左值(持久值)
a = std::move(bb);//右值引用移动 cout << "引用:" << b << endl;
cout << "右值引用:" << bb << endl;
cout << "移动: " << a << endl;
&&bb = 999;//错,类似左值引用,不可二次赋值
&b = 888;//错
我们居然输出了89,要知道,右值只是临时量,用完就扔的。
2. 用于移动(构造)函数
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class test
{
public:
test();
~test();
void push(const int& i);
void push(int&& i)
{
bar = std::move(i);//std::move(右值引用)实现移动
}
private:
int bar;
}; int main()
{
int a = ;
test t;
t.push();
//t.push(a);错误无法将右值绑定到左值 return ;
}
范围for循环
统计字符
string str = "No pain, no gain! Everyone need to struggle for himself.";
decltype(str.length()) count = , count1 =;
for (auto i : str)
if (ispunct(i))
++count;
else
++count1;
cout << "字符串长度:" << str.size() << endl;
cout << "标点字符的个数:" << count << endl;
cout << "其他字符的个数:" << count1 << endl;
改写字符
#include <iostream>
#include <string>
using namespace std; int main()
{
string str("No pain, no gain! Everyone need to struggle for himself.");
cout << str << endl; for (auto &c : str)
c = toupper(c);//小写换大写 cout << str << endl;;
return ;
}
去除字符
//去除标点 1
#include <iostream>
#include <string>
using namespace std; int main()
{
string input, res = "";
while (cin >> input)
{
for (auto c : input)
if (!ispunct(c))
res += c;
cout << res << endl;
res = "";
}
}
智能指针
std::weak_ptr & std::shared_ptr
lambda表达式·λ
先看它怎么用
例1
//惯用法
//[捕获外部变量](参数列表)->返回类型{ 函数体(别忘记";")}
auto str = "I want you!";
auto f = [](string str)->int { return str.size(); };
cout << f(str) << endl; int m = ;
//捕获参数---值捕获
auto getValue = [m] {cout << m << endl; };
getValue();
//捕获参数---引用捕获
auto getValue1 = [&m] {cout << m << endl; };
getValue1(); //捕获参数---隐式值捕获
auto getValue2 = [=] {cout << m << endl; };
getValue2(); //捕获参数---隐式引用捕获
auto getValue3 = [&] {
cout << m << endl;
cout << "修改:" << ++m << endl;
};
getValue3(); int num[] = { , , , , };
sort(num, num + , [](const int a, const int b) ->bool {return a > b; }); for (auto i : num)
cout << i << " ";
例子补充:
int main()
{
//lambda表达式
//1.标准写法[捕获函数内局部变量列表] (传入参数列表) ->return type {function body;} ;
auto fc = []()->int {
return ;
};
cout << "1.标准写法\n" << fc() << endl; //2.带传参数
auto f = [](int a, int b) {
return a + b;
};
cout << "2.带传参数\n" << f(, ) << endl; //3.捕获变量值
int param = ;
auto ff = [param](int b){
return param + b;
};
cout << "3.捕获变量值\n" << ff() << endl; //4.捕获变量引用
int param2 = ;
auto ff2 = [¶m2](int b){
param2 += ;
return param2 + b;
};
cout << "4.捕获变量引用\n" <<ff2() << endl;
cout << param2 << endl; //4.当作算法函数谓词,例修改sort排序降序
vector<int> vec = {, , , , , };
sort(vec.begin(), vec.end(), [](int a, int b){ return a > b;});
for(auto i : vec)
cout << i << " "; //隐式引用捕获:即默认局部变量都是引用类型,可以在λ体内修改,也可以在参数捕获列表中指定某些参数只是值捕获,否则全部默认引用捕获
string str = "lambda";
string str2 = " is good candy!";
auto addStr = [&]{
str = "lambda really";
return str + str2;
};
cout << addStr() << " " << str << endl;
return ;
}
它的优势在哪里?又或者说为什么要用它呢?
再看一个例子
例2
//多线程
for (int i = ; i < ; ++i)
{
std::thread t([i] { cout << i << endl; });//<thread>
t.detach();
}
从上面的多个例子看来,我们可以很方便的实现“函数内的函数定义和使用”,即函数嵌套【例1】,这样一来就不用再在外部定义函数,然后才能使用【例2】。甚至,它连函数名都可以不要,就像上面的sort函数中的用法一样。极大的简化了我们的代码,可读性也增强了许多,不用再回到定义处推敲了。
逐个使用C++11新特性的更多相关文章
- C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)
因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...
- C++11新特性总结 (二)
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
- C++11新特性总结 (一)
1. 概述 最近在看C++ Primer5 刚好看到一半,总结一下C++11里面确实加了很多新东西,如果没有任何了解,别说自己写了,看别人写的代码估计都会有些吃力.C++ Primer5是学习C++1 ...
- C++ 11 新特性
C++11新特性: 1.auto 2.nullptr 3.for 4.lambda表达式 5.override ...
- [转载] C++11新特性
C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...
- 在C++98基础上学习C++11新特性
自己一直用的是C++98规范来编程,对于C++11只闻其名却没用过其特性.近期因为工作的需要,需要掌握C++11的一些特性,所以查阅了一些C++11资料.因为自己有C++98的基础,所以从C++98过 ...
- C++11新特性——range for
很多编程语言都有range for语法功能,自C++11起,终于将这个重要功能加入C++标准中.range for语句,可以方便的遍历给定序列中的每个元素并对其执行某种操作. 1.基本语法 for(d ...
- C++11新特性——大括号初始化
C++11之前,C++主要有以下几种初始化方式: //小括号初始化 string str("hello"); //等号初始化 string str="hello" ...
- C++11新特性之六——元编程
C++11新特性之六——元编程
随机推荐
- Python:游戏:300行代码实现俄罗斯方块
本文代码基于 python3.6 和 pygame1.9.4. 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.但是想到旋转,停靠,消除等操作,感觉好像很难啊, ...
- 中小研发团队架构实践之分布式协调器ZooKeeper
一.ZooKeeper是什么 Apache ZooKeeper是由Apache Hadoop的子项目发展而来,于2010年11月正式成为了Apache的顶级项目. ZooKeeper是一个开放源代码 ...
- Mqtt学习指南
MQTT是物联网应用当中一种非常重要的,轻量级的协议,现将该协议的重要学习资源整理一下,希望能为初学者提供一个完整的学习资源. MQTT是一个客户端服务端架构的发布/订阅模式的消息传输协议.它的设计思 ...
- 5.JAVA-内部类实例
在JAVA中,类内部可以添加其它类,当然也可以实现类继承(后续章节学习). 本章示例-实现部门类和雇员类 可以通过部门对象,查找该部门的雇员信息. 可以通过雇员对象,查找该雇员所在的部门信息 代码如下 ...
- 六大设计原则(二)LSP里氏替换原则
里氏替换原则LSP(Liskov Subsituation Principle) 里氏替换原则定义 所有父类出现的地方可以使用子类替换并不会出现错误或异常,但是反之子类出现的地方不一定能用父类替换. ...
- 使用bat脚本永久激活Windows系统
每次重装完系统后,右下角会提示系统未激活,无法进行一些个性化设置. 在这里我自己写了一个bat脚本用于激活Windows系统.(仅供学习) 文件下载: 链接:https://pan.baidu.com ...
- SqlServer中循环给多张表建立聚簇索引
缘由 因为在某个复(bian)杂(tai)需求中用到了170+张表进行查询,而且表中的数据过多,查起来缓慢.只能给这些表添加索引.但是,连表名也是无法确定的(无力吐槽). 解决方法 使用游标遍历查询出 ...
- July 09th, 2018. Monday, Week 28th.
Happiness is an inside job. 自内寻找,才能找到幸福. From William Arthur Ward. Nobody wants to suffer, and we al ...
- Exp5 Msf基础应用 20164312 马孝涛
1.本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践,如ms08_067; (1分) 1.2 一个针对浏览器的攻击,如ms11_ ...
- 音视频 学习&开发&测试 资源
一.FFmpeg 学习 1. 官方API文档 FFmpeg Documentation:http://www.ffmpeg.org/doxygen/trunk/index.html 2. 优秀开源项目 ...