C++ 中文拼音排序方法。
参考文档:http://zisxks.com/2013/10/25/sort-Chinese-characters-in-cpp/
采用locate.注意事项:排序的名字,如果出现某一个人,出现在顶上,可能是因为排序的名字前面是带有空格的,CString 类型的 可以采用 Trim() ,去掉前后的空格。
有个问题就是对于中文的多音字问题,这个函数排序的多音字,一般都会按照字母靠前的,但也有一部分是按照后字母靠后的音,猜想可能是多音字有多个枚举类型,挑的是里面的第一个,也有可能,多音字都有固定的拼音(查询资料未果)。
#include <iostream>
#include <string>
#include <locale>
#include <vector>
#include <algorithm>
using namespace std;
// Linux g++ locale 名称: "zh_CN.utf"
// VC2010 locale 名称: "Chinese"或者"Chinese_china"
#ifdef _MSC_VER
static const char *ZH_CN_LOCALE_STRING = "Chinese_china";
#else
static const char *ZH_CN_LOCALE_STRING = "zh_CN.utf8";
#endif
static const locale zh_CN_locale = locale(ZH_CN_LOCALE_STRING);
static const collate<char>& zh_CN_collate = use_facet<collate<char> >(zh_CN_locale);
bool zh_CN_less_than(const string &s1, const string &s2){
const char *pb1 = s1.data();
const char *pb2 = s2.data();
return (zh_CN_collate.compare(pb1, pb1+s1.size(), pb2, pb2+s2.size()) < );
}
int main(void){
vector<string> v;
v.push_back("啊");
v.push_back("阿");
v.push_back("第一");
v.push_back("第二");
v.push_back("第贰");
v.push_back("di");
v.push_back("第三");
v.push_back("liu");
v.push_back("第叁");
v.push_back("第四");
v.push_back("abc");
v.push_back("aa");
cout << "locale name: " << zh_CN_locale.name()<< endl;
sort(v.begin(), v.end(), zh_CN_less_than);
for(vector<string>::const_iterator p = v.begin(); p != v.end(); ++p){
cout << *p << endl;
}
return EXIT_SUCCESS;
}
对于单个的好友成员排序
module::UserInfoEntity user1,user2;
USES_CONVERSION;
user1.csName.Trim();
user2.csName.Trim();
const char* username1 = W2A(user1.csName);
const char* username2 = W2A(user2.csName);
return (UserListModule_Impl::zh_CN_collate.compare(username1, username1 + strlen(username1), username2, username2 + strlen(username2)) < );
}
对于群组的成员排序,一般都需要有群组的id ,所以需要重写排序的方法,使用时
应该用 ID 初始化一次 groupInfo.groupMemeberList.sort(CompareGroupMemberMethod(groupInfo.gId));
class CompareGroupMemberMethod
{
public:
CompareGroupMemberMethod(std::string gId) :m_comparegId(gId) {}; public: bool operator () (const std::string& code1, const std::string& code2)
{
module::GroupMemberInfo* pMemInfo1;
module::GroupMemberInfo* pMemInfo2;
pMemInfo1 = module::getGroupListModule()->getGMInfoBySId(m_comparegId, code1);
pMemInfo2 = module::getGroupListModule()->getGMInfoBySId(m_comparegId, code2); if (pMemInfo1 != NULL && pMemInfo2 != NULL)
{
CString memberName1 = util::stringToCString(pMemInfo1->name);
CString memberName2 = util::stringToCString(pMemInfo2->name);
memberName1.Trim();
memberName2.Trim(); USES_CONVERSION;
const char* mebName1 = W2A(memberName1);
const char* mebName2 = W2A(memberName2);
return (UserListModule_Impl::zh_CN_collate.compare(mebName1, mebName1 + strlen(mebName1), mebName2, mebName2 + strlen(mebName2)) < );
} return false;
}
C++ 中文拼音排序方法。的更多相关文章
- Android实现中文汉字笔划(笔画)、中文拼音排序、英文排序
发布时间:2018-11-16 技术:Android 概述 最近要做一个类似微信的,在登录界面选择国家地区的功能,微信有中文汉字笔画排序以及中文拼音排序等几种方式,如下所示: 简体中文 拼音排 ...
- JavaScript中文拼音排序函数
要对很多设备根据名称排序,找了找没有找到特别适合的,然后就自己写了一个根据中文拼音首字母排序的方法. github: https://github.com/haboll/sort.git
- sqlalchemy & python & datatables & javascript 中文拼音排序
近期有中文拼单排序需要,查询资料,mysql数据库有convert函数支持 select cname from channel order by convert(cname using gbk); # ...
- MySQL实现中文拼音排序
MySQL下新建一个表,默认采用utf8字符集,中文不能直接按照拼音进行排序. 例如以下语句: SELECT * FROM `tb_fixedassets` order by C_FANAME 得到的 ...
- MySQL按中文拼音排序
好多时候,我们希望查询出来的记录能够按照汉语拼音即英文的26个字母排序,但是utf字符集是外国人弄的,不是按照汉语拼音的顺序排列的,因此,我们需要将要排序的字段把编码设定为GBK或者BG2312再进行 ...
- sql按照中文拼音排序
select * from table order by convert(columnName using gbk) asc 注意:会导致全表扫描 建立冗余字段,插入数据时字段为convert(col ...
- Java中中文拼音的排序问题
最近做一个手机数据同步的应用开发,需要提供地址簿信息按照姓名的拼音次序进行排序.但仔细考察Java提供的Collator之后,发现其中文拼音排序存在严重的问题.Java提供Collator来支持不同语 ...
- ios 汉字字符串数组拼音排序
ios没有提供简单的汉字拼音排序方法,在网上看到了oc方法,这里写以下对应的swift方法 var stringCompareBlock: (String,String)->Bool = { ( ...
- 使mysql按中文字段排序
http://ourmysql.com/archives/391 测试后我发现,gbk不仅对字符内容是按拼音排序的,对数字也是一样,使用时需注意! 另外一篇文章: MySQL按中文拼音排序
随机推荐
- LSTM Networks
Understanding LSTM Networks Recurrent Neural Networks Humans don’t start their thinking from scratch ...
- iterm快捷键设置
如图所示,选择相应的动作和快捷键组合
- Spring Data JPA使用getOne方法报错:Method threw 'org.hibernate.LazyInitializationException' exception. Cannot evaluate
getOne是懒加载,需要增加这个配置: spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true,但这种方式不太友好,建议不要使用 ...
- mysql-linux定时备份mysql数据库
sh脚本 #!/bin/bash db_user="数据库用户名" db_passwd="数据库密码" db_name="数据库名" cd ...
- Structured Exception Handling
https://docs.microsoft.com/en-us/windows/desktop/Debug/structured-exception-handling An exception is ...
- Android TextView中链接(link)点击事件的截取
布局文件xml <TextView package com.jayce.testlink; import android.net.Uri; import android.os.Bundle; i ...
- 一步步教你轻松学支持向量机SVM算法之案例篇2
一步步教你轻松学支持向量机SVM算法之案例篇2 (白宁超 2018年10月22日10:09:07) 摘要:支持向量机即SVM(Support Vector Machine) ,是一种监督学习算法,属于 ...
- Java中Lambda表达式的使用(转)
https://www.cnblogs.com/franson-2016/p/5593080.html 简介(译者注:虽然看着很先进,其实Lambda表达式的本质只是一个"语法糖" ...
- mysqlpump 和 mysql_config_editor测试
The mysql_config_editor utility enables you to store authentication credentials in an obfuscated log ...
- 微信小程序tab切换,可滑动切换,导航栏跟随滚动实现
简介 看到今日头条小程序页面可以滑动切换,而且tab导航条也会跟着滚动,点击tab导航,页面滑动,切导航栏也会跟着滚动,就想着要怎么实现这个功能 像商城类商品类目如果做成左右滑动切换类目用户体验应该会 ...