我有以下代码
std::string t = "11:05:47" (No spaces inside)
我想检查它是否有一个空格(它没有),所以我使用
unsigned present = t.find(" ");
if (present!=std::string::npos)
{
//Ends up in here
}
代码似乎认为字符串里面有一个空白的空间有什么建议我可能做错了
结果如下Present = 4294967295T = 11:15:36
是否有一个boost库可以帮助我做到这一点?有什么建议吗?
不要使用unsigned
。std::string::find
返回一个std::string::size_type
,通常是size_t
。
std::string::size_type present = t.find(" ");
if (present!=std::string::npos) {
}
正如其他人指出的那样,您可以使用c++ 11的auto
来让编译器推断present
应该是什么类型:
auto present = t.find(" ");