using namespace std;
class Runtime
{
public:
enum type{nocase,normal};
private:
type mode;
public:
Runtime(type m) :mode(m) {};
Runtime() :mode(normal) {};
static bool com(char c, char d);
bool operator ()(const string&x, const string&y);
};
bool Runtime::operator ()(const string&x, const string&y)
{
if (mode == nocase) return x < y;
else return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(),com);
}
bool Runtime::com(char c, char d)
{
return toupper(c) < toupper(d);
}
typedef multimap<string,string,Runtime > Inset;
void fill(Inset &str)
{
str.insert(make_pair("Sbs", "adabed"));
str.insert(make_pair("Ccs", "czcdas"));
str.insert(make_pair("SEs", "ghidn"));
str.insert(make_pair("SDS", "ginvn"));
str.insert(make_pair("CAs", "gocmad"));
str.insert(make_pair("crs", "gocmad"));
str.insert(make_pair("ads", "adcxcmad"));
str.insert(make_pair("bds", "asdzzad"));
}
void print(Inset &x)
{
Inset::iterator pos;
pos = x.begin();
while (pos != x.end())
{
cout << pos->first << ' ' << pos->second << endl;
pos++;
}
}
int main()
{
Inset str;
fill(str);
print(str);
cout << endl;
Runtime st(Runtime::nocase);
Inset st1(st);//multimap o(op);
fill(st1);
print(st1);
//multimap<string, string>::iterator str1;
system("pause");
return 0;
}
ads adcxcmad
bds asdzzad
CAs gocmad
Ccs czcdas
crs gocmad
Sbs adabed
SDS ginvn
SEs ghidn
CAs gocmad
Ccs czcdas
SDS ginvn
SEs ghidn
Sbs adabed
ads adcxcmad
bds asdzzad
crs gocmad
请按任意键继续. . .
lexicographical_compare 最后要求的是一个普通函数指针,而不是成员函数指针,所以要加static
static 可以使该函数全局化,在栈中或者堆中创建,即不用声明对象全局下能够被调用。