C++如何计算字符串在数据中出现的次数

发布于 2024-11-08 18:14:01 字数 323 浏览 0 评论 0原文

我想测量以下两件事:

  • 逗号出现在 a 中的次数 std::std,例如如果 str ="1,2,3,4,1,2," 如果出现上述情况,则 str.Count(',') 返回我 6 string
  • 第二件事也类似 第一个但不是单个 char 我想计算数字 字符串出现的次数,例如 str.FindAllOccurancesOF("1,2,") 返回我 2

C++ 中是否有任何内置函数用于计算此值,或者我需要为此编写自定义代码?

I want to measure the following two things:

  • How many times a comma appears in a
    std::std, e.g. if str ="1,2,3,4,1,2,"
    then str.Count(',') returns me 6 in case of above
    string
  • The second thing is also similar to
    the first on but instead of single
    char i want to calculate the number
    of occurances of a string e.g
    str.FindAllOccurancesOF("1,2,")
    returns me 2

Is there any builtin functions in c++ for calculating this or i need to write custom code for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想念有你 2024-11-15 18:14:01

关于第一个 -

std::string str="1,2,3,4,1,2," ;
std::count( str.begin(), str.end(), ',' ) ; // include algorithm header

编辑:

使用string::find< /a> -

#include <string>
#include <iostream>

using namespace std;

int main()
{
        string str1 = "1,2,3,1,2,1,2,2,1,2," ;
        string str2 = "1,2," ;

        int count = 0 ;
        int pos = -4;

        while( (pos = str1.find(str2, pos+4) ) != -1 ) // +4 because for the next 
                                                       // iteration current found
                                                       // sequence should be eliminated
        {
            ++count ;         
        }
        cout << count ;
}

IdeOne 结果

Regarding the first one -

std::string str="1,2,3,4,1,2," ;
std::count( str.begin(), str.end(), ',' ) ; // include algorithm header

Edit :

Using string::find -

#include <string>
#include <iostream>

using namespace std;

int main()
{
        string str1 = "1,2,3,1,2,1,2,2,1,2," ;
        string str2 = "1,2," ;

        int count = 0 ;
        int pos = -4;

        while( (pos = str1.find(str2, pos+4) ) != -1 ) // +4 because for the next 
                                                       // iteration current found
                                                       // sequence should be eliminated
        {
            ++count ;         
        }
        cout << count ;
}

IdeOne results

把昨日还给我 2024-11-15 18:14:01

使用 std::string::find 方法之一,您可以单步执行引用字符串,并在每次找到子字符串时进行计数。无需复制或擦除。另外,使用 std::string::npos 来检查是否已找到模式,而不是文字 -1。此外,使用子字符串的大小 std::string::size() 可以避免对步长进行硬编码(其他答案中的文字 4

size_t stringCount(const std::string& referenceString,
                   const std::string& subString) {

  const size_t step = subString.size();

  size_t count(0);
  size_t pos(0) ;

  while( (pos=referenceString.find(subString, pos)) !=std::string::npos) {
    pos +=step;
    ++count ;
  }

  return count;

}

编辑:此函数不允许重叠,即在字符串 "AAAAAAAA" 中搜索子字符串 "AA" 的结果是 4。为了允许重叠,

pos += step

应将此行替换为

++pos

这将导致计数为 7。问题中没有正确指定所需的行为,因此我选择了一种可能性。

Using one of the std::string::find methods, you can step through the reference string, counting each time you find the sub-string. No need for copies or erases. Also, use std::string::npos to check whether the pattern has been found or not, instead of literal -1. Also, using the sub-string's size, std::string::size(), avoids hard coding the step size (literal 4 in other answers)

size_t stringCount(const std::string& referenceString,
                   const std::string& subString) {

  const size_t step = subString.size();

  size_t count(0);
  size_t pos(0) ;

  while( (pos=referenceString.find(subString, pos)) !=std::string::npos) {
    pos +=step;
    ++count ;
  }

  return count;

}

EDIT: This function does not allow for overlaps, i.e. searching for sub-string "AA" in string "AAAAAAAA" results in a count of 4. To allow for overlap, this line

pos += step

should be replaced by

++pos

This will result in a count of 7. The desired behaviour isn't properly specified in the question, so I chose one possibility.

青衫负雪 2024-11-15 18:14:01

如果您使用 char* (C 风格)字符串,则可以尝试以下操作(伪代码):
对于出现的字符进行计数:

const char *str ="1,2,3,4,1,2,", *p = str - 1;
int count = 0
while(0 != (p = strchr(++p, ',')))
  count ++;

对于出现的字符串进行计数:

const char *str ="1,2,3,4,1,2,", *p = str - 1;
int count = 0;
while(0 != (p = strstr(++p, "1,2,")))
  count ++;

If you are using char* (C-style) string then following can be tried (pseudo code):
For counting character occurred:

const char *str ="1,2,3,4,1,2,", *p = str - 1;
int count = 0
while(0 != (p = strchr(++p, ',')))
  count ++;

For counting string occurred:

const char *str ="1,2,3,4,1,2,", *p = str - 1;
int count = 0;
while(0 != (p = strstr(++p, "1,2,")))
  count ++;
请远离我 2024-11-15 18:14:01

string::find() 将开始你的旅程。

string::find() will start you on your way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文