中文名通讯录转换成拼音排序后发现李、黎 姓的排序夹杂在一起,比如李三、黎三、李四。下面的排序就是将李姓排在一起,同音姓按照中文汉字来排序,让其不再混在一起。

#include <iostream>
#include <string>
#include <list>
#include <string.h>
#include <algorithm>

static const char trailingBytesForUTF8[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4,
        4, 5, 5, 5, 5 }; //检查包含的字节数

static bool compareFilterName(char c) {
    if (c == '-' || c == ' ' || c == '(' || c == ')') {
        return true;
    }
    return false;
}

static std::string trimName(const std::string& name) {
    std::string filter_name = name;
    if (filter_name.empty()) {
        return filter_name;
    }
    //remove_if()将所有应该移除的元素都移动到了容器尾部并返回一个分界的迭代器.
    //移除的所有元素仍然可以通过返回的迭代器访问到. 为了实际移除元素, 你必须对容器自行调用erase()以擦除需要移除的元素
    std::string::iterator new_end = std::remove_if(filter_name.begin(),
            filter_name.end(), compareFilterName);
    filter_name.erase(new_end, filter_name.end());

return filter_name;
}

测试的通讯录数据,包含通讯录人名以及人名转换后对应的拼音

struct Contact {
    std::string name;
    std::string name_pin;//拼音已经按照单个汉字转换,每个汉字拼音中间用空格隔开
};

//获取姓名中的姓

std::string getFirstName(const std::string& name) {
    unsigned char c = name.at(0);
    int len = trailingBytesForUTF8[c] + 1;
    std::string destination = name;
    if (name.size() >= len) {
        destination = name.substr(0, len);
    }
    //std::cout << "des =" << destination << std::endl;
    return destination;
}

//获取姓对应的拼音

std::string getFirstPingYin(const std::string& pinyin) {
    std::string des = pinyin;
    int pos = pinyin.find_first_of(" ");
    if (pos < pinyin.size()) {
        des = pinyin.substr(0, pos);
    }
    //std::cout << "des-pin =" << des << std::endl;
    return des;
}

int main() {
    std::list<Contact> listBooks;//测试通讯录数组
    Contact cc;
    cc.name = "李工";
    cc.name_pin = "li gong";
    listBooks.push_back(cc);

cc.name = "里三";
    cc.name_pin = "li san";
    listBooks.push_back(cc);

cc.name = "李网1";
    cc.name_pin = "li wang";
    listBooks.push_back(cc);

cc.name = "黎四1";
    cc.name_pin = "li si";
    listBooks.push_back(cc);

cc.name = "里四";
    cc.name_pin = "li si";
    listBooks.push_back(cc);

cc.name = "黎三";
    cc.name_pin = "li san";
    listBooks.push_back(cc);

cc.name = "李网";
    cc.name_pin = "li wang";
    listBooks.push_back(cc);

cc.name = "黎四";
    cc.name_pin = "li si";
    listBooks.push_back(cc);

cc.name = "李kk";
    cc.name_pin = "li wang";
    listBooks.push_back(cc);

std::list<Contact> desList;
    std::string prePin = "";//用于保存上一个通讯录姓名的姓拼音
    std::string preName = "";//用于保存上一个通讯录姓名的姓
    std::list<Contact>::iterator itor = listBooks.begin();
    int i = 0;
    while (itor != listBooks.end()) {

//姓为空即第一条通讯录数据直接加入目标数组中
        if (prePin.empty()) {
            std::cout << "prePin empty" << "push --name" << itor->name << std::endl;
            desList.push_back(*itor);
        } else {
            std::string curPin = getFirstPingYin(itor->name_pin);
            std::string curName = getFirstName(itor->name);
            std::cout << "fullname ="<<itor->name << "  curPin =" << curPin << "  curName =" << curName << std::endl;
            if ((curPin == prePin) && (curName != preName)) {//判断有同音的姓
                std::list<Contact>::iterator itorFind = itor;
                itorFind++;//last

//查找列表后面是否存在与添加目标数组一样的姓,如果有则先添加到目标数组中,并从源数组中删除
                while(itorFind != listBooks.end()){
                    std::string lastName = getFirstName(itorFind->name);
                    std::string lastPin = getFirstPingYin(itorFind->name_pin);
                    std::cout << "while --lastPin =" << lastPin << "  lastName =" << lastName << std::endl;
                    if(lastPin == curPin){
                        if(lastName == preName){

                            std::cout << "find same pre name and pin and push list" << "pushand error findItor" << itorFind->name <<std::endl;
                            desList.push_back(*itorFind);
                            itorFind = listBooks.erase(itorFind);
                        }else{
                            ++itorFind;
                        }
                    }else{
                        break;
                    }
                }//while
            }

            //将当前浏览的通讯录添加到目标数组中
            std::cout << "push cur itor" << "push --name" << itor->name << std::endl;
            desList.push_back(*itor);
        }
        prePin = getFirstPingYin(itor->name_pin);
        preName = getFirstName(itor->name);
        std::cout << "i = " << i++ << "  prePin =" << prePin << "  preName =" << preName << std::endl;
        ++itor;
    }
    for(auto it : desList){
        std::cout << "name =" << it.name << " pinyin = " << it.name_pin << std::endl;
    }
    return 1;
}

