加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:25:40 所属栏目:百科 来源:网络整理
导读:我写了一个程序,它使用find_last_of方法在字符串中查找字符. // ...unsigned found;found = name.find_last_of(character);if (found == std::string::npos) { std::cout "NOT FOUND" std::endl;}// ... 我已经在两台机器上编译了代码,它只能在其中一台机器
我写了一个程序,它使用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读