我几年前的工作是在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