C++11新特性

#include <vector>
#include <string>
#include <list>
#include <bitset>
#include <functional>
#include <iostream>

using namespace std;

//C++11 语言新特性
//1 Template表达式内的空格
//  在两个template表达式的闭符之间放一个空格 的要求已经过时了:
vector<list<int> > v1; //OK in each C++ version
vector<list<int> > v2; //OK since C++11

//2 nullptr 和 std::nullptr_t
//  nullpttr可以取代0或者NULL
void ff(int) {}
void ff(void*) {}

ff(0);       //调用 ff(int)
ff(NULL);    //如果NULL==0调用ff(int),否则调用ff(void*)
ff(nullptr); //调用ff(void*)

//3 以auto完成类型自动推导
auto i = 42;  //i has type int
double f();
auto d = f(); //d has type double
//以auto声明的变量,其类型回根据其初始值被自动推导出来,所以必需初始化,

//3.1 以下例子是错误的.
auto i; //Error

//3.2 可以加限定符
static auto var = 0.889;

//3.3 长类型表达式,可以用auto,节省书写麻烦
vector<string> v;
auto pos = v.begin(); //pos has type vector<string>::iterator

//4 一致性初始化与初值列
int values[] {1, 2, 3};
vector<int> v{2, 3, 4, 5, 6, 11, 13, 17};
vector<string> cities{"Berlin", "New York" , "London" , "Barunschweig", "Cairo"};
std::complex<double> c{4.0, 5.0}; //等价于 complex<double> c(4.0, 5.0)

//4.1 初始值与默认值
int i;    //i 没有初始值
int j();  //j 的默认值为0 C++11新特性
int *p;   //p 没有初始值
int *q{}; //q 的默认值是nullptr C++11新特性

//4.2 窄化--->精度降低或造成数值变动,对大括号而言是不成立的
int x1(5.3);                  //OK 但是x1的值为5
int x2 = 5.3;                 //OK 但是x2的值为5
int x3{5.3};                  //Error 大括号不成立 C++11新特性
int x4 = {5.4};               //Error 大括号不成立 C++11新特性
char c1{7};                   //Ok 在char范围内
char c2{256};                 //Error 超出char的范围,不成立
vector<int> v1{1, 2, 3, 4};   //OK
vector<int> v1{1, 2, 3, 4.0}; //Error 不能把double转成int
//注:浮点型转int永远是窄化

//5 自定义类型初值列std::initializer_list<>, 初始化一列值
print({1, 2, 3, 4});

//5.1 当有"指明参数个数"和"指明一个初值列"的构造函数同时存在,带有初值列的构造函数胜出
class P
{
public:
    P(int, int);
    P(std::initializer_list<int>);
};
P p(1, 2);      //calls P::P(int, int)
P p{1, 2};      //calls P::P(std::initializer_list<int>)   C++11新特性
P p{1, 2, 4};   //calls P::P(std::initializer_list<int>)   C++11新特性
P p = {1, 4};   //calls P::P(std::initializer_list<int>)   C++11新特性

//5.2 explicit--多数值自动类型转换失效 即 初始化=语法进行
class P
{
public:
    P(int, int);
    explicit P(std::initializer_list<int>);
};

P x(88, 9);        //OK
P y{10, 20};       //OK
P z{20, 30, 40};   //OK
P v = {40, 50};    //OK
P w = {77, 3, 42}; //Eroor
//注:如果加了explicit的构造函数,不能接受初值列

//6 Rang-Based for循环
//C++11引入的一种崭新的for循环形式,语法如下
//for (decl : coll)
//{
//    statement
//}

//例1
for (int i : {2, 3, 4, 5, 6, 7, 19, 20})
{
    cout << i << endl;
}

//例2 将vector的每个元素elem剩于3
vector<double> vec;
for (auto& elem : vec)
{
    elem * 3;
}

//例子3
for (auto _pos=coll.begin(); _pos!=coll.end(); ++_pos)
{
    const auto& elem = *_pos;
    cout << elem << endl;
}

//例子4
int array[] = {1, 2, 3, 4, 5};

long sum = 0;
for (int x : array)
{
    sum += x;
}

//7 Move语义和Rvalue Reference
//C++11的一个重要的特性就是,支持move semantic(搬迁语义), 用来
//避免非必要拷贝(copy)和零时对象(temporary)
class X
{
    X (const X& lvalue);//copy constructor
    X (X&& rvalue);     //move constructor
};

//8 Lambda
//C++11引入了Lambda,允许inline函数的定义被用作一个参数,或是一个local对象
auto l = [] {cout << "hell lambad" << endl;}; //lambad

std::function<int(int, int)> returnLambad()
{
    return [] (int x, int y)
    {
        return x*y;
    };
}

long long int lli = 90;

int main()
{
    auto l = [] {cout << "hell lambad" << endl;}; //lambad
    l();
    cout << bitset<16>(8) << endl;

    auto lf = returnLambad();
    cout << lf(5, 8) << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark-puls

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值