运行结果:

通讯录同音姓按照汉字排序相关推荐

  1. 关于iphone、QQ通讯录、飞聊联系人排序设计的思考

    目前,对于联系人的排序,如果不考虑对方的在线状态,一般都是按照音序排序的.所谓音序排序,也就是拼音字母的顺序:首先是按照整个拼音的首字母(26个字母从A~Z)的顺序排列,如果首字母相同,则依次按照声母 ...

  2. java 汉字拼音排序_Java汉字排序(2)按拼音排序

    1.前言 对于包含汉字的字符串来说,排序的方式主要有两种: 一种是拼音,一种是笔画. 本文就讲述如何实现按拼音排序的比较器(Comparator). 作者:Jeff 发表于:2007年12月21日 1 ...

  3. GB2312-80 所有汉字排序,拼音

    须知:所有这些汉字都是GB2312-80标准中说明的汉字. 下面的这些规则不一定适用其它标准中的汉字范围. // 各声母的汉字界限. 这是经过将GB2312-80标准中的所有汉字排序后,获取拼音后提取 ...

  4. Java数组中文排序_Java模块 -- 数组/集合中文汉字排序(支持生僻汉字)

    这里举例List集合 , 对list中的中文进行排序 , 按照中文拼音首字母. 支持生僻汉字的话 , 需要使用一个jar包 , 链接地址如下 传统的 : List list = new ArrayLi ...

  5. NSArray利用Cocoa框架进行汉字排序

    NSArray利用Cocoa框架进行汉字排序 NSArray利用Cocoa框架进行汉字排序 在NSString有一个函数localizedCompare:,它的功能是通过自身与给定字符串的比較,返回一 ...

  6. oracle汉字排序

    最近项目过程中碰到对于数据进行排序按照升序排序需求.这需求貌似很简单,我没多想直接order by columnName.忽略了汉字排序的问题导致bug的出现. 业务要求,按照excel中排序出来的结 ...

  7. NSArray进行汉字排序

    因为NSArray并不直接支持对汉字的排序,这就要通过将汉字转换成拼音完成按A~Z的排序,这看起来是个头疼的问题,因为牵扯到汉字转为拼音,kmyhy给出一个较易实现的方法,获取汉字的首字的首字母,如将 ...

  8. oracle中文的升序降序,Oracle汉字排序

    Oracle汉字排序 使用一下SQL select * from T_0303003 order by stock_holder 进行选取数据时(stock_holder为存放中文的字段),结果发现两 ...

  9. oracle汉字默认排序,oracle对汉字排序

    汉字排序须综合考虑数据库字符集.NLS_SORT. 查看版本信息: select * from v$version; 查看数据库字符集: SELECT userenv('language') FROM ...

最新文章

  1. 解压zip_go|用Go写一个zip解压脚本
  2. 复旦邱锡鹏团队:Transformer最新综述!
  3. Tensorflow2.0安装(win10系统cpu版本)
  4. Python:Selenium 1:浏览器驱动
  5. 服务器配置PHP系统,php用什么系统做服务器配置
  6. 如何用命令行给mySQL添加用户
  7. 考研数学自整理,弥补知识漏洞(强化、冲刺)
  8. babel需要这样配置
  9. 西门子PLC S7-300紧凑型简介
  10. 纪念非线性光学诞生:Peter Franken和非线性光学
  11. 2017计算机考研统考,2017年计算机考研408统考真题及答案.pdf
  12. jQuery 键盘快捷键
  13. 阿里开女性创业者大会 马云:世界因为女性而美好
  14. 求曲线面积的原理(微积分入门)
  15. 免费服务器领取步骤(详细)
  16. 我爱赚钱吧:你也可以通过建网站赚钱的④
  17. MATLAB上用十一行代码实现深度学习…
  18. C - Neko does Maths 数论
  19. chrome 插件导出与导入
  20. Retrofit简介

热门文章

  1. 注册表计算机性能设置怎么应用,如何修改注册表优化电脑性能 修改注册表优化电脑性能方法...
  2. SPSS(三)统计分析方法体系----一幅图教你如何快速选用合适的模型
  3. ubuntu14.04 samba 无法访问。您可能没有权限使用网络资源
  4. 【物联网】16.物联网开发之传感器
  5. Preferences...
  6. html怎么连接mysql_html怎么连接数据库
  7. 转:拉近人与人之间的关系
  8. 混合开发jsBridge介绍
  9. (设计模式八)java设计模式之代理模式
  10. 火绒安全软件(安全防护软件)官方中文版V5.0.59.0 | 火绒安全软件好用吗