Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C++;_C++_String_Pattern Matching - Fatal编程技术网

C++ 在C++;

C++ 在C++;,c++,string,pattern-matching,C++,String,Pattern Matching,如何计算字符串(如“bla_bla_bla_bla”)中的“u”个数?伪代码: count = 0 For each character c in string s Check if c equals '_' If yes, increase count < >编辑:C++示例代码: int count_underscores(string s) { int count = 0; for (int i = 0; i < s.size(); i++) if (

如何计算字符串(如
“bla_bla_bla_bla”
)中的
“u”个数?

伪代码:

count = 0
For each character c in string s
  Check if c equals '_'
    If yes, increase count
< >编辑:C++示例代码:

int count_underscores(string s) {
  int count = 0;

  for (int i = 0; i < s.size(); i++)
    if (s[i] == '_') count++;

  return count;
}
int count_下划线(字符串s){
整数计数=0;
对于(int i=0;i
请注意,这是与
std::string
一起使用的代码,如果使用的是
char*
,请将
s.size()
替换为
strlen


另外请注意:我可以理解您想要“尽可能小”的东西,但我建议您使用此解决方案。正如您所见,您可以使用一个函数来封装代码,这样您就不必每次都为
循环写出
,而只需在其余代码中使用
计数下划线(“我的字符串”)
。使用先进的C++算法当然是可能的,但我认为它是多余的。Lambda版本…:)

使用名称空间boost::lambda;
std::string s=“a_b_c”;

std::cout带有适当命名变量的老式解决方案。这给了代码一些精神

#include <cstdio>
int _(char*__){int ___=0;while(*__)___='_'==*__++?___+1:___;return ___;}int main(){char*__="_la_blba_bla__bla___";printf("The string \"%s\" contains %d _ characters\n",__,_(__));}
#包括
int(char*){int(char*)uuuuuuuuuuuuuuuuuuuu=0;while(*uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu=”=*uuuuuuuuu++?uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu+1:uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu


编辑:大约8年后,看着这个答案,我很惭愧我做了这件事(尽管我对自己说这是一个卑躬屈膝的问题)。这是有毒的,不好。我不是在撤职;我加上这个道歉是为了改变StackOverflow的气氛。所以OP:我很抱歉,我希望你的家庭作业做对了,尽管我在搜索,但像我这样的答案并没有阻止你参与该网站。

有几种搜索std::string的方法,但find可能就是你要找的。如果您指的是C样式的字符串,那么等价物是strchr。但是,在任何一种情况下,您都可以使用for循环并检查每个字符循环本质上就是这两个字符的总括

一旦你知道如何找到给定起始位置的下一个字符,你就可以不断推进搜索(即使用循环),边走边计数。

\include
#include <algorithm>

std::string s = "a_b_c";
size_t n = std::count(s.begin(), s.end(), '_');
std::string s=“a_b_c”; size_t n=std::count(s.begin()、s.end()、'');
试试看

#包括
#包括
使用名称空间std;
int WordOccurrenceCount(std::string const&str,std::string const&word)
{
整数计数(0);
std::string::size\u type word\u pos(0);
while(word_pos!=std::string::npos)
{
word\u pos=str.find(word,word\u pos);
if(word_pos!=std::string::npos)
{
++计数;
//在此单词后开始下一次搜索
单词位置+=单词长度();
}
}
返回计数;
}
int main()
{
字符串sting1=“theeee-peeearl在theeee-riveeer中”;
字符串word1=“e”;
不能包含
std::string str=“a_b_c”;
int cnt=boost::count(str,'.');

您可以使用字符串函数查找源字符串中出现的“\u1”。 find()函数有两个参数,第一个参数是要查找其出现次数的字符串,第二个参数是起始位置。而循环用于查找出现次数,直到源字符串结束

例如:

string str2 = "_";
string strData = "bla_bla_blabla_bla_";

size_t pos = 0,pos2;

while ((pos = strData.find(str2, pos)) < strData.length()) 
{
    printf("\n%d", pos);
    pos += str2.length();
} 
string str2=“25;”;
string strData=“bla_bla_bla_bla_”;
尺寸\u t pos=0,pos2;
而((pos=strData.find(str2,pos))
计算字符串中出现的字符数很容易:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s="Sakib Hossain";
    int cou=count(s.begin(),s.end(),'a');
    cout<<cou;
}
#包括
使用名称空间std;
int main()
{
字符串s=“Sakib Hossain”;
int cou=计数(s.begin(),s.end(),'a');
cout
publicstaticvoidmain(字符串[]args){
char[]array=“aabsbdcbdgratsbdbcfdgs”.toCharArray();
char[][]countArr=新字符[array.length][2];
int lastIndex=0;
for(字符c:数组){
int foundIndex=-1;
对于(int i=0;i=0){
int a=countArr[foundIndex][1];
countArr[foundIndex][1]=(字符)++a;
}否则{
countArr[lastIndex][0]=c;
countArr[lastIndex][1]=“1”;
lastIndex++;
}
}
对于(int i=0;i
我会这样做:

#include <iostream>
#include <string>
using namespace std;
int main()
{

int count = 0;
string s("Hello_world");

for (int i = 0; i < s.size(); i++) 
    {
       if (s.at(i) == '_')    
           count++;
    }
cout << endl << count;
cin.ignore();
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
整数计数=0;
字符串s(“Hello_world”);
对于(int i=0;icout使用lambda函数检查字符是否为“u”,则只增加计数,否则不是有效字符

std::string s = "a_b_c";
size_t count = std::count_if( s.begin(), s.end(), []( char c ){if(c =='_') return true; });
std::cout << "The count of numbers: " << count << std::endl;
std::string s=“a_b_c”;
size_t count=std::count_if(s.begin(),s.end(),[](char c){if(c=''return true;});

std::cout我会这样做:)


当然,我们可以用lamba函数和bind2nd()生成一个完全不可读的模板版本我真的不知道,我认为C++的函数编程实际上是不存在的。我认为调用一个Web服务比LAMBDAS要有趣得多,那么核心算法不仅不可被理解,它还被存储在别处。这不是一个家庭作业问题。我是C++新手,对C++没有足够的知识。+以高级方式编程。阅读:尽可能小。我能够用for循环等简单方式编程,但我正在寻找一种复杂的解决方案,类似于迭戈的解决方案。下次我将提供更多信息,以说明原因
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s="Sakib Hossain";
    int cou=count(s.begin(),s.end(),'a');
    cout<<cou;
}
public static void main(String[] args) {
        char[] array = "aabsbdcbdgratsbdbcfdgs".toCharArray();
        char[][] countArr = new char[array.length][2];
        int lastIndex = 0;
        for (char c : array) {
            int foundIndex = -1;
            for (int i = 0; i < lastIndex; i++) {
                if (countArr[i][0] == c) {
                    foundIndex = i;
                    break;
                }
            }
            if (foundIndex >= 0) {
                int a = countArr[foundIndex][1];
                countArr[foundIndex][1] = (char) ++a;
            } else {
                countArr[lastIndex][0] = c;
                countArr[lastIndex][1] = '1';
                lastIndex++;
            }
        }
        for (int i = 0; i < lastIndex; i++) {
            System.out.println(countArr[i][0] + " " + countArr[i][1]);
        }
    }
#include <iostream>
#include <string>
using namespace std;
int main()
{

int count = 0;
string s("Hello_world");

for (int i = 0; i < s.size(); i++) 
    {
       if (s.at(i) == '_')    
           count++;
    }
cout << endl << count;
cin.ignore();
return 0;
}
std::string s = "a_b_c";
size_t count = std::count_if( s.begin(), s.end(), []( char c ){if(c =='_') return true; });
std::cout << "The count of numbers: " << count << std::endl;
const char* str = "bla_bla_blabla_bla";
char* p = str;    
unsigned int count = 0;
while (*p != '\0')
    if (*p++ == '_')
        count++;