我寫了一個程序,它使用find_last_of
方法找到字符串中的一個字符。不同大小的std :: string :: npos
// ...
unsigned found;
found = name.find_last_of(character);
if (found == std::string::npos) {
std::cout << "NOT FOUND" << std::endl;
}
// ...
我已經在兩臺機器上編譯了代碼,它只在其中一個機器上工作(PC1)。我調試過它並發現,std :: string :: npos對於PC1和PC2是不同的。
如果沒有找到字符,那麼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
方法返回的值以使它在兩臺機器上都能正常工作?
爲什麼它不工作? –
將它與'std :: string :: npos'進行比較。 – juanchopanza
@LuchianGrigore如果'found',即'unsigned'的類型太小而不能容納'std :: string :: npos',那麼它將不起作用。 – hvd