2013年3月7日 星期四

lower_bound, upper_bound and sorted vector

#include <vcl.h>
#pragma hdrstop
#include <vector>
#include <string>
#include <iostream>
using namespace std;
typedef struct column_desc
{
    const char* Token;
    const char* Comment;
    int value;
}COLUMN_DESC;
//---------------------------------------------------------------------------
class TokenCompare { // 用於比較的類
    public:
    bool operator()(const COLUMN_DESC& lhs, // 用於排序的比較函數
        const COLUMN_DESC& rhs) const {
        return keyLess(lhs.Token, rhs.Token); // keyLess在下麵
    }

    bool operator()(const COLUMN_DESC& lhs, // 用於查找的比較函數
        const std::string& k) const // (形式1)
    {
        return keyLess(lhs.Token, k);
    }

    bool operator()(const std::string& k, // 用於查找的比較函數
        const COLUMN_DESC& rhs) const // (形式2)
    {
        return keyLess(k, rhs.Token);
    }


    private:
    bool keyLess(const std::string& k1, // “真的”
        const std::string& k2) const // 比較函數
    {
        return k1 < k2;
    }
};

#pragma argsused
int main(int argc, char* argv[])
{
    COLUMN_DESC column[]={
    { "sdfg" , "sdfsdfsdf" , 9},
    { "abcd","abcdabcd" ,1},
    { "aeecd","eeecdeeecd" ,3},
    { "aeecd","eee1cdeeecd" ,5},
    { "aeecd","eee2cdeeecd" ,7},
    { "aaaa","aaaaaaaa" ,13},
    { "efef","efefefefefef" ,11},
    { "0000","dfdfdfdfdf" ,1},
    };
    std::vector<COLUMN_DESC> vvv;
    unsigned int colsz = (sizeof(column) / sizeof(column[0]));
    for ( int ii = 0 ; ii < colsz ; ii++)
    {
        std::vector<COLUMN_DESC>::iterator i = std::lower_bound(vvv.begin(), vvv.end(), column[ii],TokenCompare()); // 在次通過lower_bound查找,
        vvv.insert(i,column[ii]);
    }
    std::vector<COLUMN_DESC>::iterator itr ;
    for ( itr=vvv.begin() ; itr != vvv.end(); ++itr)
    {
        std::cout << itr->Token << "\t" << itr->Comment << "\t" << itr->value << std::endl;
    }
    return 0;
}


RESULT:
0000 dfdfdfdfdf 1
aaaa aaaaaaaa 13
abcd abcdabcd 1
aeecd eee2cdeeecd 3
aeecd eee1cdeeecd 5
aeecd eeecdeeecd 7
efef efefefefefef 11
sdfg sdfsdfsdf 9



int main(int argc, char* argv[])
{
    COLUMN_DESC key[]={
     { "aeecd","eeecde2eecd" ,3},
    };
    COLUMN_DESC column[]={
     { "sdfg" , "sdfsdfsdf" , 9},
     { "abcd","abcdabcd" ,1},
     { "aeecd","eeecdeeecd" ,3},
     { "aeecd","eee1cdeeecd" ,5},
     { "aeecd","eee2cdeeecd" ,7},
     { "aaaa","aaaaaaaa" ,13},
     { "efef","efefefefefef" ,11},
     { "0000","dfdfdfdfdf" ,1},
    };
    std::vector<COLUMN_DESC> vvv;
    unsigned int colsz = (sizeof(column) / sizeof(column[0]));
    for ( int ii = 0 ; ii < colsz ; ii++)
    {
        std::vector<COLUMN_DESC>::iterator i = std::lower_bound(vvv.begin(), vvv.end(), column[ii],TokenCompare()); // 在次通過lower_bound查找,
        if(i != vvv.end() && !strcmp(i->Token ,column[ii].Token))
        {
            continue;
        }
        vvv.insert(i,column[ii]);
    }
    std::vector<COLUMN_DESC>::iterator itr ;
    for ( itr=vvv.begin() ; itr != vvv.end(); ++itr)
    {
         std::cout << itr->Token << "\t" << itr->Comment << "\t" << itr->value << std::endl;
    }
    std::vector<COLUMN_DESC>::iterator itr_v;
    itr_v = std::lower_bound(vvv.begin(), vvv.end(),key[0], TokenCompare());
    if(itr_v != vvv.end())
    {
         std::cout << "Found:\t"<< itr_v->Token << "\t" << itr_v->Comment << "\t" << itr_v->value << std::endl;
    }
    return 0;
}

0000 dfdfdfdfdf 1
aaaa aaaaaaaa 13
abcd abcdabcd 1
aeecd eeecdeeecd 3
efef efefefefefef 11
sdfg sdfsdfsdf 9
Found: aeecd eeecdeeecd 3



