vector stable_sort结合排序规则将牌按照大小和长短进行排序, 为了三带二的牌型等

<1>

struct JiShu
{
    int num;  //牌的数量
    int pkZhi; //牌值大小
    vector<int> arr; //对应的牌
};

class HelloWorld : public cocos2d::CCLayer
{
public:
    HelloWorld();
    ~HelloWorld();
    virtual bool init();
    static cocos2d::CCScene* scene();
    CREATE_FUNC(HelloWorld);
public:
    vector<int> _pkArr;  //存储牌
    void chuLiPk();  //处理牌,先按照值排序,再按照多少排序
};
<2>

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    //CCSize size = CCDirector::sharedDirector()->getWinSize();
   
    _pkArr.push_back(6);
    _pkArr.push_back(6);
    _pkArr.push_back(6);
    _pkArr.push_back(8);
    _pkArr.push_back(8);
    _pkArr.push_back(8);
    _pkArr.push_back(7);
    _pkArr.push_back(7);
    _pkArr.push_back(7);
    _pkArr.push_back(10);
    _pkArr.push_back(9);
    _pkArr.push_back(10);
    
    chuLiPk();
    return true;
}


bool isDaXiao(const JiShu& js1, const JiShu& js2)  //
{
    return js1.pkZhi < js2.pkZhi;
}

bool isShorter(const JiShu& js1, const JiShu& js2) //
{
    return js1.num > js2.num;
}


void HelloWorld::chuLiPk()
{
    sort(_pkArr.begin(), _pkArr.end());  //先将手牌从小到达排序
    
    vector<JiShu> vec;
    while(!_pkArr.empty())
    {
        JiShu js;
        int pk = _pkArr.at(0);
        _pkArr.erase(_pkArr.begin());
        
        //
        js.num = 1;
        js.pkZhi = pk;
        js.arr.push_back(pk);
        
        //找出与第一个元素相同的牌
        int i = 0;
        while(i < _pkArr.size())
        {
            int pk1 = _pkArr.at(i);
            if(pk1 == pk)
            {
                ++js.num;
                js.arr.push_back(pk1);
                _pkArr.erase(_pkArr.begin() + i);
            }
            else
               break;
        }
        vec.push_back(js);
    }
    
    stable_sort(vec.begin(), vec.end(), isDaXiao);  //先按照大小排序
    stable_sort(vec.begin(), vec.end(), isShorter); //再按照长短排序
    
    //进行排序处理后,恢复牌
    for(vector<JiShu>::iterator iter = vec.begin(); iter != vec.end(); iter++)
    {
        for(vector<int>::iterator iter1 = (*iter).arr.begin(); iter1 != (*iter).arr.end(); iter1++)
        {
            _pkArr.push_back((*iter).pkZhi);
        }
        
    }
    
    for(vector<int>::iterator iter = _pkArr.begin(); iter != _pkArr.end(); iter++)
    {
        CCLOG("====数值:%d", *iter);
    }
}

/*
Cocos2d: ====数值:6
Cocos2d: ====数值:6
Cocos2d: ====数值:6
Cocos2d: ====数值:7
Cocos2d: ====数值:7
Cocos2d: ====数值:7
Cocos2d: ====数值:8
Cocos2d: ====数值:8
Cocos2d: ====数值:8
Cocos2d: ====数值:10
Cocos2d: ====数值:10
Cocos2d: ====数值:9
*/

 
 

总结:

比如:应该出成333 44,而不是44 333;  应该是:666 777 888 10 10 9三飞,而不是别的组合。感觉很有意思!!!先按照大小排序的,后按照长短,因此最后排序后肯定是:长短为主,其次是大小。





你可能感兴趣的:(vector stable_sort结合排序规则将牌按照大小和长短进行排序, 为了三带二的牌型等)