1

文字列内の目的の部分文字列ブロックを見つけたいコードを次に示します。

#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]]'

index メソッドを使用した場合はすべて問題ありません。単純な間違いであることは理解していますが、誰かが指摘できることを願っています。

4

3 に答える 3

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

それは間違っています。stringではstring、 を使用して見つけることができませんstd::find。キャラクターのみ。

簡単に使用できます

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

そしてイテレータを作成する

iter = s.begin() + pos;

posと等しくない場合std::string::npos

于 2013-05-02T10:06:44.107 に答える
1

イテレータはstd::string、文字列ではなく文字を繰り返し処理します。

文字列で検索したい場合は"#STAGE"、単に次を使用します。

s.find("#STAGE");
于 2013-05-02T10:09:09.963 に答える