#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;
}

最新文章

  1. 创建docker私人仓库
  2. 一个简单的jsp+servlet实例,实现简单的登录
  3. html5移动Web开发实战
  4. subclipse安装后从svn资源库视图check out的资源无法创建server
  5. Postgresql命令行和数据库备份与恢复
  6. iOS 定位系统 知识
  7. json格式的字符串转为json对象遇到特殊字符问题解决
  8. WPF博客地址分享
  9. mysql数据库全局只读和会话只读问题解析
  10. iOS开发——屏幕适配篇&amp;Masonry详解
  11. 了解实时媒体的播放(RTP/RTCP 和 RTSP)
  12. php给一张图片加上水印效果
  13. virtual box + win7 + usb + share folder
  14. syslog系统日志、事件日志分析、EventLog Analyzer
  15. Active Directory 域服务(AD DS)
  16. leetcode-填充同一层的兄弟节点Ⅱ
  17. BZOJ4556 Tjoi2016&amp;Heoi2016 字符串【后缀自动机+倍增+线段树合并】
  18. Mysql内置功能《三》视图
  19. Python No module named pkg_resources
  20. 基础篇---memcache

热门文章

  1. AC-PC线(前联合-后联合线)
  2. 使用OllyDbg破解软件
  3. zendstudio采用xdebug调试,断点不停的解决
  4. 世界围棋人机大战、顶峰对决第一盘:围棋世界冠军Lee Sedol(李世石,围棋职业九段)对战Google DeepMind AlphaGo围棋程序
  5. Python学习(四)数据结构(概要)
  6. go语言基础之有参有返回值函数的使用
  7. Converting a fisheye image into a panoramic, spherical or perspective projection [转]
  8. 【Scroller】scrollTo scrollBy startScroll computeScroll 自定义ViewPage 简介 示例
  9. css hack 和问题
  10. java学习笔记12--异常处理