#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. oracle全文索引的创建和使用

    整理一下我所遇到过的有关全文索引的问题吧 一.设置词法分析器 Oracle实现全文检索,其机制其实很简单.即通过Oracle专利的词法分析器(lexer),将文章中所有的表意单元(Oracle 称为  ...

  2. 如何从MATLAB里面保存出分辨率高的图形

    MATLAB堪称科技工作者的倚天屠龙,其科学计算,简洁的编程风格,友好的图形界面等等,都使得它颇受欢迎.MATLAB作图相当简单,而且美观,但是,缺点是分辨率低,一直没有发现,直到最近一期刊编辑告诉我 ...

  3. 8个使用JavaScript展示图片解决方案

    1. JonDesign’s SmoothGallery 2.0 SmoothGallery demo 2. (E)2 Photo Gallery (E)2 Photo Gallery demo 3. ...

  4. Javascript里面的时间处理:将时间戳或时间对象转成字符串格式

    问题背景:想把一个时间直接转成字符串格式 通过查api发现有个toLocaleString(),根据本地时间格式,把 Date 对象转换为字符串 new Date().toLocaleString() ...

  5. hdoj5645DZY Loves Balls

    Problem Description DZY loves playing balls. He has n balls in a big box. On each ball there is an i ...

  6. URL转发

    原理:iframe 代码: <?php if($_SERVER['HTTP_HOST']=="i.mansions.com.cn"){ $html = <<< ...

  7. angular中$location读取url信息

    读取url信息: <html ng-app="myApp"> <head> <title>angularjs-demo</title> ...

  8. angularjs中的interval定时执行功能

    一个例子,用来显示当前实时时间,1秒钟刷新一次: <!DOCTYPE html> <html ng-app="myApp"> <head> &l ...

  9. LoadRunner录制:事务

    背景 LoadRunner 会对事务的性能指标进行记录. 添加事务也是为了在测试的时候统计这段脚本运行时用的时间等等,方便定位性能瓶颈. 一个事务可以包含一个请求,也可以包含多个请求.一般把完成一件事 ...

  10. 首先不谈C语言,我们先来谈谈编程工具

    系统环境: 推荐Windows xp sp1 软件: Tb2.0或3.0 推荐理由:最好的一个系统环境 系统环境: 推荐Windows xp/ Windows 7/ Windows 8.1/Windo ...