设为首页 加入收藏

TOP

8.11.5 搜索字符串(4)
2013-10-07 16:11:04 来源: 作者: 【 】 浏览:73
Tags:8.11.5 搜索 字符串

8.11.5  搜索字符串(4)

试一试:排序文本中的单词

本例将读一个文本块,然后提取一些单词,并以升序输出它们。在此将使用相当低效的冒泡排序函数,这在Ex8_14中可以看到。在第10章将使用一个好用得多的库函数来排序,不过在使用此函数前需要先了解一些别的知识。该程序也会计算每个单词出现的次数,并输出各个单词的个数。因此,这样的分析称为词语搭配(collocation)。代码如下:

  1. // Ex8_15.cpp  
  2. // Extracting words from text  
  3. #include <iostream> 
  4. #include <iomanip> 
  5. #include <string> 
  6. using std::cin;  
  7. using std::cout;  
  8. using std::endl;  
  9. using std::ios;  
  10. using std::setiosflags;  
  11. using std::resetiosflags;  
  12. using std::setw;  
  13. using std::string;  
  14. // Sort an array of string objects  
  15. string* sort(string* strings, size_t count)  
  16. {  
  17. bool swapped(false);  
  18. while(true)  
  19. {  
  20. for(size_t i = 0 ; i < count-1 ; i++)  
  21. {  
  22. if(strings[i] > strings[i+1])  
  23. {  
  24. swapped = true;  
  25. strings[i].swap(strings[i+1]);  
  26. }  
  27. }  
  28. if(!swapped)  
  29. break;  
  30. swapped = false;  
  31. }  
  32. return strings;  
  33. }  
  34. int main()  
  35. {  
  36. const size_t maxwords(100);  
  37. string words[maxwords];  
  38. string text;  
  39. string separators(" \".,:;! ()\n");  
  40. size_t nwords(0);  
  41. size_t maxwidth(0);  
  42. cout << "Enter some text on as many lines as you wish."  
  43. << endl << "Terminate the input with an asterisk:" << endl;  
  44. getline(cin, text, '*');  
  45. size_t start(0), end(0), offset(0);             // Record start & end of word & offset  
  46. while(true)  
  47. {  
  48. // Find first character of a word  
  49. start = text.find_first_not_of(separators, offset);   // Find non-separator  
  50. if(string::npos == start)                                           // If we did not find it, we are done  
  51. break;  
  52. offset = start + 1;                                                             // Move past character found  
  53. // Find first separator past end of current word  
  54. end = text.find_first_of(separators,offset);           // Find separator  
  55. if(string::npos == end)                 // If it's the end of the string  
  56. {                                                                                                                           // current word is last in string  
  57. offset = end;                                                                               // We use offset to end loop later  
  58. end = text.length();                                                        // Set end as 1 past last character  
  59. }  
  60. else  
  61. offset = end + 1;                                                               // Move past character found  
  62. words[nwords] = text.substr(start, end-start);       // Extract the word  
  63. // Keep track of longest word  
  64. if(maxwidth < words[nwords].length())  
  65. maxwidth = words[nwords].length();  
  66. if(++nwords == maxwords)                                                // Check for array full  
  67. {  
  68. cout << endl << "Maximum number of words reached."  
  69. << endl << "Processing what we have." << endl;  
  70. break;  
  71. }  
  72. if(string::npos == offset)                                      // If we reached the end of the string  
  73. break;                                                                                                      // We are done  
  74. }  
  75. sort(words, nwords);  
  76. cout << endl 
  77. << "In ascending sequence, the words in the text are:"  
  78. << endl;  
  79. size_t count(0);                                     // Count of duplicate words  
  80. // Output words and number of occurrences  
  81. for(size_t i = 0 ; i < nwords ; i++)  
  82. {  
  83. if(0 == count)  
  84. count = 1;  
  85. if(i < nwords-2 && words[i] == words[i+1])  
  86. {  
  87. ++count;  
  88. continue;  
  89. }  
  90. cout << setiosflags(ios::left)                 // Output word left-justified  
  91. << setw(maxwidth+2) << words[i];  
  92. cout << resetiosflags(ios::right)             // and word count right-justified  
  93. << setw(5) << count < < endl;  
  94. count = 0;  
  95. }  
  96. cout << endl;  
  97. return 0;  
  98. }  

下面是该程序的部分输出:

  1. Enter some text on as many lines as you wish.  
  2. Terminate the input with an asterisk:  
  3. I sometimes think I'd rather crow  
  4. And be a rooster than to roost  
  5. And be a crow. But I dunno.  
  6. A rooster he can roost also,  
  7. Which don't seem fair when crows can't crow  
  8. Which may help some. Still I dunno.*  
  9. In ascending sequence, the words in the text are:  
  10. A                                       1  
  11. And                             2  
  12. But                             1  
  13. I                                       3  
  14. I'd                             1  
  15. Still                       1  
  16. Which                       2  
  17. a                                       2  
  18. also                            1  
  19. be                                  2  
  20. can                             1  
  21. can't                       1  
  22. crow                            3  
  23. crows                       1  
  24. don't                       1  
  25. dunno                       2  
  26. fair                            1  
  27. he                                  1  
  28. help                            1  
  29. may                             1  
  30. rather                  1  
  31. roost                       2  
  32. rooster                 2  
  33. seem                            1  
  34. some                            1  
  35. sometimes           1  
  36. than                            1  
  37. think                       1  
  38. to                                  1  
  39. when                            1  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.11.5 搜索字符串(3) 下一篇8.11.5 搜索字符串(5)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)