在 C++ 中,最好没有 Boost 库,如何确保 std::string str 包含数字或数字后跟 '%' 符号?如果不属于这两种情况,则应发出错误。
最佳答案
#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>
bool is_a_bad_char(char c) {
return !(isdigit(c) || (c=='%'));
}
int main() {
std::string str = "123123%4141219";
if (std::find_if(str.begin(), str.end(), is_a_bad_char) != str.end()) {
std::cout << "error" << std::endl;
return 1;
}
return 0;
}
关于c++ - 将字符串解析为数字或数字和百分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9666252/