2012-12-21 38 views
0

如果我在代碼中使用std :: find,我的項目無法生成。我得到的錯誤如下:std :: find無法編譯

usr/include/C++/4.6/bits/stl_algo.h:162:4:錯誤:首先在'_ '中不匹配'operator =='。 _gnu_cxx :: __ normal_iterator < _Iterator,_container> ::運算符*與_Iterator =字符*,_container =性病:: basic_string的,__gnu_cxx :: __ normal_iterator < _Iterator,_container> ::參考=炭& == __val」 的/ usr /包括/ C++/4.6 /比特/ stl_algo.h:162:4:注意:候選是:

下面是我的代碼..

 std::string lValueCmd = ""; 
     std::string lModeType = ""; 
     std::string lAPModeCmd = "sudo iwconfig /sbin/wlan0 | grep -i 'Mode:' | awk '{print $1}'"; 
     // function to retrive values from linux command and store it in lValueCmd 
     lResult = RequestCmdOutput(lAPModeCmd, lValueCmd)  ; // function to retrive values from linux command and store it in lValue Cmdd 

    // std::cout << " the string is " << lValueCmd << std::endl;// debug 
     std::string delimiter= ":"; 

     std::string::iterator pos; 
     pos= std::find(lValueCmd.begin(), lValueCmd.end(), delimiter); // no error if I comment this line. 

的頭文件中,我使用的是iostream的,算法和字符串。

+1

如果類具有一個通用的算法等效的方法,使用類方法。它通常更快。在這種情況下,除非你確實需要返回一個迭代器(通常不會使用'std :: string'),否則你最好使用'std :: string :: find()'。 – Gorpik

回答

4

你需要搜索一個字符,因爲"'"是一個空結束的字符串,所以由字符'\0的,而當前的分隔符是一個std::stringstd::find將比較字符串的元素,其本身單個字符:

char delimiter= ':'; 
+0

仍然會有一個錯誤 - 你不應該將std :: string作爲第三個參數傳遞給'std :: find'。在那裏通過'char'。 –

+0

@billz這就是我的意思,複製和粘貼錯誤。謝謝! – juanchopanza

+0

@juanchopanza我知道:D – billz