STL List 排序测试


// /opt/rh/devtoolset-2/root/usr/bin/g++ -lredis3m -lboost_regex -lboost_system -lboost_filesystem -lboost_log -lboost_thread -lboost_log_setup  -I/usr/local/include/boost -I/usr/local/include/redis3m main.cpp


#include<list>

#include <boost/lexical_cast.hpp>

#include <redis3m/redis3m.hpp>

#include <iostream>

#include <sys/time.h>


#define CUSTOMER_SCHED_PREFIX "test_customer_sched_"


typedef struct Element{

    int customerId;

    int countRef;

}Element;


class Compare {

public:

    bool operator()(const Element e1, const Element e2) const {

        return e1.countRef > e2.countRef;

    }

};


void set(redis3m::simple_pool::ptr_t& pool, int customerId, int value){

    redis3m::connection::ptr_t c = pool->get();

    c->run(redis3m::command("HSET")<<CUSTOMER_SCHED_PREFIX<<customerId<<value);

    pool->put(c);

}


int sel(redis3m::simple_pool::ptr_t& pool, std::vector<int>& vecCustomerId){

    int customerId = -1;

    if(0 == vecCustomerId.size()){

        return customerId;

    }

    redis3m::command cmd("HMGET");

    cmd<<CUSTOMER_SCHED_PREFIX;

    std::vector<int>::iterator iterVecCustomerId;

    for(iterVecCustomerId=vecCustomerId.begin(); iterVecCustomerId!=vecCustomerId.end(); iterVecCustomerId++){

        cmd<<*iterVecCustomerId;

    }

    redis3m::connection::ptr_t c = pool->get();

    redis3m::reply r = c->run(cmd);

    std::list<Element> lstCountRef;

    if (2 == r.type()){

        std::vector<redis3m::reply> vecReply = r.elements();

        if(0 == vecReply.size()){

            return customerId;

        }

        std::vector<redis3m::reply>::iterator iterVecReply;

        for(iterVecReply=vecReply.begin(), iterVecCustomerId=vecCustomerId.begin();\

            iterVecReply!=vecReply.end() && iterVecCustomerId!=vecCustomerId.end();\

            iterVecReply++, iterVecCustomerId++){

            Element e;

            e.customerId = 0;

            e.countRef = 0;

            if (iterVecReply->type() == 1){

                int value = boost::lexical_cast<int>(iterVecReply->str());

                Element e = {.customerId=*iterVecCustomerId, .countRef=value};

                lstCountRef.push_back(e);

                std::cout<<"customer "<<*iterVecCustomerId<<" count ref = "<<value<<std::endl;

            }else if(iterVecReply->type() == 4){

                Element e = {.customerId=*iterVecCustomerId, .countRef=0};

                lstCountRef.push_back(e);

                std::cout<<"customer "<<*iterVecCustomerId<<" count ref = "<<0<<std::endl;

            }else{

                Element e = {.customerId=*iterVecCustomerId, .countRef=0};

                lstCountRef.push_back(e);

                std::cout<<"customer "<<*iterVecCustomerId<<" count ref = "<<0<<std::endl;

                std::cout<<"customer "<<*iterVecCustomerId<<" something wrong.";

            }

        }

    }

    pool->put(c);

    lstCountRef.sort(Compare());

    Element frontElement = lstCountRef.front();

    Element endElement = lstCountRef.back();

    std::cout<<"front customer "<<frontElement.customerId<<" count ref = "<<frontElement.countRef<<std::endl;

    std::cout<<"end customer "<<endElement.customerId<<" count ref = "<<endElement.countRef<<std::endl;

    customerId = endElement.customerId;

    return customerId;

}


int main(int argc, char **argv){

    redis3m::simple_pool::ptr_t pool = redis3m::simple_pool::create("127.0.0.1");

    std::vector<int> vecCustomerId;

    set(pool, 0, 100);

    set(pool, 1, 200);

    set(pool, 2, 300);

    vecCustomerId.push_back(0);

    vecCustomerId.push_back(1);

    vecCustomerId.push_back(2);

    int customerId = sel(pool, vecCustomerId);

}


测试结果:

customer 0 count ref = 100

customer 1 count ref = 200

customer 2 count ref = 300

front customer 2 count ref = 300

end customer 0 count ref = 100


你可能感兴趣的:(STL List 排序测试)