【发布时间】:2023-02-04 10:47:33
【问题描述】:
如何计算 "bla_bla_blabla_bla" 之类的字符串中 "_" 的数量?
【问题讨论】:
标签: c++ string pattern-matching
如何计算 "bla_bla_blabla_bla" 之类的字符串中 "_" 的数量?
【问题讨论】:
标签: c++ string pattern-matching
#include <algorithm>
std::string s = "a_b_c";
std::string::difference_type n = std::count(s.begin(), s.end(), '_');
【讨论】:
std::count 返回类型 iterator_traits<InputIt>::difference_type,对于大多数标准容器来说,它是 std::ptrdiff_t,而不是 std::size_t。
std::count 返回值的变量应该是std::string::difference_type 类型以获得最大正确性。我提交了一个编辑答案的请求:std::string::difference_type n = std::count(s.begin(), s.end(), '_');
auto 的好案例。 ;)
伪代码:
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;
}
请注意,这是与std::string一起使用的代码,如果您使用char*,请将s.size()替换为strlen(s)。
另请注意:我可以理解您想要“尽可能小”的东西,但我建议您改用此解决方案。如您所见,您可以使用一个函数为您封装代码,这样您就不必每次都写出 for 循环,而可以在其余代码中使用 count_underscores("my_string_")。在这里使用高级 C++ 算法当然是可能的,但我认为这是矫枉过正的。
【讨论】:
具有适当命名变量的老式解决方案。这给了代码一些精神。
#include <cstdio>
int _(char*__){int ___=0;while(*__)___='_'==*__++?___+1:___;return ___;}int main(){char*__="_la_blba_bla__bla___";printf("The string "%s" contains %d _ characters
",__,_(__));}
编辑:大约 8 年后,看着这个答案我很惭愧我这样做了(尽管我向自己证明这是对一个不费力的问题的尖刻戳戳)。这是有毒的,不行。我不会删除帖子;我添加此道歉是为了帮助改变 StackOverflow 上的气氛。所以 OP:我深表歉意,我希望你能把作业做对,尽管我很挑剔,像我这样的回答并没有阻止你参与这个网站。
【讨论】:
使用 lambda 函数检查字符是“_”然后唯一的计数将递增否则不是有效字符
std::string s = "a_b_c";
size_t count = std::count_if( s.begin(), s.end(), []( char c ){return c =='_';});
std::cout << "The count of numbers: " << count << std::endl;
【讨论】:
size_t count = std::count_if( s.begin(), s.end(), []( char c ){if(c =='_') return true; else return false; });
size_t count = std::count_if( s.begin(), s.end(), []( char c ){ return c == '_'; });
#include <boost/range/algorithm/count.hpp>
std::string str = "a_b_c";
int cnt = boost::count(str, '_');
【讨论】:
你的名字... Lambda 版本... :)
using namespace boost::lambda;
std::string s = "a_b_c";
std::cout << std::count_if (s.begin(), s.end(), _1 == '_') << std::endl;
你需要几个包含......我把它留给你作为练习......
【讨论】:
std::count 已经完成了所有需要的事情时,为什么所有的复杂性?
计算字符串中出现的字符很容易:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s="Sakib Hossain";
int cou=count(s.begin(),s.end(),'a');
cout<<cou;
}
【讨论】:
std::string 有几种搜索方法,但 find 可能是您要查找的方法。如果您指的是 C 风格的字符串,则等效项是 strchr。但是,在任何一种情况下,您都可以使用 for 循环并检查每个字符——循环本质上就是这两者的结合。
一旦知道如何找到给定起始位置的下一个字符,就可以不断推进搜索(即使用循环),边走边数。
【讨论】:
我会这样做:
#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;
}
【讨论】:
您可以使用字符串函数找出源字符串中是否出现“_”。 find() 函数有 2 个参数,第一个是我们要查找其出现的字符串,第二个参数是起始位置。While 循环用于查找直到源字符串末尾的出现。
例子:
string str2 = "_";
string strData = "bla_bla_blabla_bla_";
size_t pos = 0,pos2;
while ((pos = strData.find(str2, pos)) < strData.length())
{
printf("
%d", pos);
pos += str2.length();
}
【讨论】:
基于范围的for循环派上用场
int countUnderScores(string str)
{
int count = 0;
for (char c: str)
if (c == '_') count++;
return count;
}
int main()
{
string str = "bla_bla_blabla_bla";
int count = countUnderScores(str);
cout << count << endl;
}
【讨论】:
我会做那样的事情:)
const char* str = "bla_bla_blabla_bla";
char* p = str;
unsigned int count = 0;
while (*p != '
尝试
#include <iostream>
#include <string>
using namespace std;
int WordOccurrenceCount( std::string const & str, std::string const & word )
{
int count(0);
std::string::size_type word_pos( 0 );
while ( word_pos!=std::string::npos )
{
word_pos = str.find(word, word_pos );
if ( word_pos != std::string::npos )
{
++count;
// start next search after this word
word_pos += word.length();
}
}
return count;
}
int main()
{
string sting1="theeee peeeearl is in theeee riveeeer";
string word1="e";
cout<<word1<<" occurs "<<WordOccurrenceCount(sting1,word1)<<" times in ["<<sting1 <<"]
";
return 0;
}
【讨论】:
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]);
}
}
【讨论】: