Case-Insensitive

/* 

ci_string s( "AbCdE" ); 

// case insensitive
//
assert( s == "abcde" );
assert( s == "ABCDE" );

// still case-preserving, of course
//
assert( strcmp( s.c_str(), "AbCdE" ) == 0 );
assert( strcmp( s.c_str(), "abcde" ) != 0 );

// the char_traits defines how characters interact-and compare!
// the char_traits eq(), lt(), compare(), find(). 

*/

#include <iostream>
#include <string>

using namespace std;;


// just inherit all the other functions that we don't need to replace
struct ci_char_traits : public char_traits<char> 
{
	static bool eq(char c1, char c2)
    { return toupper(c1) == toupper(c2); }
	
	static bool lt(char c1, char c2)
    { return toupper(c1) <  toupper(c2); }
	
	static int compare(const char* s1, const char* s2, size_t n)
    { return memicmp( s1, s2, n ); }
	
	// if available on your platform, otherwise you can roll your own
	static const char* find(const char* s, int n, char a)
	{
		while(n-- > 0 && toupper(*s) != toupper(a))
		{
			++s;
		}
		return n >= 0 ? s : 0;
	}
};


// char_traits control the a string's characters
typedef basic_string<char, ci_char_traits> ci_string;

int main()
{
	ci_string	a("aaa");
	cout << a.c_str() << endl;  
	// typedef basic_ostream<char, char_traits<char> > ostream;

// 	string b;
// 	string c = a + b;
// 
// 	cout << b;
}
学习char_traits

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值