c++ - string.find() doesn't return -1 -


the code below simple. know, if string::find() didn't find matches returns -1. reasons code below doesn't work. everytime run code endless loop. thank help!

#include <string> #include <iostream> using namespace std;  int main() {     string text;     text = "asdasd ijk asdasd";     string toreplace = "ijk";     cout<<text<<endl;     int counter = 0;     while ( text.find(toreplace) != -1)         counter++;      cout<<counter<<endl;      system("pause"); } 

aside other answers correct, wanted add while loop have produced endless loop anyway. example:

while(text.find(toreplace) != std::string::npos)     counter++; 

will endless loop because keep trying find toreplace string in text , find (that's because find starts beginning of string each time). not intended.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -