65,189
社区成员




size_type find(const basic_string<charT,traits,Allocator>& str,
size_type pos = 0) const;
Effects: Determines the lowest position xpos, if possible, such that both of the following conditions
obtain:
— pos <= xpos and xpos + str.size() <= size();
— at(xpos+I) == str.at(I) for all elements I of the string controlled by str.
Returns: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
Notes: Uses traits::eq().
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "abcde";
string s2 = "cd";
string s3 = "dc";
string::size_type i1 = s1.find(s2);
string::size_type i2 = s1.find(s3);
cout << i1 << endl;
cout << i2 << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s="golden global view";
string r="new";
if (s.find(r) < s.length())
{
cout<<"s中包含r"<<endl;
}
else
{
cout<<"s中不包含r"<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
string s="golden global view";
string r="global";
const char *show;
show=strstr(s.c_str(),r.c_str());//返回指向第一次出现r位置的指针,如果没找到则返回NULL。
cout<<show<<endl;
return 0;
}