c++ string中有没有返回特定字符串出现的次数的函数?

sunboyqq23 2011-08-30 08:48:51
比如“123b123”中查找“123”出现的次数就返回2
...全文
825 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
RLib 2011-08-31
  • 打赏
  • 举报
回复
不断的strstr
sunboyqq23 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gykgod 的回复:]
木有算几个的 油酸位置的

find就能返回字串的位置,您可以找一个删掉,再找直到找不到 计个数

C/C++ code


#include <string>
#include <iostream>

int main( )
{
using namespace std;

// The first member function
// search……
[/Quote]
码了这么多行,辛苦了 -_-
turing-complete 2011-08-30
  • 打赏
  • 举报
回复
自己封装一下吧很简单

另外推荐个资源 http://download.csdn.net/source/3510689
gykgod 2011-08-30
  • 打赏
  • 举报
回复
木有算几个的 油酸位置的

find就能返回字串的位置,您可以找一个删掉,再找直到找不到 计个数


#include <string>
#include <iostream>

int main( )
{
using namespace std;

// The first member function
// searches for a single character in a string
string str1 ( "Hello Everyone" );
cout << "The original string str1 is: " << str1 << endl;
basic_string <char>::size_type indexCh1a, indexCh1b;

indexCh1a = str1.find ( "e" , 3 );
if (indexCh1a != string::npos )
cout << "The index of the 1st 'e' found after the 3rd"
<< " position in str1 is: " << indexCh1a << endl;
else
cout << "The character 'e' was not found in str1 ." << endl;

indexCh1b = str1.find ( "x" );
if (indexCh1b != string::npos )
cout << "The index of the 'x' found in str1 is: "
<< indexCh1b << endl << endl;
else
cout << "The Character 'x' was not found in str1."
<< endl << endl;

// The second member function searches a string
// for a substring as specified by a C-string
string str2 ( "Let me make this perfectly clear." );
cout << "The original string str2 is: " << str2 << endl;
basic_string <char>::size_type indexCh2a, indexCh2b;

const char *cstr2 = "perfect";
indexCh2a = str2.find ( cstr2 , 5 );
if ( indexCh2a != string::npos )
cout << "The index of the 1st element of 'perfect' "
<< "after\n the 5th position in str2 is: "
<< indexCh2a << endl;
else
cout << "The substring 'perfect' was not found in str2 ."
<< endl;

const char *cstr2b = "imperfectly";
indexCh2b = str2.find ( cstr2b , 0 );
if (indexCh2b != string::npos )
cout << "The index of the 1st element of 'imperfect' "
<< "after\n the 5th position in str3 is: "
<< indexCh2b << endl;
else
cout << "The substring 'imperfect' was not found in str2 ."
<< endl << endl;

// The third member function searches a string
// for a substring as specified by a C-string
string str3 ( "This is a sample string for this program" );
cout << "The original string str3 is: " << str3 << endl;
basic_string <char>::size_type indexCh3a, indexCh3b;

const char *cstr3a = "sample";
indexCh3a = str3.find ( cstr3a );
if ( indexCh3a != string::npos )
cout << "The index of the 1st element of sample "
<< "in str3 is: " << indexCh3a << endl;
else
cout << "The substring 'perfect' was not found in str3 ."
<< endl;

const char *cstr3b = "for";
indexCh3b = str3.find ( cstr3b , indexCh3a + 1 , 2 );
if (indexCh3b != string::npos )
cout << "The index of the next occurrence of 'for' is in "
<< "str3 begins at: " << indexCh3b << endl << endl;
else
cout << "There is no next occurrence of 'for' in str3 ."
<< endl << endl;

// The fourth member function searches a string
// for a substring as specified by a string
string str4 ( "clearly this perfectly unclear." );
cout << "The original string str4 is: " << str4 << endl;
basic_string <char>::size_type indexCh4a, indexCh4b;

string str4a ( "clear" );
indexCh4a = str4.find ( str4a , 5 );
if ( indexCh4a != string::npos )
cout << "The index of the 1st element of 'clear' "
<< "after\n the 5th position in str4 is: "
<< indexCh4a << endl;
else
cout << "The substring 'clear' was not found in str4 ."
<< endl;

string str4b ( "clear" );
indexCh4b = str4.find ( str4b );
if (indexCh4b != string::npos )
cout << "The index of the 1st element of 'clear' "
<< "in str4 is: "
<< indexCh4b << endl;
else
cout << "The substring 'clear' was not found in str4 ."
<< endl << endl;
}

zmshy2128 2011-08-30
  • 打赏
  • 举报
回复
准确的说:没有。

在标准的string中,只提供了查找字符的功能,没有查找子字符串的功能。
boost中有个字符串的算法库,功能还不错。还可以实现不区分大小写的查找匹配

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