C++11 (一)

统一的初始化方法和基于范围的for循环

#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;

struct A{
    int i,j;
    A(int m,int n):i(m),j(n){}

};

int main()
{
    //统一的初始化方式
    int arr[3]{1,2,3};
    vector<int> iv{1,2,3};
    map<int,string> mp{{1,"a"},{2,"b"}};
    string str{"Hello World"};
    int *p=new int[20]{1,2,3};

    //基于范围的 for循环
    for(int &e :arr)
        e*=10;
    for(int e :arr)
        cout<<e<<", ";
    cout<<endl;

    for(int &e :iv)
        e*=10;
    for(int e :iv)
        cout<<e<<", ";
    cout<<endl;


    A *pa=new A{3,7};
    cout<<pa->i<<" "<<pa->j<<endl;

    delete []p;
    delete pa;
    return 0;
}

auto 关键字

#include<iostream>
#include<map>
using namespace std;

class A{
public :
    int var;
    A(){var=-1;}
};
A operator +(int n,const A&a){
    return a;
}


template<class T1,class T2>
auto add(T1 x,T2 y)-> decltype(x+y){
    return x+y;
};
int main()
{
    //auto关键字
    auto i=100.2;
    auto p=new A();
    cout<<i<<"  "<<p->var<<endl;

    map<string,int,greater<string> >mp;

    mp.insert(make_pair("lss",1));
    mp.insert(make_pair("lws",2));

    // i的类型是  map<string,int,greater<string> >::iterator;
    for(auto i=mp.begin();i!=mp.end();++i)
        cout<<i->first<<","<<i->second<<endl;

    auto d=add(100,1.5);
    auto kk=add(100,A());

    cout<<d<<endl;
    cout<<kk.var<<endl;
    delete p;
    return 0;
}

auto msdn示例

//msdn 示例


#include <iostream>
#include <string>
#include <utility>
#include <iomanip>

using namespace std;

template<typename T1, typename T2>
auto Plus(T1&& t1, T2&& t2) ->
decltype(forward<T1>(t1) + forward<T2>(t2))
{
    return forward<T1>(t1) + forward<T2>(t2);
}

class X
{
    friend X operator+(const X& x1, const X& x2)
    {
        return X(x1.m_data + x2.m_data);
    }

public:
    X(int data) : m_data(data) {}
    int Dump() const { return m_data;}
private:
    int m_data;
};

int main() {
    // Integer
    int i = 4;
    cout <<
         "Plus(i, 9) = " <<
         Plus(i, 9) << endl;

    // Floating point
    float dx = 4.0;
    float dy = 9.5;
    cout <<
         setprecision(3) <<
         "Plus(dx, dy) = " <<
         Plus(dx, dy) << endl;

    // String
    string hello = "Hello, ";
    string world = "world!";
    cout << Plus(hello, world) << endl;

    // Custom type
    X x1(20);
    X x2(22);
    X x3 = Plus(x1, x2);
    cout <<
         "x3.Dump() = " <<
         x3.Dump() << endl;

}

auto decltype C++11 与C++14的不同

//C++11
 template<typename T, typename U>
auto myFunc(T&& t, U&& u) -> decltype (forward<T>(t) + forward<U>(u)) 
        { return forward<T>(t) + forward<U>(u); };

//C++14
template<typename T, typename U>
decltype(auto) myFunc(T&& t, U&& u) 
        { return forward<T>(t) + forward<U>(u); };

注意:

double tmp;
decltype((tmp))  t=tmp;//t is double &

参考:

郭炜老师CPP程序设计课程(慕课)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值