EC3-35 通过mismatch或lexicographical比较实现简单的忽略大小写字符串比较

一个STL菜鸟最常问的问题是“我怎么使用STL来进行忽略大小写的字符串比较?",这里用mismatch和lexicographical_compare两种方法实现。
首先是mismatch:返回两个范围中第一个不同的位置。代码如下:

int ciCharCompare(char c1, char c2)
// 忽略大小写比较字符 类似strcmp(返回一个负数、零或正数)
{
	int Ic1 = tolower(static_cast<unsigned char>(c1));
	int Ic2 = tolower(static_cast<unsigned char>(c2));
	if (Ic1 < Ic2) 
		return -1;
	if (Ic1 > Ic2) 
		return 1;
	return 0;
}
int ciStringCompareImpl(const string& s1, const string& s2)
{
	typedef pair<string::const_iterator,string::const_iterator> PSCI; 
	//找到第一个忽视大小写仍不等的字符 ciCharCompare与布尔值的相等正好相反 用not2处理
	PSCI p = mismatch(s1.begin(), s1.end(), s2.begin(), not2(ptr_fun(ciCharCompare))); 
	if (p.first == s1.end()) 
	{
		if (p.second == s2.end()) 
			return 0;
		else 
			return -1;
	}
	return ciCharCompare(*p.first, *p.second);
}
int ciStringCompare(const string& s1, const string& s2)
//短的字符串在前
{
	if (s1.size() <= s2.size()) 
		return ciStringCompareImpl(s1, s2);
	else 
		return -ciStringCompareImpl(s2, s1);
}

lexicographical_compare相当于strcmp的泛型版本。strcmp只对字符数组起作用,lexicographical_compare对所有任何类型值的区间都起作用,并且可以传入自定义比较函数。实现:

bool ciCharLess(char c1, char c2)
{
	return tolower(static_cast<unsigned char>(c1)) < tolower(static_cast<unsigned char>(c2));
}
bool ciStringCompare(const string& s1, const string& s2)
//忽略大小写后字符串是否相等
{
	return lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ciCharLess);
}

另外,非标准库中可能有类似stricmp/strcmpi的忽略大小写的字符串比较函数,他们对长字符串运行起来一般比通用的算法mismatch和lexicographical_compare快得多。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值