sort排序不区分大小写C语言,sort std :: list区分大小写的元素

不区分大小写的字符比较是棘手。这就是为什么它是一个好主意,做他们在一个区域,合理的方式:

struct char_iless

: public std::binary_function

{

std::locale loc;

char_iless(std::locale const & loc=std::locale()) : loc(loc)

{

}

bool operator()(char a, char b) const

{

return std::tolower(a, loc) < std::tolower(b, loc);

}

};

这是你如何使用这个类来比较两个字符:

char_iless('a', 'b', my_locale);

只需使用std::locale()作为my_locale如果你想使用默认设置的那个。

如果你可以使用Boost,那么在字符串算法库中有is_iless函子,它可以做同样的事情。

从字符比较字符串扩展,这是容易由于std::lexicographical_compare:

struct str_iless

: public std::binary_function<:string std::string bool>

{

std::locale loc;

str_iless(std::locale const & loc=std::locale()) : loc(loc)

{

}

bool operator()(std::string const & a, std::string const & b) const

{

return std::lexicographical_compare(

a.begin(), a.end(),

b.begin(), b.end(),

char_iless(loc)

);

}

};

现在,你拥有所有,它的要求来解决问题:

int main()

{

std::list<:string> list;

list.push_back("C");

list.push_back("a");

list.push_back("b");

// Sort using default locale

list.sort(str_iless());

// Sort using French locale

// (warning: this locale format string is MS specific)

std::locale loc("French_France.1252");

list.sort(str_iless(loc));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值