C ++使用带有字符串,count_if与ISDIGIT标准算法,功能投 [英] C++ using standard algorithms with strings, count_if with isdigit, function cast
问题描述
我想指望在字符串中的所有数字在最短code方式。我想这种方式:
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);
}
错误消息是:
Error message is:
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()想要的功能,如: 布尔(* F)(焦);作为第三个参数,所以我试着投功能:
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));
}
错误消息是:
Error message is:
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);
}
性病
我的问题是,我该如何使用函数INT(* F)(INT)::这要布尔(* F)(INT),而无需创建适配器的功能,不使用lambda EX $ P算法的功能$ pssions?
我有更多的问题,当我知道如何解决这个问题,这将得到解决,例如:
I have more issues which would be solved when I get know how to solve the problem, e.g.:
- 检查字符串是打印:find_if_not(s.begin(),s.end(),isprint判断)
- 检查字符串包含,......。!?:find_if(s.begin(),s.end(),ispunct) 更多...
- Check if string is printable: find_if_not(s.begin(), s.end(), isprint)
- Check if string contains ",.!?...": find_if (s.begin(), s.end(), ispunct) and more...
我只是想知道如何在标准C ++得益于更字符串可能为std ::算法 我正在寻找在互联网很长一段时间,我发现<一href="http://stackoverflow.com/questions/13262444/finding-an-alphanumeric-character-in-a-string-using-find-if-and-isalnum">similar 的问题,但我没有发现任何解决方案
I just want to know how to have much more string possibilities in standard C++ thanks to std::algorithms I 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
是解决歧义的通常的方式 - 它总是你所期望,可以是一个较大的前pression一部分。
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标准算法,功能投的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!