int main(int argc, char* argv[])
{
    COLUMN_DESC key[]={
     { "aeecd","eeecde2eecd" ,3},
    };
    COLUMN_DESC column[]={
     { "sdfg" , "sdfsdfsdf" , 9},
     { "abcd","abcdabcd" ,1},
     { "aeecd","eeecdeeecd" ,3},
     { "aeecd","eeecdeeecd" ,5},
     { "aeecd","1eeecdeeecd" ,7},
     { "aaaa","aaaaaaaa" ,13},
     { "efef","efefefefefef" ,11},
     { "0000","dfdfdfdfdf" ,1},
    };
    std::vector<COLUMN_DESC> vvv;
    unsigned int colsz = (sizeof(column) / sizeof(column[0]));
    for ( int ii = 0 ; ii < colsz ; ii++)
    {
        std::vector<COLUMN_DESC>::iterator i = std::upper_bound(vvv.begin(), vvv.end(), column[ii],TokenCompare()); // 在次通過lower_bound查找,
//        if(i != vvv.end() && !strcmp(i->Token ,column[ii].Token))
//        {
//            continue;
//        }
        vvv.insert(i,column[ii]);
    }
    std::vector<COLUMN_DESC>::iterator itr ;
    for ( itr=vvv.begin() ; itr != vvv.end(); ++itr)
    {
         std::cout << itr->Token << "\t" << itr->Comment << "\t" << itr->value << std::endl;
    }
    std::vector<COLUMN_DESC>::iterator itr_v;
    itr_v = std::upper_bound(vvv.begin(), vvv.end(),key[0], TokenCompare());
    if(itr_v != vvv.end())
    {
         std::cout << "Found:\t"<< itr_v->Token << "\t" << itr_v->Comment << "\t" << itr_v->value << std::endl;
    }
    return 0;
}

0000 dfdfdfdfdf 1
aaaa aaaaaaaa 13
abcd abcdabcd 1
aeecd eeecdeeecd 3
aeecd eee1cdeeecd 5
aeecd eee2cdeeecd 7
efef efefefefefef 11
sdfg sdfsdfsdf 9
Found: efef efefefefefef 11



int main(int argc, char* argv[])
{
    COLUMN_DESC key[]={
     { "aeecd","eeecde2eecd" ,3},
    };
    COLUMN_DESC column[]={
     { "sdfg" , "sdfsdfsdf" , 9},
     { "abcd","abcdabcd" ,1},
     { "aeecd","eeecdeeecd" ,3},
     { "aeecd","eee1cdeeecd" ,5},
     { "aeecd","eee2cdeeecd" ,7},
     { "aaaa","aaaaaaaa" ,13},
     { "efef","efefefefefef" ,11},
     { "0000","dfdfdfdfdf" ,1},
    };
    std::vector<COLUMN_DESC> vvv;
    unsigned int colsz = (sizeof(column) / sizeof(column[0]));
    for ( int ii = 0 ; ii < colsz ; ii++)
    {
        std::vector<COLUMN_DESC>::iterator i = std::upper_bound(vvv.begin(), vvv.end(), column[ii],TokenCompare()); // 在次通過lower_bound查找,
//        if(i != vvv.end() && !strcmp(i->Token ,column[ii].Token))
//        {
//            continue;
//        }
        vvv.insert(i,column[ii]);
    }
    std::vector<COLUMN_DESC>::iterator itr ;
    for ( itr=vvv.begin() ; itr != vvv.end(); ++itr)
    {
         std::cout << itr->Token << "\t" << itr->Comment << "\t" << itr->value << std::endl;
    }
    std::vector<COLUMN_DESC>::iterator itr_v;

    itr_v = std::upper_bound(vvv.begin(), vvv.end(),key[0], TokenCompare());
    if(itr_v != vvv.end())
    {
         std::cout << "Found:\t"<< itr_v->Token << "\t" << itr_v->Comment << "\t" << itr_v->value << std::endl;
    }
    pair < vector < COLUMN_DESC > ::iterator, vector < COLUMN_DESC > ::iterator > range = std::equal_range(vvv.begin(), vvv.end(),key[0],TokenCompare());
    std::cout << "Found_1:\t"<< range.first->Token << "\t" << range.first->Comment << "\t" << range.first->value << std::endl;
    std::cout << "Found_2:\t"<< range.second->Token << "\t" << range.second->Comment << "\t" << range.second->value << std::endl;
    return 0;
}

0000 dfdfdfdfdf 1
aaaa aaaaaaaa 13
abcd abcdabcd 1
aeecd eeecdeeecd 3
aeecd eee1cdeeecd 5
aeecd eee2cdeeecd 7
efef efefefefefef 11
sdfg sdfsdfsdf 9
Found: efef efefefefefef 11
Found_1: aeecd eeecdeeecd 3
Found_2: efef efefefefefef 11

沒有留言:

張貼留言

文章分類