Learn C++11 one

// Learn C++ 11
//P1: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.begin(); itr!=v.end(); ++itr)
                m_vec.push_back(*itr);
        }
};
*/
//boVector v = { 0, 2, 3, 4 };
//boVector v{ 0,2,3,4};//effectively the same

/*
*2.Uniform Initialization
*/
//C++03
class dog //Aggregate class or struct
{
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" };
/*
Uniform Initialization Search order
1.Initializer_list constructor
2.Regular constructor that takes the appropriate parameters
3.Aggregate initializer.
 */
/*
example:
dog d1{3};
class dog
{
    public:
        int age; //3rd choice
    dog(int a)
    {
        age = a; //2nd choice
    }
    dog(const initializer_list<int>& vec)
    {
        age = *(vec.begin());
    }
}
 */
/*
3.auto type
std::vector<int> vec = {1,2,3,4};
//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
//IDE becomes more important
 */

//4.foreach
//c++ 03
for(vector<int>::iterator itr = v.begin(); itr != v.end(); ++itr)
    cout << (*itr); 
//c++11:
for(auto i:v)
    cout << i;// works on any class that has begin() and end() readonly access
for(auto& i:v)
    i++;    //changes the values in v
//5.nullptr to replace NULL in c++ 03
void foo(int i) { }
void foo(char* pc) { }
int main()
{
    foo(NULL);//ambiguity
    //C++11:
    foo(nullptr);//call foo(char*)
}
//6.enum class
//c++03
enum apple {green_a, red_a};
enum orange { big_o,small_o};
apple a = green_a;
orange o = big_o;
if(a == 0)
    cout << "green";
else
    cout << "not the same";


//C++11
enum class apple{green,red};
enum class orange{big,small};
apple a = apple::green;
orange o = orange::big;
if (a == o)
	cout << "yes";
else
	cout << "no";
//compile fails because we haven't define == (apple,orange)
//7.static_assert
//run-time assert
assert(myPointer != NULL);
//Compile time assert(C++11)
static_assert(sizeof(int) == 4);
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 functions;
*/

//C++ 11:
class dog
{
	int age = 9;
public:
	dog() {}
	dog(int a) :dog() { doOtherThings(); }
};
//Limitation:dog() has to be called first.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值