為了不要那麼多廢話所以也很簡單xd
之後會再改的更像vector~

測試的main

#include "ComboVector.hpp"
#include <iostream>
#include <cstdlib>
#include <vector>

int main()
{
    using namespace std;
    ComboVector<int> myCombo;
    int ary[5];
    for (int i=1;i<6;i++)
    {
        ary[i-1]=i;
        myCombo.Add(i);
    }
    cout << myCombo.CheckKeys(5,ary) << "\n";
    cout << myCombo.CheckLastKey(5) << "\n\n" ;
    myCombo.EatKeys(2);
    cout << myCombo.CheckKeys(3,ary) << "\n";
    cout << myCombo.CheckLastKey(5) << "\n" ;
    cout << myCombo.CheckLastKey(3) << "\n\n" ;
    cout << myCombo.IsNull() << "\n" ;
    myCombo.SetSize(2);
    cout << myCombo.CheckKeys(3,ary) << "\n";
    cout << myCombo.CheckLastKey(3) << "\n" ;
    cout << myCombo.IsNull() << "\n" ;
    system("pause");
    return 0;
}

亂碼補正

template <class _Ty,int size = 20>    // 會得到一個size-1大小的空間存combo
class ComboVector
{
public:
    ComboVector();
    ~ComboVector();
    bool Add(const _Ty data);    // 增加一個元素
    void Clear();            // 清除所有元素
    void SetSize(const int newSize);    // 砍掉原來的陣列新建一個新的大小的陣列
    void EatKeys(const int num);        // 吃掉不需要的n個鍵
    bool CheckKeys(const int num, const _Ty *datas) const;    // 判斷絕招陣列
    bool CheckLastKey(const _Ty datas) const;    // 判斷最後一個鍵
    bool IsNull() const;
private:
    _Ty* Keys;
    int head;
    bool nothing;
    int max;
};

ComboVector.hpp

#ifndef ComboVector_I69536f4dm12490dbe5c9mm6927_H #define ComboVector_I69536f4dm12490dbe5c9mm6927_H #include <memory.h> template <class _Ty,int size = 20> // ???唬??ize-1憭批??征??combo class ComboVector { public: ComboVector(); ~ComboVector(); bool Add(const _Ty data); // 憓?銝€??蝝? void Clear(); // 皜?€??蝝? void SetSize(const int newSize); // ??????撱箔???之撠???? void EatKeys(const int num); // ??銝?閬?n? bool CheckKeys(const int num, const _Ty *datas) const; // ?斗蝯???? bool CheckLastKey(const _Ty datas) const; // ?斗?€敺?? bool IsNull() const; private: _Ty* Keys; int head; bool nothing; int max; }; template <class _Ty,int size> bool ComboVector<_Ty, size>::IsNull() const { return nothing; } template <class _Ty,int size> ComboVector<_Ty, size>::ComboVector():head(0),nothing(true),max(size) { Keys = new _Ty[size]; memset(Keys,0,sizeof(_Ty)*size); } template <class _Ty,int size> bool ComboVector<_Ty, size>::Add(const _Ty data) { nothing = false; if (head<max) { Keys[head] = data; ++head; return true; } return false; } template <class _Ty,int size> void ComboVector<_Ty, size>::Clear() { head = 0; nothing = true; } template <class _Ty,int size> void ComboVector<_Ty, size>::SetSize( const int newSize ) { nothing = true; head = 0; max = newSize; delete[] Keys; Keys = new _Ty[newSize]; memset(Keys,0,sizeof(_Ty)*newSize); } template <class _Ty,int size> void ComboVector<_Ty, size>::EatKeys( const int num ) { if (head <= num) { nothing = true; head = 0; } else head -= num; } template <class _Ty,int size> bool ComboVector<_Ty, size>::CheckKeys( const int num, const _Ty *datas ) const { for (int i = 0;i < num;i++) { if (datas[i] != Keys[head-num+i]) return false; } return true; } template <class _Ty,int size> bool ComboVector<_Ty, size>::CheckLastKey( const _Ty data ) const { if (data != Keys[head-1]) return false; return true; } template <class _Ty,int size> ComboVector<_Ty, size>::~ComboVector() { delete[] Keys; } #endif // ComboVector_I69536f4dm12490dbe5c9mm6927_H
arrow
arrow
    全站熱搜

    讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()