cap-7:C++IO(char_trait)

定义大小写不敏感的 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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柔弱胜刚强.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值