简单介绍C++ 11新增加的8种新特性

今天看了一本英文书,简单做了翻译对于c++新特性经常用但却不知道辨别,虽然c++17都已经出来了!
一起学习吧!

主要:初始化,auto、nullptr等
//C++ 11 新特性
#include "stdafx.h"
#include <vector>
#include<list>
#include <set>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
 //第一个 书写格式
 list<int> list1;
 vector<list<int> >  listw; //之前的
 listw.push_back(list1);
 vector < list<int>> list2; //C++11 支持种写法
 list2.push_back(list1);
 //添加nullptr 代替NULL  0

 void f(int);
 void f(void *);

 f(0);
 f(NULL);  //有歧义不知道调用上面的哪一个函数
 f(nullptr); //这个解决了这个问题调用f(void *)

 //添加了一个auto 的数据类型,支持自定义转换数据类型
 auto i = 42;
 double f();
 auto d = f(); //就自动的变为double类型

 //auto i; //wrong 必须要初始化
 static auto vat = 0.21; //添加前缀也是准许的
 //auto 在递归是的应用

 vector<string> v;
 auto pos = v.begin(); //pos = vector<string>::interaator 是不是方便多了
 //在lambada 表达式中的应用

 auto L = [](int x)->bool{   //具体的使用方法可以学习lambada表达式的使用
  return x;
 };

 //c++11的初始化方式
 int valures[] {1, 2, 4};
 std::vector<int> v{ 2, 4, 56, 7, 78, 8, 9, 9 };
 std::vector<string> s{ "monday", "tuesday" };
 int x1(5.3);
 int x2 = 5.3;
 //int x3{ 5.3 };//ERROR 不被润许
 /// int x4 = {5.3}; error
 char c{ 7 };//ok
 //char c{ 999 }; //wrong out of char

 vector<int> v1{1, 3, 5, 6, 78, 8};
 //vector<int> v2{23,345,5,4.4}; wrong 不准许类型不一致



//对于循环for
 vector<int> vec;
 vec.push_back(1);
 vec.push_back(2);
 vec.push_back(3);
 //利用这种方式进行遍历替换了迭代器
 for(auto &delm: vec)
 {
  cout << delm << endl;
 }

 //第二种bian遍历
 int array[] = { 1, 3, 4, 56, 7, 78 };
 for(auto &x : array)
 {
  cout << x << endl;

 }
 //最重要的改变
 std::set<int> coll;
 coll.insert(1);
 coll.insert(move(1)); //注意这里,在这里创建对象不进行重新生成一次拷贝构造,而是掉用原来已有的!
//keyword contexpr

contexpr int squre(int)  //支持在编译时期被检测使用
 return x*x;{
}
 return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值