c++ 中文转拼音的封装, char 类型 不支持 中文 已解决

        在日常业务中,需要进行中文转拼音的检索。已便实现对应的 模糊搜索。

 使用方法

	std::string res = "我是中国人";char* result = new char[res.length() + 1];for (int i = 0; i < res.length(); ++i){result[i] = res[i];}result[res.length()] = '\0';std::string str2(result);std::string p = m_pinyin->chineseConvertPinYinAllSpell(str2);qDebug() << "pinyin 9999999999999" << p;
void CCendPinyinSearch::insertSearchIndex(int index,QString name)
{// 检索索引QMap<int,QString> m_pSearchIndex;m_pSearchIndex.insert(index, name);
}void CCendPinyinSearch::getSeachIndexList(QString name)
{QMap<int, QString>::const_iterator i;for (i = m_pSearchIndex.constBegin(); i != m_pSearchIndex.constEnd(); ++i) {qDebug() << "123123777777777777777 index" << i.key() << ": " << i.value();if (i.value() == name){qDebug() << "123123123888888888888 index" << i.key() << ": " << i.value();}}
}//检查哈希表  查找
bool CCendPinyinSearch::isContainsAllCharactersHash(const std::string& mainString, const std::string& subString) {// 初始化哈希表  std::unordered_map<char, int> charCounts;for (char c : subString) {charCounts[c]++;}// 遍历主字符串并更新哈希表  for (char c : mainString) {if (charCounts.find(c) != charCounts.end()) {charCounts[c]--;if (charCounts[c] == 0) {// 如果字符的计数减到0,从哈希表中删除该字符  charCounts.erase(c);}}}// 检查哈希表  return charCounts.empty(); // 如果哈希表为空,说明所有字符都被找到了  
}// 排序算法 查找匹配
bool CCendPinyinSearch::isContainsAllCharacters(const std::string& mainString, const std::string& subString) {// 对两个字符串进行排序std::string sorted_main = mainString;std::string sorted_sub = subString;std::sort(sorted_main.begin(), sorted_main.end());std::sort(sorted_sub.begin(), sorted_sub.end());// 使用双指针法检查 sorted_sub 是否是 sorted_main 的子序列  int i = 0, j = 0;while (i < sorted_sub.length() && j < sorted_main.length()) {if (sorted_sub[i] == sorted_main[j]) {i++; // 移动到 sorted_sub 的下一个字符  }j++; // 移动到 sorted_main 的下一个字符  }// 如果 sorted_sub 的所有字符都在 sorted_main 中找到,则 sorted_sub 是 sorted_main 的子序列  return  sorted_sub.length() == i;
}std::string CCendPinyinSearch::chineseConvertPinYinAllSpell(const std::string & dest_chinese)
{static const int spell_value[] = { -20319, -20317, -20304, -20295,-20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036,-20032, -20026,-20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775,-19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728,-19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484,-19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263,-19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224,-19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996,-18977, -18961, -18952, -18783, -18774, -18773, -18763, -18756,-18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526,-18518, -18501, -18490, -18478, -18463, -18448, -18447, -18446,-18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183,-18181, -18012, -17997, -17988, -17970, -17964, -17961, -17950,-17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730,-17721, -17703, -17701, -17697, -17692, -17683, -17676, -17496,-17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202,-17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706,-16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459,-16452, -16448, -16433, -16429, -16427, -16423, -16419, -16412,-16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205,-16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959,-15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878,-15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640,-15631, -15625, -15454, -15448, -15436, -15435, -15419, -15416,-15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362,-15183, -15180, -15165, -15158, -15153, -15150, -15149, -15144,-15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117,-15110, -15109, -14941, -14937, -14933, -14930, -14929, -14928,-14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889,-14882, -14873, -14871, -14857, -14678, -14674, -14670, -14668,-14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399,-14384, -14379, -14368, -14355, -14353, -14345, -14170, -14159,-14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123,-14122, -14112, -14109, -14099, -14097, -14094, -14092, -14090,-14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905,-13896, -13894, -13878, -13870, -13859, -13847, -13831, -13658,-13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391,-13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,-13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095,-13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871,-12860, -12858, -12852, -12849, -12838, -12831, -12829, -12812,-12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346,-12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058,-12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604,-11589, -11536, -11358, -11340, -11339, -11324, -11303, -11097,-11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024,-11020, -11019, -11018, -11014, -10838, -10832, -10815, -10800,-10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331,-10329, -10328, -10322, -10315, -10309, -10307, -10296, -10281,-10274, -10270, -10262, -10260, -10256, -10254 };// 395个字符串,每个字符串长度不超过6 static const char spell_dict[396][7] = { "a", "ai", "an", "ang","ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi","bian", "biao","bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao","ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che", "chen","cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang","chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan", "cui","cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di","dian", "diao", "die", "ding", "diu", "dong", "dou", "du", "duan","dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei","fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang", "gao","ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai","guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang","hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua","huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian","jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan","jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken","keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui","kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei","leng", "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling","liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo","ma", "mai", "man", "mang", "mao", "me", "mei", "men", "meng", "mi","mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu","na", "nai", "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni","nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong","nu", "nv", "nuan", "nue", "nuo", "o", "ou", "pa", "pai", "pan","pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie","pin", "ping", "po", "pu", "qi", "qia", "qian", "qiang", "qiao","qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun","ran", "rang", "rao", "re", "ren", "reng", "ri", "rong", "rou","ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang","sao", "se", "sen", "seng", "sha", "shai", "shan", "shang", "shao","she", "shen", "sheng", "shi", "shou", "shu", "shua","shuai", "shuan", "shuang", "shui", "shun", "shuo", "si", "song","sou", "su", "suan", "sui", "sun", "suo", "ta", "tai","tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", "tie","ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo","wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi","xia", "xian", "xiang", "xiao", "xie", "xin", "xing","xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang","yao", "ye", "yi", "yin", "ying", "yo", "yong", "you","yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze","zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang","zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu","zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo","zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo" };std::string pinyin;const int length = dest_chinese.length();for (int j = 0, chrasc = 0; j < length;) {// 非汉字处理if (dest_chinese.at(j) >= 0 && dest_chinese.at(j) < 128) {pinyin += dest_chinese[j];// 偏移下标j++;continue;} // 汉字处理chrasc = dest_chinese[j] * 256 + dest_chinese[j + 1] + 256;if (chrasc > 0 && chrasc < 160) {// 非汉字pinyin += dest_chinese.at(j);// 偏移下标j++;}else {// 汉字for (int i = (sizeof(spell_value) / sizeof(spell_value[0]) - 1);i >= 0; --i) {// 查找字典if (spell_value[i] <= chrasc) {pinyin += spell_dict[i];break;}} // 偏移下标 (汉字双字节)j += 2;}}// for endreturn pinyin;
}

 测试的 拼音完全正确,谁能告诉我哪儿出错l

