C++11新特性

/*#include <string>    
#include <iostream>  
#include <set>
using namespace std;
template <typename T>
class CPPCollection {
public:
    //迭代器类
    class Iterator{
    public:
        int index;//元素下标
        CPPCollection& outer;

        Iterator(CPPCollection &o, int i) :outer(o), index(i){}

        void operator++(){
            index++;
        }
        friend Iterator operator +(Iterator & a, int s){
            a.index += s ;
            return a;
        }
        T operator*() const{
            return outer.str[index];
        }
        bool operator!=(Iterator i){
            return i.index != index;
        }
    };

public:
    CPPCollection(){
        T strTemp[10] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
        int i = 0;
        for (auto strIt : strTemp)
            str[i++] = strIt;
    }

    Iterator begin(){
        return Iterator(*this, 0);
    }
    Iterator end(){
        return Iterator(*this, 10);
    }

private:
    T str[10];
};
int main(){
    CPPCollection<string> coll;
    //CPPCollection<string>::Iterator a = coll.begin() + 2;
    CPPCollection<string>::Iterator a(coll,2);
    for (; a != coll.end(); a++){
        cout << *a << " ";
    }
    cout << endl;
}*/

#include <iostream>
#include <list>
#include <map>
#include <algorithm>
#include <hash_map>
#include <unordered_map>

using namespace std;
int main(){
    /*list<int> c;
    c.insert(c.begin(),1);
    c.insert(c.end(),2);
    c.insert(c.end(),3);
    c.insert(c.begin(), 4);
    for (auto i : c){
        cout << i << " ";
    }
    cout << endl;
    c.remove_if([](int i){return i >= 3; });
    cout << endl;
    for (auto i : c){
        cout << i << " ";
    }
    cout << endl;
    */  
}

C++ 自定义迭代器,以及list相关的函数…只有list有remove, remove_if,其他的都是erase

#include <iostream>
#include <list>
#include <set>
#include <algorithm>
#include <string>
#include <unordered_map>
using namespace std;
template <typename T>
class A{
public:
    template <typename... Args>
    static T* Instance(Args &&... args){
        if (instance == nullptr){
            instance = new T(std::forward<Args>(args)...); //以此实现完美转发
        }
        return instance;
    }
    static void DestoryInstance(){
        delete instance;
        instance = nullptr;
    }
    static T* getInstance(){
        if (!instance)
            throw std::logic_error("instance must be initilized");
        return instance;
    }
private:
    static T*instance;
    A();
    virtual ~A(void);
    A(const A &);
    A&operator = (const A&);
};
template <class T> T* A<T>::instance = nullptr;
class Test{
public:
    Test(string s) :str(s){}
    void fun(){
        cout << str << endl;
    }
private:
    string str;
};
int main(){
    string str = "chengyang";
    A<Test>::Instance(str);
    A<B>::Instance(2,3,4);
    A<Test>::getInstance()->fun();
}

利用C++11特性完成的单利模式,可以接收任意类型的类,以及类的任意构造函数参数(只要该类中有相应的构造函数)

#include <iostream>
#include <list>
#include <set>
#include <algorithm>
#include <string>
#include <unordered_map>
using namespace std;

template <class Function, class ...Args>
inline auto FuncWrapper(Function &&f, Args&&... args) ->decltype( f(std::forward<Args>(args)...)){
    return f(forward<Args>(args)...);
}
void test0(){
    cout<<"void"<<endl;
}
int test1(){
    return 1;
}
int test2(int x){
    return x;
}
string test3(string a, string b){
    return a+b;
}
int main(){
    FuncWrapper(test0);
    cout<<FuncWrapper(test1)<<endl;
    cout<<FuncWrapper(test2,3)<<endl;
    cout<<FuncWrapper(test3,"cheng","yang")<<endl;
}

一个简单的函数包装器.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值