设为首页 加入收藏

TOP

8.8.5 搜索字符串(2)
2013-10-07 12:37:31 来源: 作者: 【 】 浏览:73
Tags:8.8.5 搜索 字符串

8.8.5  搜索字符串(2)

对于find_fisrt_of()和find_last_of()函数的所有版本,如果没有发现匹配的字符,都会返回string::npos。

使用与上一个代码片段中相同的字符串,可以看到find_last_of()函数对字符串"had"所执行的搜索。

  1. size_t count = 0;  
  2. size_t offset = string::npos;  
  3. while(true)  
  4. {  
  5. offset = str.find_last_of(substr, offset);  
  6. if(offset == string::npos)  
  7. break;  
  8. --offset;  
  9. ++count;  
  10. }  
  11. cout << endl << " Characters from the string \"" << substr 
  12. << "\" were found " << count << " times in the string above."  
  13. << endl

这次我们从字符串末尾索引位置string::npos开始向后搜索,因为这是默认开始位置。该代码片段的输出为:

  1. The string to be searched is:  
  2. Smith, where Jones had had "had had", "had had"
    had. "Had had" had had  
  3. the examiners' approval.  
  4.  
  5. Characters from the string "had" were found 38
    times in the string above. 

结果应当不出意料。记住,我们在搜索字符串str中出现的"had"中的任何字符。"Had"和"had"单词中有32个,其他单词中有6个。因为我们在沿着字符串向后搜索,因此当我们发现一个字符时,递减循环内的offset。

最后一组搜索函数是find_first_not_of()和find_last_not_of()函数的各个版本,如表8-5所示。

表  8-5

    < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

   

find_first_not_of(char ch,

size_t offset=0)

string对象中搜索从offset

索引位置开始出现的不是字符ch

的字符,并返回发现字符的索引

位置值,类型为size_t。如果省略

第二个实参,offset的默认值就为0

find_first_not_of(

char* pstr,

size_t offset=0)

string对象中搜索从offset索引

位置开始第一次出现的不在以空字

符结尾的字符串pstring中的字符,

并返回发现字符的索引位置值,类型

size_t。如果省略第二个实参,

offset的默认值就为0

find_first_not_of(

char* pstr,

         size_t offset,

size_t count)

string对象中搜索从offset索引

位置开始第一次出现的不在以空字

符结尾的字符串pstring中的前

count个字符中的任何字符,并返

回发现字符的索引位置值,类型为size_t

find_first_not_of(

string str,

size_t offset=0)

string对象中搜索从offset索引位

置开始第一次出现的不在字符串str

中的任何字符,并返回发现字符的索

引位置值,类型为size_t。如果省略

第二个实参,offset的默认值就为0

find_last_not_of(char ch,

size_t offset=npos)

string对象中向后搜索从offset

引位置开始最后一次出现的不是字符

ch的字符,并返回发现该字符的索

引位置值,类型为size_t。如果省略

第二个实参,offset的默认值就为字

符串的末尾字符nops

find_last_not_of(char* pstr,

size_t offset=npos)

string对象中向后搜索从offset

引位置开始最后一次出现的不在以

空字符结尾的字符串pstr中的任何

字符,并返回发现该字符的索引位置

值,类型为size_t。如果省略第二个

实参,offset的默认值就为字符串的

末尾字符nops

find_last_not_of(char* pstr,

size_t offset,

size_t count)

string对象中向后搜索从offset

引位置开始最后一次出现的不在以

空字符结尾的字符串pstr中的前

count个字符中的字符。该函数返回

发现该字符的索引位置值,类型为size_t

find_last_not_of(string str,

size_t offset=npos)

string对象中向后搜索从offset

引位置开始最后一次出现的不在字

符串str中的任何字符,并返回发现

该字符的索引位置值,类型为size_t

如果省略第二个实参,offset的默认

值就为字符串的末尾字符nops

对于前面的这些搜索函数,如果没有搜索到匹配的字符,将返回string::npos。这些函数有很多用法,通常用来在字符串中查找可能由各种字符隔开的令牌(token)。例如,用空格和标点符号隔开的单词组成的文本,因此,我们可以用这些函数在文本块中查找单词。下面举例说明其工作过程。

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

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

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

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

评论

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