C++字符串与 int 进行比较
C++ String Compare with Int
我有以下问题:
do
{
cout << "Type in something nice: ";
getline(cin,test);
}
while (test.empty());
此代码片段将检查输入是否为空。如果为空,则相同的查询将重复。
现在我想添加一个函数,字符串"test"必须在 0 到 100 之间。如何将字符串与静态 int 值进行比较?
stoi 函数在这里不起作用,因为我必须使用非 c++ 11 编译器
你必须将字符串转换为 int:-
int b = atoi(a.c_str());
然后在确保转换成功后检查 B 是否介于 0 和 100 之间。
do
{
cout << "Type in something nice: ";
getline(cin,test);
}
while (test.empty() && 0 > stoi(test) && stoi(test) < 100);
函数 Stoi 将字符串转换为整数
相关文章:
- 比较if语句中的数组值和int值
- 比较两个整数在C++中与未知 int 类型的相等性
- 与 string.size() 比较时 int 的符号性显示警告
- 错误:运算符 [] 不匹配。在比较列表中的 int 和 int 时<int>,
- 在 IF 语句中比较 int 时出现奇怪的行为
- std::<int>betterer() 比较器与 partial_copy_sort 的困难,在 Mac OSX 上"no matching function call.."
- 无符号int的比较始终是正确的(NPOS问题?)
- 有没有办法比较用 int 初始化的 char 类型的数组的 2 个元素
- 将 int 与 double 进行比较的预处理器"invalid integer constant expression"
- 为 map<int、pair<int、int 创建比较器>>
- 如何使用将 const int 的运算符用作 int* 优先级队列的比较器
- 比较int和double时出错
- 比较INT会产生奇怪的结果
- Visual Studio调试与发布版本:比较int和float不匹配
- 如何比较 int 和字符
- C++ 如何比较<int>从开始到最后一个元素的集合 -1
- 为什么我可以在C中赋值/比较int和char ?
- 比较int和unsigned引用时警告,但比较g++/msvc的const(无ref)时没有警告
- 比较 (int)double 和 (int)int 时出现异常
- 比较 (int)double 和 (int)int 时出现异常