C++11部分特性总结

C++11在G++ 4.6上不支持,4.7则支持,但是需要编译的时候加上选项’-std=c++11’
即‘g++ -std=c++11 test.cpp -o test’

void func(int z){ cout << "int"; }
void func(int *z){ cout << "int*"; }

void sumf(int &x){ cout << x; }

class foo{
public:
    virtual int bar() final { cout << "foo:bar"; return 1; }

};
class foo2: public foo{
public:
     int bar() const  { cout << "foo2:bar"; return 1; }

};
     void main(void)
     {
        //auto       
         auto i = 0;
         vector<int> x;
         auto j = x.begin();
         //nullptr
         int * p = NULL;
         int * q = nullptr;
         cout << (p == q) << endl;       
        // in test, if func(int *)&func(int) both defined, use func(int)
        // if just define one of them,compile ok too.
        //but for func(nullptr), it only compile ok in func(int *);
         func(0);
         func(nullptr);
         func(NULL);
         // auto in for
         int ar[] = { 1, 2, 3 };
         for (auto p : ar)
         {
             cout << p << endl;
         }
         //lamda
         int sum = 0;
         for_each(ar, ar + 3, [&sum](int &x){sum += x; });
         cout << sum<<endl;

         for_each(ar, ar + 3, sumf);

         for (auto i : ar)
         {
             sum += i;
         }
         cout << sum;
         cout << "test lamda------------------" << endl;

         int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
         for_each(a, a + 9, [](int &x){x *= 2; });// x = x *2
         for_each(a, a + 9, [](int &x){cout << x << endl; });//cout x
         int sum2 = 0;       
         for_each(a, a + 9, [&](int &x){sum2 += x; });// get sum, sum is change bcz '[&]', it means all para outside are passed by ref.
         cout << sum2 << endl;
         for_each(a, a + 9, [](int x){x*=2; });// x is not changed, bcz '(int x)', x is passed by value.
         for_each(a, a + 9, [](int x){cout << x << endl; });
         // initial list
         cout << "test initial list-----------------" << endl;
         int arr2[3] = { 1, 2, 3 };
         int arr3[3] {1, 2, 3};
         for_each(arr3, arr3 + 3, [](int &x){cout << x << endl; });// 1 2 3

         int arr4[3] {1};
         for_each(arr4, arr4 + 3, [](int &x){cout << x << endl; });// 1 0 0 . if set it as 1 2 3 4 , will compile failed bxz too many initial paras(vs2013).

         // override final
         //'override' to indicate that the function want override its parent function, compiler? will check it.if it can't match override rules, compile failed.
         //'final' to indicate that we don't want the function to be override by its child class..if there is a child class want to override it ,compile failed.
         cout << "override final......................." << endl;
         foo *pfoo = new foo2;
         pfoo->bar();
         delete pfoo;
         cout << "begin,end................................." << endl;
         int arr5[] = { 1, 2, 3, 4, 5 };
         for_each(arr5, arr5 + 5, [](int x){cout << x << endl; });
         for_each(begin(arr5), end(arr5), [](int x){cout << x << endl; });//  'begin' , 'end' can be set as the first and last-next iterator of a container. and it ONLY can be set to a container.
         cout << "static_assert................................." << endl;
         auto  var = 0;
         static_assert(0<3, "aaaaaaaaaaaaaaaa");




         system("pause");
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值