本文介绍了C ++使用标准算法与字符串,count_if与isdigit,函数转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想以最短代码的方式计算字符串中的所有数字。我试过这样:I want to count all numbers in string in shortest code way. I tried that way:#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);作为第三个参数,所以我试图转换函数:I know that count_if() wants function like:bool (*f)(char); as a third argument, so I tried to cast the function: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我也尝试了更长的版本,这给出了相同的编译错误:I tried also a bit longer version, which gives the same compilation error: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);}我想避免的解决方案是创建一个函数,适配器:The solution that I want to avoid is to create a function which would be an adapter:#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);} 有更多的问题,当我知道如何解决这个问题,将解决例如:I have more issues which would be solved when I get know how to solve the problem, e.g.: 检查字符串是否可打印:find_if_not 检查字符串是否包含,。!?...:find_if(s.begin(),s.end() ),ispunct)和更多... 我只想知道如何在标准C ++感谢std :: algorithms 我在互联网上搜索很长时间,我发现类似问题,但我没有找到解决方案I just want to know how to have much more string possibilities in standard C++ thanks to std::algorithmsI was searching at the Internet long time, I found similar problem, but I found no solution推荐答案您可以使用静态转换来解析函数。或者,如果这是你想要做的事,你可以使用模板来解决它:You can resolve the function by using a static cast. Alternatively if this is something you want to do a lot you can use a template to resolve it:#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 通常的方式解决模糊 - 它总是做你期望的,可以成为一个更大的表达式的一部分。 static_cast is the "usual" way of resolving ambiguous - it always does what you expect and can be part of a larger expression. 这篇关于C ++使用标准算法与字符串,count_if与isdigit,函数转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 19:51