我想从一个string字符串中找一个前面已经有的符号,怎么用find和rfind都无法实现啊?
例如string str="http://aaa";
    nindx1=str.find(":");     //正常返回
    nindx2=str.rfind(":");    //并不是返回-1   为什么????(不符合要求) 
如果把string换为"http://aaaaa:123"
则nindx2返回第二个:的位置      //符合要求
  
问题:在字符串只有一个":"的情况下,有办法使执行rfind时返回-1吗

解决方案 »

  1.   

    #include <iostream.h>
    #include <string>
    using namespace std;
    int main(int argc, char* argv[])
    {
        
    string str="http://aaa";
    int nindx1,nindx2;
        nindx1=str.find(":");     //正常返回
        nindx2=str.rfind(":");    //并不是返回-1   为什么????(不符合要求) 
    cout<<nindx1<<"   "<<nindx2;
        return 0;
    }
    返回值都是4
    vc6
      

  2.   

    #include <iostream.h>
    #include <string>
    using namespace std;
    int main(int argc, char* argv[])
    {
        
    string str="http://aaa";
    int nindx1,nindx2;
        nindx1=str.find(":");     //正常返回
        nindx2=str.rfind(":");    //并不是返回-1   为什么????(不符合要求) 
    cout<<nindx1<<"   "<<nindx2;
    system("pause");
        return 0;
    }
    4,4
    //dev c++