GetWords
来源:互联网 发布:grid.base.js 编辑:程序博客网 时间:2024/04/30 15:17
#include <cstring>#include <string>#include <vector>#include <algorithm>#include <cctype>using namespace std;class StringEqual{public:StringEqual(const string &s): _str(s) {};bool operator()(pair<string , int> pr){if (pr.first == _str)return true;elsereturn false;}private:string _str;};bool CompareMore(pair<string, int> left, pair<string, int> right){return (left.second > right.second);}void GetWords(char *InputString, char *OutputString){char *pInStart = InputString;char *pOutStart = OutputString;string word;pair<string, int> prStringCount;vector<pair<string, int>> vWordSet;if (*pInStart == '\0') { // InputString为空*pOutStart = *pInStart;return ;}do {if (*pInStart == ' ' || *pInStart == '\t' || *pInStart == '\n' || ispunct(*pInStart)) {if (!word.empty()) {// vector<pair<string, int>>::iterator vit = find_if(vWordSet.begin(), vWordSet.end(), StringEqual(word));// if (vit != vWordSet.end()) {// vit->second++;// } else {// prStringCount.first = word;// prStringCount.second = 1;// vWordSet.push_back(prStringCount);// }vector<pair<string, int>>::iterator vit = vWordSet.begin();while (vit != vWordSet.end()) {if (vit->first == word)break;++vit;}if (vit == vWordSet.end()) {prStringCount.first = word;prStringCount.second = 1;vWordSet.push_back(prStringCount);} else {vit->second++;}word.clear();}} else if(isalnum(*pInStart)) {word += *pInStart;} } while (*++pInStart != '\0');stable_sort(vWordSet.begin(), vWordSet.end(), CompareMore);for (vector<pair<string, int>>::iterator vit = vWordSet.begin(); vit != vWordSet.end(); ++vit) {int size = vit->first.size();strcpy(pOutStart, &(vit->first[0]));pOutStart += size;*pOutStart++ = ' ';}if (!vWordSet.empty())pOutStart--;*pOutStart = '\0';}int main(){char InputString[] = " Life is what you can make it,so try to make it beautiful! it";char OutputString[128] = {0};GetWords(InputString, OutputString);printf("OutputString:%s\n", OutputString);system("pause");return 0;}
0 0
- GetWords
- 2.7 GetWords
- Android 开发技巧集合
- Jsp内置对象的简单说明
- 中国空气质量分析
- 详解MTK系统中字符转换问题
- air jordan pas cher Nike designed for the Italian soccer shoes
- GetWords
- 【JAVA笔记——道】Hibernate 线程本地化基础篇
- basket nike pas cher adjustable dumbbells are the answer.
- jdbcTemplate与hibernateTemplate混用
- chaussures nike ensuring superior engineering in their headphones down to the smallest detail.
- Javascript中数据类型及其判别方法(typeof instanceof Object.prototype.toSting.call(obj)的区别)
- air max pas cher Tiongkok tendance au refroidissement
- nike femme See next point
- Codeforces 104C Cthulhu dfs暴力 || 点双连通缩点