C++知识点-C++新特性01

我几年前的工作是在TI相机中开发算法,是用C语言开发,后来接触了C++,了解了C++语言惊艳的地方,我接下来会把C++语言简单全面的介绍一下。今天这篇博文从介绍C++11和C++03不同点的视角进行介绍。

Initializer list

//c++ 03 initializer list:
int arr[4] = {3,2,4,5};
vector<int> v;
v.push_back(3);
v.push_back(2);
v.push_back(4);
v.push_back(5);

//c++ 11 extended the support
vector<int> v = {3,4,1,9}; //calling initializer_list constructor
//All the relevant STL containers have been updated to accept initializer_list

//define your own initializer_list constructor:
#include <initializer_list>
class boVector{
    vector<int> m_vec;
public:
    boVector(const initializer_list<int> &v){
        for(initializer_list<int>::iterator itr = v.begine();itr != v.end();++itr){
            m_vec.push_back(*itr);
        }
    }
}
boVector v = {0,2,3,4};
boVector v{0,2,3,4}; //effectively the same

Uniform Initialization Search Order

//c++ 03
class dog{	//Aggregate class or strcut
public:
    int age;
    string name;
}
dog d1 = {5, "Henry"}; //Aggregate Initialization

//c++ 11 extended the scope of curly brace initialization
class dog{
public:
    dog(int age, string name){...};
};

dog d1 = {5, "Henry"};
dog d1{3};

class dog{
public:
    int age;	//3rd choice
    dog(int a){
        age = a;		//2nd choice
    }
    dog(const initializer_list<int>& vec){	//1st choice
        age = *(vec.begin());
    }
}

初始化列表选择的顺序

1 Initializer_list constructor

2 Regular constructor that takes the appropriete parameters

3 Aggregate initializer

 

auto type

std::vector<int> vec = {2,3,4,5};
//c++ 03
for(std::vector<int>::iterator it = vec.begin(); it!=vec.end();++it)
    m_vec.push_back(*it);

//c++ 11: use auto type
for(auto it = vec.begin(); it!=vec.end(); ++it)
    m_vec.push_back(*it);
auto a = 6; //a is a integer
auto b = 9.6; //b is a double
auto c = a; //c is an integer

foreach

//c++ 03
for(vector<int>::iterator itr = v.begin(); itr!=v.end();++itr)
    cout<<(*itr);
//c++ 11
for(int i:v){//works on any class that has begin() and end()
    cout<<i;	//readonly access
}
for(auto&i:v){
    i++;	//changes the values in v
}

nullptr

//to replace NULL in c++ 03

void foo(int){cout<<"foo_int"<<endl;}
void foo(char* pc){cout<<"foo_char*"<<endl;}
int main(){
    foo(NULL); //Ambiguity
    //c++ 11
    foo(nullptr); //call foo(char*)
}

enum class

//c++ 03
enum apple{green_a, red_a};
enum orange{big_o, samll_o};
apple a = green_a;
orange o = big_o;
if(a == o)
    cout<<"green apple and big orange are the same\n";
else 
    cout<<"green apple and big orange are not the same\n";

//c++ 11
enum class apple(green, red);
enum class orange{big, small};
apple a = apple::green;
orange o = orange::big;
if(a == o)
    cout<<"green apple and big orange are the same\n";
else 
    cout<<"green apple and big orange are not the same\n";

//compile fails because we have not define == (apple, orange)

static_assert

//run-time assert
assert(myPoint != NULL);
//compile time assert(c++ 11)
static_assert(sizeof(int) == 4);

delegating constructor

class dog{
public:
    dog() {...}
    dog(int a){dog();doOtherThings(a);}
};

//c++ 03
class dog{
    init(){...};
    public:
        dog(){init();}
        dog(int a){init(); doOtherThings();}
}
//cons:
//1 cumbersome code
//2 init() could be invoked by other funcitons

//c++ 11
class dog{
public:
    dog(){...}
    dog(int a):dog(){doOtherThings();}
}
//Linitation:dog() has to be called first

C++知识点-C++新特性02 是本篇博文的后续文章。

最后的话:

这篇文章发布在CSDN/蓝色的杯子, 没事多留言,让我们一起爱智求真吧.我的邮箱wisdomfriend@126.com

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值