#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. lua string介绍

    1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,...2. string库中所有的function都不会直接操作字符串,而是返回一个结果 string.byte(s ...

  2. SQL Server会话KILL不掉,一直处于KILLED /ROLLBACK状态情形浅析[转]

    本文将为您描述SQL Server会话KILL不掉,一直处于KILLED /ROLLBACK状态情形浅析,教程操作方法: 今天遇到一个很奇怪的情况,发现一个会话异常,这个会话只是在执行一个简单的存储过 ...

  3. datagridview 纵向 横向 合并单元格

    datagridview 单元格合并:纵向以及横向合并参考了csdn上不知哪位的代码,具体哪位找不到连接了. 纵向合并: /// <summary> /// 纵向合并,即合并数据项的值 / ...

  4. 操作系统概念学习笔记三 cpu调度算法

    一 基本概念 1 队列中的记录通常是进程的进程控制块. 2 CPU调度决策可在如下四种环境下发生 a 当一个进程从运行状态切换到等待状态 例如,I/O请求或调用wait以等待一个子进程的终止 b 党一 ...

  5. STL之set集合容器 【转】

    set集合容器实现了红黑树(Red-Black Tree)的平衡二叉检索树的的数据结构,在插入元素时,它会自动调整二叉树的排列,把该元素放到适当的位置,以确保每个子树根节点的键值大于左子树所有节点的键 ...

  6. Effective C++ 条款 50:了解new和delete的合理替换时机

    (一) 为什么有人想要替换operator new 和 operator delete呢?三个常见的理由: (1)用来检測运用上的错误. (2)为了强化效果. (3)为了收集使用上的统计数据. (二) ...

  7. 更简单更全的material design状态栏

    从实际使用须要出发,以最简单的方式实现了几种类型的MD状态栏. (重点在fitsSystemWindows的使用) 0,使用前提 Theme.AppCompat.Light.DarkActionBar ...

  8. 在第一段ionic示例的基础上增加底部导航

    demo2.html <!DOCTYPE html> <html ng-app="app"> <head> <meta charset=& ...

  9. ios开发之-继承的实现运用

    // // main.m // 继承 // // #import <Foundation/Foundation.h> #import "Animal.h" #impor ...

  10. QtGui.QGridLayout

    The most universal layout class is the grid layout. This layout divides the space into rows and colu ...