【摘要】 定义大小写不敏感的 ci_trait,并用 ci_trait 构造 ci_string!
/* 定义大小写不敏感的 ci_trait,并用 ci_trait 构造 ci_string!*/#include<iostream>#include <string>using namespace std; struct ci_char_traits : public char_tr…
定义大小写不敏感的 ci_trait,并用 ci_trait 构造 ci_string!
/*
定义大小写不敏感的 ci_trait,并用 ci_trait 构造 ci_string!
*/
#include<iostream>
#include <string>
using namespace std;
struct ci_char_traits : public char_traits<char> {
// 覆盖 eq 函数
static bool eq(char c1, char c2) {
return toupper(c1) == toupper(c2);
}
// 覆盖 lt 函数
static bool lt(char c1, char c2) {
return toupper(c1) < toupper(c2);
}
// 覆盖 compare 函数
static int compare(const char *s1, const char *s2, size_t n) {
return memicmp(s1, s2, n);
}
// 覆盖 find 函数
static const char *find(const char *s, int n, char a) {
while (n-- >0 && toupper(*s) != toupper(a)) {
++s;
}
return n >= 0 ? s : 0;
}
};
int main()
{
typedef basic_string<char, ci_char_traits> ci_string;
ci_string s1("Apple");
ci_string s2("APPLE");
s1 == s2 ? cout << "equal!"<< endl : cout << "not equal!" << endl;
return 0;
}
© 版权声明文章版权归作者所有,未经允许请勿转载。THE END
喜欢就支持一下吧
相关推荐