以下是查找字符串是否仅包含数字的代码:
#include <iostream> //see below
#include <string>
#include <cctype>
#include <algorithm>
int main() {
std::string s("123");
std::all_of(s.begin(), s.end(), std::isdigit); //error!
}
Run Code Online (Sandbox Code Playgroud)
但代码无法编译,出现以下错误:
error: no matching function for call to 'all_of'
note: candidate template ignored: couldn't infer template argument '_Predicate'
all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
Run Code Online (Sandbox Code Playgroud)
当我替换std::isdigit为时isdigit,代码编译时没有错误。
error: no matching function for call to 'all_of'
note: candidate template ignored: couldn't infer template argument '_Predicate'
all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
Run Code Online (Sandbox Code Playgroud)
isdigit那么和之间有什么区别呢std::isdigit?我知道它isdigit是从 C …