不自己写函数,有没有现成的方法可以调用呢?

解决方案 »

  1.   

    这是STL里的吧?
    string里包含了basic_string类,
    调用find方法:
    basic_string::find
    size_type find(E c, size_type pos = 0) const;
    size_type find(const E *s, size_type pos = 0) const;
    size_type find(const E *s, size_type pos, size_type n) const;
    size_type find(const basic_string& str, size_type pos = 0) const;If it succeeds, it returns the position where the matching subsequence begins. Otherwise, the function returns npos.
    (basic_string::npos
    static const size_type npos = -1;)
    如果成功找到返回位置,否则返回-1.
      

  2.   

    This sample code searches for every instance of the string "cat" in a given string and counts the total number of instances:string input;
    int i = 0;
    int cat_appearances = 0;getline(cin, input, '\n');i = input.find("cat", 0);
    for(; i != string::npos; i = input.find("cat", i))
    {
        cat_appearances++;
        i++;  // Move past the last discovered instance to avoid finding same
              // string
    }
    cout<<cat_appearances;
      

  3.   

    string str;
    str=L"sdfsadf";
    int pos =str.find(L"sa");
    pos=-1 没有找到,pos大于0就是找到的位置