c++ - 错误 : ISO C++ forbids comparison between pointer and integer

标签 c++

这里有一些代码,我想在字符串中找到所需的子字符串 block :

#include <algorithm>
#include <string> 
using namespace std;

...
unsigned int stage = 3;
string::iterator iter = s.begin();
string::iterator iter_next = s.end();

for (unsigned int i=0; i<stage; i++) {

    iter = std::find(iter, s.end(),"#STAGE");

}
    iter_next = std::find(iter, s.end(), "#STAGE");

string sub = string(iter, iter_next);
...

我选择返回迭代器而不是索引。但是,调试给我这个:

error: ISO C++ forbids comparison between pointer and integer. 

c:\qtsdk\mingw\bin\..\lib\gcc\mingw32\4.4.0\include\c++\bits\stl_algo.h:-1: 
In function '_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, 
const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator =   
__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, _Tp = char [7]]':
c:\qtsdk\mingw\bin\..\lib\gcc\mingw32\4.4.0\include\c++\bits\stl_algo.h:4224: instantiated  
from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = 
__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >, _Tp = char [7]]'

当我使用索引方法时,一切都很好。我明白这一定是一个简单的错误,但我希望有人能指出。

最佳答案

iter = std::find(iter, s.end(),"#STAGE");

这是错误的。您无法使用 std::findstring 中找到 string。只有字符。

你可以简单地使用

size_t pos = s.find("#STAGE");

并创建迭代器

iter = s.begin() + pos;

如果 pos 不等于 std::string::npos

关于c++ - 错误 : ISO C++ forbids comparison between pointer and integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16335277/

相关文章:

c++ - 如何在多个 QT 图形 View 小部件之间同步移动

c++ - 获取迭代器在基类型 "intrusive doubly linked list"的指针字典中的位置

c++ - 想要在 C++ 中创建一个存储 Nx3 值的 2D 指针

c++ - 如何加快 C++ 链接时间

c++ - 在 CentOS 7 上将 Clang 与 libc++ 结合使用时缺少符号(没有 C++ ABI 库?)

c++ - 为什么 Microsoft 的 C/C++ 编译器允许带有逗号分隔表达式的 if 语句?

c++ - 如何清除 C++ 结构化数组中的数据?

C++从具有多个分隔符的文件中读取矩阵

c++ - 构造函数采用参数时的GTest夹具?

c++ - 需要查找数组中内容的个数