【问题标题】:std::find failed to compilestd::find 编译失败
【发布时间】:2012-12-09 01:24:29
【问题描述】:

如果我在代码中使用 std::find,我的项目将无法构建。我得到的错误如下

usr/include/c++/4.6/bits/stl_algo.h:162:4:错误:'_first 中的'operator=='不匹配。_gnu_cxx::__normal_iterator <_iterator _container>::operator* with _Iterator = char*, _Container = std::basic_string, __gnu_cxx::__normal_iterator<_iterator _container>::reference = char& == __val' /usr/include/c++/4.6/bits/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、算法和字符串。

【问题讨论】:

  • 如果类具有与泛型算法等效的方法,请使用类方法。它通常更快。在这种情况下,你最好使用std::string::find(),除非你真的需要返回一个迭代器(这通常不是使用std::string的情况)。

标签: c++ stl stdstring


【解决方案1】:

您需要搜索单个字符,因为"'" 是一个以空字符结尾的字符串,因此由字符'\0 组成,并且您当前的分隔符是std::stringstd::find 将比较字符串的元素,这些元素本身就是单个字符:

char delimiter= ':';

【讨论】:

  • 仍然会有一个错误——你根本不应该将std::string 作为第三个参数传递给std::find。通过char 那里。
  • @juanchopanza 我知道:D
猜你喜欢
  • 2017-11-15
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 2021-10-07
  • 2019-11-17
  • 1970-01-01
相关资源
最近更新 更多