错误的拼音!!! 

 拼音 翻译的很到位,关键不认识!

正确的 拼音

		// 查询 数据拼音//std::string p = m_pPinyinSearch->chineseConvertPinYinAllSpell(searchName.toStdString());std::string p = m_pPinyinSearch->chineseConvertPinYinAllSpell(string((const char*)searchName.toLocal8Bit()));

 

string与QString之间的转换 

 1.fromStdString直接转换

string str;
QString qstr;//从QString 到 std::string
str = qstr.toStdString();//从std::string 到QString
qstr = QString::fromStdString(str);

 2.fromLocal8Bit(cstr.c_str())

std::string cstr;
QString qstring;//从std::string 到QString
qstring = QString(QString::fromLocal8Bit(cstr.c_str()));//从QString 到 std::string
cstr = string((const char *)qstring.toLocal8Bit());

参考 

C++ 解决string转为char*中文乱码问题_char* 中文乱码-CSDN博客

突破编程_C++_字符串算法(判断字符串是否包含)_c++判断字符串是否含有某字符串-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/813103.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++第三阶段】vector容器

以下内容仅为当前认识&#xff0c;可能有不足之处&#xff0c;欢迎讨论&#xff01; 文章目录 构造函数赋值操作容量和大小插入和删除数据存取互换容器预留空间 构造函数 vector函数是常用数据结构&#xff0c;见于刷题网站。 它是单端数组&#xff0c;与普通数组区别在于可以…

