8

我想以最短的代码方式计算字符串中的所有数字。我试过这样:

#include <string>
#include <algorithm>

unsigned countNumbers(const std::string s) {
    return count_if(s.begin(), s.end(), isdigit);
}

错误信息是:

a.cc: In function ‘unsigned int countNumbers(std::string)’:
a.cc:5:45: error: no matching function for call to ‘count_if(std::basic_string<char>::const_iterator, std::basic_string<char>::const_iterator, <unresolved overloaded function type>)’
a.cc:5:45: note: candidate is:
/usr/include/c++/4.6/bits/stl_algo.h:4607:5: note: template<class _IIter, class _Predicate> typename std::iterator_traits<_InputIterator>::difference_type std::count_if(_IIter, _IIter, _Predicate)

我知道 count_if() 想要这样的功能: bool (*f)(char); 作为第三个参数,所以我尝试强制转换函数:

unsigned countNumbers(const std::string s) {
    return count_if(s.begin(), s.end(), reinterpret_cast<bool (*)( char )>(isdigit));
}

错误信息是:

a.cc: In function ‘unsigned int countNumbers(std::string)’:
a.cc:5:80: error: overloaded function with no contextual type information

我也尝试了更长的版本,它给出了相同的编译错误:

unsigned countNumbers(const std::string s) {
    typedef bool ( * f_ptr )( char );
    f_ptr ptr = reinterpret_cast<f_ptr>(isdigit);
    return count_if(s.begin(), s.end(), ptr);
}

我想避免的解决方案是创建一个作为适配器的函数:

#include <string>
#include <algorithm>

bool is_digit(char c) {
    return isdigit(c);
}

unsigned countNumbers(const std::string s) {
    return count_if(s.begin(), s.end(), is_digit);
}

我的问题是如何在 std::algorithm 的函数中使用函数 int(*f)(int) 而不创建适配器函数和不使用 lambda 表达式?

当我知道如何解决问题时,我还有更多问题可以解决,例如:

  • 检查字符串是否可打印:find_if_not(s.begin(), s.end(), isprint)
  • 检查字符串是否包含“,.!?...”:find_if (s.begin(), s.end(), ispunct) 等等...

我只是想知道如何在标准 C++ 中拥有更多的字符串可能性,这要归功于我在互联网上搜索了很长时间的 std::algorithms,我发现了类似的问题,但我没有找到解决方案

4

4 回答 4

6

您可以使用静态强制转换来解析该函数​​。或者,如果这是您想要做的很多事情,您可以使用模板来解决它:

#include <string>
#include <cctype>
#include <algorithm>

unsigned count(const std::string& s) {
  return std::count_if(s.begin(), s.end(), static_cast<int(*)(int)>(std::isdigit));
}

template <int(*Pred)(int)> 
unsigned foo(const std::string& s) {
  return std::count_if(s.begin(), s.end(), Pred);
}

int main() {
  count("");
  foo<std::isdigit>("");
  foo<std::isprint>("");
}

static_cast是解决模棱两可的“通常”方式 - 它总是按照您的预期进行,并且可以成为更大表达式的一部分。

于 2013-02-23T11:22:37.663 回答
3

使用函数指针解析类型:

unsigned countNumbers(const std::string s) {
    int (*isdigit)(int) = std::isdigit;
    return count_if(s.begin(), s.end(), isdigit);
}

不要忘记包括<cctype>. (演示

于 2013-02-23T11:19:25.143 回答
2

我发现以下也有效:

    #include <ctype.h>
    count_if(s.begin(), s.end(), ::isdigit); //explicitly select the C version of isdigit 

但我必须弄清楚它只有在 C 版本被定义为函数而不是宏时才有效

因此,std::isdigit 的 static_cast 可能是平台中最好的可移植解决方案。

于 2014-07-29T03:49:40.340 回答
2

我将提供 2 个其他解决方案来解决歧义<locale> std::isdigit

  1. 使用 lambda 表达式:

    std::count_if(s.begin(), s.end(), [](char c){ return std::isdigit(c); })

或显式转换:

std::count_if(s.begin(), s.end(), [](char c){ return std::isdigit(static_cast<int>(c)) != 0; })
  1. 使用显式模板类型(您还需要编写迭代器类型):

std::count_if<std::string::const_iterator, int(*)(int)>(s.begin(), s.end(), std::isdigit)

于 2018-02-22T18:46:04.497 回答