#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
2013年3月7日 星期四
訂閱:
張貼留言 (Atom)
文章分類
- 爬山 (3)
- 參考文章 (3)
- 鳥事 (5)
- 報稅 (1)
- AIX (2)
- ajax (1)
- BCB (3)
- C/C++ (2)
- cloudera (3)
- DISK (1)
- ftp (1)
- Fuse (2)
- gdb (2)
- hadoop (13)
- hdfs (8)
- HPC (2)
- hypertable (12)
- iOS (1)
- iscsi (1)
- JAVA (2)
- KFS (5)
- kickstart (1)
- KVM (2)
- LAMP (2)
- linux (2)
- Lion (1)
- LVM (2)
- mapreduce (3)
- mpi (3)
- mpich2 (4)
- msgpack (2)
- mysql (2)
- nfs (1)
- openmp (2)
- OS (1)
- OSX (2)
- others (5)
- PBS (1)
- performance_tuning (3)
- php (3)
- phplist (3)
- programming (27)
- REST (2)
- RHCA (6)
- rhel (13)
- rhel6 (4)
- scp (1)
- shell_scripts (2)
- snowleopard (2)
- Solaris (6)
- ssh (1)
- syslog (1)
- t-442-1 (4)
- torque (1)
- ubuntu (2)
- VNC (1)
- watercolor (5)
- windows (1)
- yum (1)
沒有留言:
張貼留言