微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – std :: string :: npos的大小不同

我写了一个程序,它使用find_last_of方法在字符串中查找字符.

// ...
unsigned found;
found = name.find_last_of(character);
if (found == std::string::npos) {
    std::cout << "NOT FOUND" << std::endl;
}
// ...

我已经在两台机器上编译了代码,它只能在其中一台机器上运行(PC1).我调试了它,发现,对于PC1和PC2,std :: string :: npos是不同的.

如果未找到任何字符,则find_last_of == 4294967295为两台计算机返回的值.

PC1:

std::string::npos == 4294967295

PC2:

std::string::npos == 18446744073709551615

更多测试:

PC1:

sizeof(size_t) == 4

PC2:

sizeof(size_t) == 8

第一台机器使用32位操作系统,第二台机器使用64位操作系统.

我应该使用什么来比较find_last_of方法返回的值,使其在两台机器上都能正常工作?

解决方法

What should I use to compare the value returned by the find_last_of
method to make it work on both machines?

std :: string :: npos,position(found)的类型应该是size_t.

常量的具体大小可以在不同的体系结构上有所不同,但这不是您的担忧.

npos is a static member constant value with the greatest possible value for an element of type size_t.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