【Linux】基础IO----理解缓冲区

> 作者&#xff1a;დ旧言~ > 座右铭&#xff1a;松树千年终是朽&#xff0c;槿花一日自为荣。 > 目标&#xff1a;理解缓冲区 > 毒鸡汤&#xff1a;有些事情&#xff0c;总是不明白&#xff0c;所以我不会坚持。早安! > 专栏选自&#xff1a;Linux初阶 > 望…

Aritest+python+Jenkins解放双手iOS/Android自动化

ARITest、Python 和 Jenkins 可以结合在一起创建一个自动化测试解决方案&#xff0c;实现持续集成和持续测试的目标。以下是三者如何协同工作的基本概念&#xff1a; 1. **ARITest**&#xff1a; ARITest 是一款功能全面的自动化测试工具&#xff0c;提供 UI 自动化、接口自…

独立服务器,数据安全保障:WorkPlus即时通讯为企业信息安全把关

随着手机普及率的不断增加&#xff0c;即时通讯已经成为了企业内部沟通的重要方式。WorkPlus作为一家领先的企业级即时通讯服务商&#xff0c;提供了全方位的即时通讯解决方案&#xff0c;助力企业实现沟通无障碍、协作高效率的目标。 WorkPlus提供了多样化的即时通讯功能&…

轻松解锁销售利润!使用淘宝商品关键词搜索API接口

随着电商行业的快速发展&#xff0c;如何提高销售利润成为了卖家们的关注焦点。淘宝商品关键词搜索API接口提供了一种高效的方式来帮助卖家们找到最适合的商品关键词&#xff0c;提高商品曝光率和销售量。联讯数据将全面介绍淘宝商品关键词搜索API接口的用途、优势以及如何使用…

蓝桥杯模拟赛题——魔法失灵了——toRefs()

目标 找到 index.html 中 TODO 部分&#xff0c;正确修复代码使 data 对象恢复响应式特性&#xff0c;即点击页面上的 - 与 按钮可以改变 value 的值。正确实现后效果如下&#xff1a; 题解 value是reactive 利用toRefs; toRefs() 函数可以将 reactive() 创建出来的响应式对象…

数据结构之单链表的相关知识点及应用

找往期文章包括但不限于本期文章中不懂的知识点&#xff1a; 个人主页&#xff1a;我要学编程(ಥ_ಥ)-CSDN博客 所属专栏&#xff1a;数据结构 目录 链表的概念及结构 链表与顺序表的区别与优劣势 链表的分类 单链表的实现 单链表中增加节点 单链表中尾插数据 打印单链…

《零基础入行IT:步步为营的转型攻略与实践策略》

在信息化社会&#xff0c;IT行业以其强劲的发展势头、广阔的就业前景和丰厚的薪酬待遇&#xff0c;吸引了无数希望转行或初入职场人士的目光。然而&#xff0c;对于毫无相关背景知识的人来说&#xff0c;如何成功叩开IT行业的大门&#xff0c;似乎是一项颇具挑战性的任务。本文…

visual studio 2017开发QT框架程序

1. 配置开发环境 首先创建项目 进入到项目后&#xff0c;右键点击项目点击属性&#xff0c;配置如下&#xff1a;

Window11的CUDA 和CUDNN的安装

确定自己电脑的英伟达驱动版本 打开英伟达的控制面板:确定自己的版本,安装的CUDA和CUDNN不能超过这个版本 下载CUDA (我个人安装的是CUDA12)CUDA12的下载网址 下载网址 下载到安装包之后,直接点击就可以完成安装: 完成之后会有个文件夹:

