#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. linux开机启动的三种方式

    参考:www.cnblogs.com/gzggyy/archive/2012/08/07/2626574.html 一./etc/rc.local 其中的rc.local是在完成所有的系统初始化之后执 ...

  2. SpringMVC的学习

    在看<跟开涛学SpringMVC.pdf> /Users/baidu/Documents/Data/Interview/Java Spring Web MVC 也是服务到工作者模式的实现, ...

  3. java设计模式6--适配器模式(Adapter )

    本文地址:http://www.cnblogs.com/archimedes/p/java-adapter-pattern.html,转载请注明源地址. 适配器模式(别名:包装器) 将一个类的接口转换 ...

  4. 引导工具GRUB详解

    导读 引导程序是驻留在硬盘第一个扇区(MPR.主引导记录)的程序.GRUB是一个功能强大的多系统引导程序,专门处理Linux与其它操作系统共存的问题.下面就由我介绍一下grub.conf文件里的具体内 ...

  5. Keepalived+Nginx实现高可用和双主节点负载均衡

    简介 Nginx可以实现高并发反向代理,lvs集群可以实现负载均衡,但是他们都有一个共同的弊端,就是Nginx,lvs架构中Director是单点故障,有没有一个好的方案解决这个问题呢?答案是有.通过 ...

  6. Qt 之 QtConcurrent

    本文以 Qt 中的 QtConcurrent::run() 函数为例,介绍如何将函数运行在单独的某一个线程中. 1  QtConcurrent::run() QtConcurrent 是一个命名空间, ...

  7. Unity Editor类常用方法

    http://www.cnblogs.com/zhaoqingqing/p/3944718.html 一些比较常用的Editor功能我在之前的博客中也有提到过所以就不详细写啦,今天参考了一下 麒麟子( ...

  8. ubuntu执行级别,设置单用户模式

    redhat的runlevel级别定义例如以下:   0:关机.不能将系统缺省执行级别设置为0,否则无法启动. 1:单用户模式.仅仅同意root用户对系统进行维护. 2:多用户模式.但不能使用NFS( ...

  9. llinux环境变量查看和修改

    查看全局环境变量:env 查看所有环境变量:set 删除一个变量:unset [变量名] #只针对当前会话 设置一个变量:name=songjiankang export name #将这个变量提升为 ...

  10. Spring+Struts+Hibernate 简介(转)

    http://blog.csdn.net/slnqnd/article/details/1772910/ Struts2.0 +Hibernate 3.2 +Spring 2.0 一.         ...