C++ 为什么count_会给我短信总数
我正在测试下面的代码,有点困惑,为什么count_会返回我所有的文本 将文本字符串作为参数并返回true的Match函数的文本大小为4C++ 为什么count_会给我短信总数,c++,string,algorithm,vector,function-object,C++,String,Algorithm,Vector,Function Object,我正在测试下面的代码,有点困惑,为什么count_会返回我所有的文本 将文本字符串作为参数并返回true的Match函数的文本大小为4 bool Match(string Text) { if (Text.size() == 4) return true; } numMatchwes生成向量中的文本总数 int numMatches(vector<string>Texts, bool (*Match)(string Text)) // Texts is a
bool Match(string Text)
{
if (Text.size() == 4)
return true;
}
numMatchwes生成向量中的文本总数
int numMatches(vector<string>Texts, bool (*Match)(string Text)) // Texts is an array
{
int count = 0;
for (int i = 0; i < Texts.size(); i++)
{
if (Match(Texts[i]) == 1)
// checking every string in vector and returns true
count++;
}
return count;
}
int numMatches(vectorTexts,bool(*Match)(字符串文本))//文本是一个数组
{
整数计数=0;
对于(int i=0;i
主要功能
int main()
{
vector<string> texts;
texts.push_back("Bing");
texts.push_back("Pony");
texts.push_back("Mil");
texts.push_back("Sillty");
texts.push_back("Ballz");
texts.push_back("Mars");
cout << Match("Sind") << endl;
cout << "Matches are: " << numMatches(texts, Match) << endl;
cout << endl;
int num = count_if(texts.begin(), texts.end(), Match); // count_if is STL function
cout << num << endl;
}
intmain()
{
矢量文本;
文本。推回(“Bing”);
文本。推回(“小马”);
文本。推回(“Mil”);
文本。推回(“愚蠢”);
文本。推回(“鲍尔兹”);
文本。推回(“火星”);
cout当传递的字符串长度不等于4时,函数Match
具有未定义的行为
用下面的方法定义它
bool Match( const std::string &Text )
{
return Text.size() == 4;
}
auto numMatches( const std::vector<std::string> &Texts, bool Match(const std::string & ) )
{
std::vector<std::string>::size_type count = 0;
for ( const auto &s : Texts )
{
if ( Match( s ) ) ++count;
}
return count;
}
相应地,可以通过以下方式定义函数numMatches
bool Match( const std::string &Text )
{
return Text.size() == 4;
}
auto numMatches( const std::vector<std::string> &Texts, bool Match(const std::string & ) )
{
std::vector<std::string>::size_type count = 0;
for ( const auto &s : Texts )
{
if ( Match( s ) ) ++count;
}
return count;
}
当传递的字符串长度不等于4时,函数Match
具有未定义的行为
用下面的方法定义它
bool Match( const std::string &Text )
{
return Text.size() == 4;
}
auto numMatches( const std::vector<std::string> &Texts, bool Match(const std::string & ) )
{
std::vector<std::string>::size_type count = 0;
for ( const auto &s : Texts )
{
if ( Match( s ) ) ++count;
}
return count;
}
相应地,可以通过以下方式定义函数numMatches
bool Match( const std::string &Text )
{
return Text.size() == 4;
}
auto numMatches( const std::vector<std::string> &Texts, bool Match(const std::string & ) )
{
std::vector<std::string>::size_type count = 0;
for ( const auto &s : Texts )
{
if ( Match( s ) ) ++count;
}
return count;
}
当Text
的大小不等于4时,Match
返回什么?如果该代码已编译,则应调整编译器设置。当Text
的大小不等于4时,Match
返回什么?如果该代码已编译,则应调整编译器设置。std::boolalpha
在中定义e> 头,而不是
。由于
包含在
中,您不需要任何额外的头。std::boolalpha
在
头中定义,而不是
。由于
包含在
中,您不需要任何额外的头。