对字符串数组进行排序,将变位词排在相邻位置
来源:互联网 发布:淘宝上的卫生巾能买吗 编辑:程序博客网 时间:2024/04/30 17:25
#include<iostream>#include<string>#include<vector>#include<hash_map>#include<list>#include<algorithm>using namespace std;string sortChars(string s){sort(s.begin(), s.end(), less<char>());return s;}void sort(vector<string>& array){hash_map<string, list<string>> hash;list<string> l;auto it = array.begin();for (; it != array.end(); ++it){string key = sortChars(*it);auto hashiterator = hash.find(key);if (hashiterator == hash.end()){hash[key] = l;}hash[key].push_back(*it);}int index = 0;for (auto hashkey = hash.begin(); hashkey != hash.end(); ++hashkey){for (auto listIt = (*hashkey).second.begin(); listIt != (*hashkey).second.end(); ++listIt){array[index] = *listIt;index++;}}}void show(vector<string> a){for (auto it = a.begin(); it != a.end(); it++)cout << *it << " ";cout << endl;}int main(){vector<string> a;a.push_back("acre");a.push_back("hello"); a.push_back("syntax"); a.push_back("care");a.push_back("fatigue"); a.push_back("race");a.push_back("misinterpretation"); a.push_back("montony");show(a);cout << endl; cout << endl; cout << endl;sort(a);show(a);cin.get();return 0;}执行结果:
如果需要改变单词则可在string sortChars(string s)中加上引用string sortChars(string& s),执行结果:
当然在编程珠玑2.4中,也论述了变位词除了排序,也可以用统计各字符出现次数来判断
0 0
- 对字符串数组进行排序,将所有变位词排在相邻的位置。
- 对字符串数组进行排序,将变位词排在相邻位置
- 【Java】编写一个方法,对字符串数组进行排序,将所有变位词排在相邻的位置
- 编写一个方法,对字符串进行排序,将所有变位词排在相邻的位置
- 9.11排序与查找(二)——对字符串数组进行排序,将所有的变位词排在相邻的位置
- 程序员面试金典: 9.11 排序与查找 11.1编写一个方法,对字符串数组进行排序,将所有变位词排在相邻的位置。
- 变位词排在相邻的位置
- 对字符串数组进行排序
- C++利用指针数组和快排对输入的字符串进行排序
- Sorting&Searching 基于变位词的字符串数组排序 @CareerCup
- 对扑克牌字符串数组进行排序
- 用指针数组对字符串进行排序
- C#如何对字符串数组进行排序?
- C语言 字符串用指针数组将字符串进行排序
- qsort 对字符串和字符串数组进行排序
- 对字符串数组进行排序,在冒泡法排序中使用compareTo()方法确定排序的顺序。
- 对数组进行排序
- 对数组进行排序
- 利用java的native2ascii转换编码
- centos 7 下安装php-sqlite
- 在linux下查看文件夹和文件大小
- 转载listview优化
- Android中的倒计时
- 对字符串数组进行排序,将变位词排在相邻位置
- jsp页面实现倒计时自动跳转
- Android Studio使用新的Gradle构建工具配置NDK环境
- 基于小端规则的几个java方法
- iOS 关于ASIHTTPRequest
- 字节数组转字符串
- centos7 防火墙的开启与关闭
- Java提高篇(三三)-----Map总结
- Android开发,AchartEngine使用