linux上使用redis-cli登录以及操作redis

1、找到redis-cli 2、输入redis-cli回车 3、登录redis 输入auth密码 4、登录成功

stm32 之SPI通信协议

本文为大家介绍 SPI 通信协议的基础知识。 文章目录 前言一、SPI协议的概念二、SPI总线架构三、SPI通讯时序1. 起始&#xff0c;停止 信号2.CPOL&#xff08;时钟极性&#xff09;/CPHA&#xff08;时钟相位&#xff09; 四&#xff0c; I2C 总线 和SPI 总线比较相同点&#xf…

Elastic 线下 Meetup 将于 2024 年 4 月 27 号在重庆举办

2024 Elastic Meetup 重庆站活动&#xff0c;由 Elastic、新智锦绣联合举办&#xff0c;现诚邀广大技术爱好者及开发者参加。 活动时间 2024年4月27日 13:30-18:00 活动地点 中国重庆 沙坪坝区学城大道62-1号研发楼一期b3栋1楼(瑞幸咖啡旁&#xff09; 活动流程 14:00-14:50…

卫星图像10个开源数据集资源汇总

文章目录 1、UC Merced Land-Use 2、Indian Pines 3、KSC 4、Washington DC 5、BigEarthNet 6、水体卫星图像的图像 7、城市航拍图像分割数据集 8、游泳池和汽车卫星图像检测 9、人工月球景观数据集 10、马萨诸塞州道路数据集 1、UC Merced Land-Use 数据集下载地址&am…

Windows系统本地部署Jupyter Notebook并实现公网访问编辑笔记

文章目录 1.前言2.Jupyter Notebook的安装2.1 Jupyter Notebook下载安装2.2 Jupyter Notebook的配置2.3 Cpolar下载安装 3.Cpolar端口设置3.1 Cpolar云端设置3.2.Cpolar本地设置 4.公网访问测试5.结语 1.前言 在数据分析工作中&#xff0c;使用最多的无疑就是各种函数、图表、…

鱼眼摄像头畸变校正方法概述

1. 摘要 鱼眼摄像头以其独特的广阔视场和其他特点&#xff0c;在各个领域得到了广泛应用。然而&#xff0c;与针孔相机相比&#xff0c;鱼眼摄像头存在显著的畸变&#xff0c;导致拍摄的图像失畸变严重。鱼眼摄像头畸变是数字图像处理中常见的问题&#xff0c;需要有效的校正技…

淘宝/天猫获得淘宝商品评论 API 返回值说明,item_review-获得淘宝商品评论

淘宝/天猫的API通常不对公众开放&#xff0c;特别是涉及到具体商品评论的API。商家或开发者需要使用淘宝/天猫开放平台提供的API进行开发&#xff0c;并且需要遵循一定的申请流程和权限限制。 对于“item_review-获得淘宝商品评论”的API返回值说明&#xff0c;我无法提供具体…

CSS3 立体 3D 变换

个人主页&#xff1a;学习前端的小z 个人专栏&#xff1a;HTML5和CSS3悦读 本专栏旨在分享记录每日学习的前端知识和学习笔记的归纳总结&#xff0c;欢迎大家在评论区交流讨论&#xff01; 文章目录 ✍CSS3 立体 3D 变换&#x1f48e;1 坐标轴&#x1f48e;2 perspective 透视视…

mybatis-puls-配置日志

#日志配置 mybatis-plus.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl 我们所有的sql现在是不可见的&#xff0c;我们希望知道它是怎么执行的&#xff0c;所以我们必须要看日志

远程连接工具NoMachine的使用

一、软件介绍 NoMachine是一款远程桌面软件。适用于Linux、windows、ARM、Android等几乎全系统。常见的远程桌面软件还有向日葵、ToDesk等。选择NoMachine是因为它支持ARM32位、ARM64位处理器。 ZeroTier是一款内网穿透软件&#xff0c;通过建立虚拟局域网&#xff0c;能够实现…