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按中文拼音排序
随机推荐
- SSE图像算法优化系列九:灵活运用SIMD指令16倍提升Sobel边缘检测的速度(4000*3000的24位图像时间由480ms降低到30ms)。
这半年多时间,基本都在折腾一些基本的优化,有很多都是十几年前的技术了,从随大流的角度来考虑,研究这些东西在很多人看来是浪费时间了,即不能赚钱,也对工作能力提升无啥帮助.可我觉得人类所谓的幸福,可以分为 ...
- 自动化运维之PSSH
1.PSSH简介 PSSH提供OpenSSH和相关工具的并行版本.包括pssh,pscp,prsync,pnuke和pslurp.该项目包括psshlib,可以在自定义应用程序中使用. pssh是py ...
- 每天一个linux命令(17):whereis
1.命令简介 whereis (whereis) 命令用来定位指令的二进制程序.源代码文件和man手册页等相关文件的路径. whereis命令只能用于程序名的搜索,而且 ...
- 时间mysql
查询一天/今天: select * from table where to_days(column_time) = to_days(now()) select * from table where d ...
- codevs 2033 邮票
洛谷 P2725 邮票 Stamps codevs 2033 邮票 题目链接 http://codevs.cn/problem/2033/ https://www.luogu.org/problemn ...
- MATLAB 画柱状图(/直方图)修改横坐标名称并使其横着显示
使用MATLAB 画柱状图 ,即bar (x,y),其横坐标是默认 1.2.3.4.……的 % --v1 y1=[asum1,asum2,asum3,asum4,asum5,asum6,asum7,a ...
- html input 文本框 只能输入数字,包含输小数点.
<input type="text" id="source_tds" name="source_tds" value="&l ...
- STM32F105 PA9/OTG_FS_VBUS Issues
https://www.cnblogs.com/shangdawei/p/3264724.html F105 DFU模式下PA9引脚用来检测USB线缆,若电平在2.7~5v则认为插入usb设备(检测到 ...
- 论如何优雅的自定义ThreadPoolExecutor线程池
更好的markDown阅读体验可直接访问我的CSDN博客:https://blog.csdn.net/u012881584/article/details/85221635 前言 线程池想必大家也都用 ...
- mysql 按照月份自动创建表,以年和月为表明,动态生成。
需求:mysql5.5 数据库,想要根据月份自动创建表,每个月创建一张表,需要数据库自动创建,并根据当前年和月动态生成表名称. 解决办法:1 连接数据库工具为Navicat 2 首先创建存储过程, ...