2016-11-03 131 views
-9

我試圖在表示POSIX系統上的文件路徑的字符串中搜索'..'的存在。我使用std :: string.find(「..」),它似乎找到了正確的索引,但是在布爾表達式中沒有正確評估。請給我建議我是如何完成這個的?我在我的代碼中有一個錯誤。請告訴我

#include <string> 
#include <stdio.h> 

int main(int argc, char *argv[]) { 
    std::string a = "abcd"; 

    int apos = a.find(".."); 

    bool test1 = a.find("..") >= 0; 
    bool test2 = apos >= 0; 

    if (test1) { 
     printf("TEST1 FAILED: %ld >= 0!\n", a.find("..")); 
    } 
    if (test2) { 
     printf("TEST2 FAILED %d >= 0!\n", apos); 
    } 
} 
+3

有一個調試器的原因:請使用它! –

+0

我收到一條警告:「警告:無符號表達式的比較> = 0始終爲真」。 – tadman

回答

2

這不是測試與0相關,而是與std::string::npos相關。看看find上的文檔:

bool found = a.find('..') != std::string::npos; 
相關問題