#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <utility>
#include <tuple>
#include <functional>
#include <memory> using namespace std;
void F(int a){
cout<<"int:"<<a<<endl;
}
void F(int *pa){
cout<<"int *:"<< pa <<endl;
}
void PrintVector(const vector<int> & vi){
cout<< "[";
for(auto it = vi.begin(); it != vi.end(); ++it){
cout<< *it <<",";
}
cout<< "]" <<endl;
}
struct Sum{
Sum(){sum=0;}
void operator()(int n){ sum += n; }
int sum;
}; template<typename T>
void Print(T t){
cout<< t <<endl;
}
template<typename Head, typename... Tail>
void Print(Head head, Tail... tail) {
cout<< head <<", ";
Print(tail...);
}
class Foo{
public:
Foo(){ cout<< "Foo construct."<< this <<endl;}
~Foo(){ cout<< "Foo destruct."<< this <<endl; }
int val;
void bar(){ cout<< "Foo.bar() called."<< val <<endl; }
};
int main(){
/****************************************/
auto i = 12345678;
auto s = "Hello中国";
auto f = 3.14;
vector<int> vi {1,2,3,4};
decltype(f) d = 6.28;
PrintVector(vi); cout<< i << endl;
cout<< s << endl;
cout<< f << endl;
// old style: vector<int>::iterator it = vi.begin()
cout<< endl;
cout << "decltype(x):" << d <<endl;
/****************************************/
// NULL vs. nullptr
int a = 0;
int *pa = nullptr;
int *pb = NULL;
F(a);
F(pa);
F(pb);
bool bEqual = (pa == pb);
cout<< bEqual <<endl;
/****************************************/
// for_each
map<string,int> m1 {
{"Hello中国",110},
{"美国",345},
{"Japan",000}
};
for(auto item: m1){
cout<<"Key:"<<item.first<<",Value:"<<item.second<<endl;
}
for_each(vi.begin(),vi.end(),[](int & x){ x*=2; } );
PrintVector(vi);
// Calls Sum::operator() for each number
Sum sum1 = for_each(vi.begin(),vi.end(),Sum());
cout<<"Sum:"<<sum1.sum<<endl; int arg1=1,arg2=2;
for_each(vi.begin(),vi.end(),[=](int & x){ x *=(arg1+arg2); } );
PrintVector(vi);
for_each(vi.begin(),vi.end(),[=](int & x) -> int { return x *(arg1+arg2); } );
PrintVector(vi);
/****************************************/
// 变长参数模板
auto pair1 = make_pair(110,"Hello中国");
cout<< "pair.first:"<<pair1.first<<",pair.second:"<<pair1.second<<endl;
auto tuple1 = make_tuple("Hello",1,3.14);
cout<< std::get<0>(tuple1) <<endl;
cout<< std::get<1>(tuple1) <<endl;
cout<< std::get<2>(tuple1) <<endl; Print(1);
Print(1,"Hello中国");
Print(1,"Hello中国",3.1415926);
cout<< "******************************" <<endl;
// unique_ptr具有转移语义
unique_ptr<Foo> p1(new Foo());
if(p1) p1->val = 123;
if(p1) p1->bar();
{
unique_ptr<Foo> p2 = std::move(p1);
p2->val *=2;
p2->bar();
}
if(p1) p1->bar();
cout<< "******************************" <<endl;
// shared_ptr
shared_ptr<Foo> sp1(new Foo());
if(sp1) sp1->val = 123;
if(sp1) sp1->bar();
{
shared_ptr<Foo> sp2 = sp1;
sp2->val *=2;
sp2->bar();
}
if(sp1) sp1->bar(); return 0;
}

C++11新特性实验的更多相关文章

  1. c++学习书籍推荐《深入理解C++11 C++11新特性解析与应用》下载

    百度云及其他网盘下载地址:点我 编辑推荐 <深入理解C++11:C++11新特性解析与应用>编辑推荐:C++标准委员会成员和IBM XL编译器中国开发团队共同撰写,权威性毋庸置疑.系统.深 ...

  2. C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)

    因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...

  3. C++11新特性总结 (二)

    1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...

  4. C++11新特性总结 (一)

    1. 概述 最近在看C++ Primer5 刚好看到一半,总结一下C++11里面确实加了很多新东西,如果没有任何了解,别说自己写了,看别人写的代码估计都会有些吃力.C++ Primer5是学习C++1 ...

  5. C++ 11 新特性

    C++11新特性:          1.auto          2.nullptr          3.for          4.lambda表达式          5.override ...

  6. [转载] C++11新特性

    C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...

  7. 在C++98基础上学习C++11新特性

    自己一直用的是C++98规范来编程,对于C++11只闻其名却没用过其特性.近期因为工作的需要,需要掌握C++11的一些特性,所以查阅了一些C++11资料.因为自己有C++98的基础,所以从C++98过 ...

  8. C++11新特性——range for

    很多编程语言都有range for语法功能,自C++11起,终于将这个重要功能加入C++标准中.range for语句,可以方便的遍历给定序列中的每个元素并对其执行某种操作. 1.基本语法 for(d ...

  9. C++11新特性——大括号初始化

    C++11之前,C++主要有以下几种初始化方式: //小括号初始化 string str("hello"); //等号初始化 string str="hello" ...

随机推荐

  1. combogrid获取多个字段的方法

    var g = $('#cc').combogrid('grid'); // 获取表格控件对象var r = g.datagrid('getSelected'); //获取表格当前选中行alert(r ...

  2. 杭电OJ——1032 The 3n + 1 problem

    The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belongin ...

  3. STM32F103 GU906B模块GPRS、短信收发、拨号等功能的实现

    这个程序搞了我很久,尤其是对如何提高响应速度上,程序流程很简单,大概就是: 发送AT指令->等待模块响应->一旦响应了,立即返回,并处理掉. 这个程序不一定只能用在GU906上,程序框架在 ...

  4. js 小数点 取整数

    取整数 Math.round() 小数点 (10/3).toFixed(2)

  5. 《纵向切入ASP.NET 3.5控件和组件开发技术》笔记:高效率事件集合对象

    在之前讲的几个例子中,使用的是最普通的定义事件方法,比如KingTextBox中事件是这样定义的:/// <summary>/// 获得本书更多内容,请看:/// http://blog. ...

  6. C++标准库简介

    C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能. <cname>形式的标准头文件[ <complex>例外]其 ...

  7. CentOS系统时间同步(NTP)

    CentOS系统时间同步的步骤如下: 新装的CentOS系统服务器可能设置了错误的,需要调整时区并调整时间. 如下是CentOS系统使用NTP来从一个时间服务器同步把当前时区调整为上海就是+8区,想改 ...

  8. javascript - = 、==、===、!=、!==、&&、||、!

     = .==.===.!=.!==.&&.||.! /* * = .==.===.!=.!==.&&.||.! */ var a = 1; var b = 1; var ...

  9. Python List+Tuple+Dict+Set小结

    创建List:L = ['Adam', 'Lisa', 'Bart', 'Gechong', 'Kongming'] 显示List:L[0] 遍历List:print (L)和for循环 更新List ...

  10. ant design pro (十三)advanced 错误处理

    一.概述 原文地址:https://pro.ant.design/docs/error-cn 二.详细 2.1.页面级报错 2.1.1.应用场景 路由直接引导到报错页面,比如你输入的网址没有匹配到任何 ...