cod*_*ict 20
C
字符串等价C++
string
物c_str
.char
中a 的第一个
出现位置.string
find_first_of
例:
string s = "abc";
// call to strlen expects char *
cout<<strlen(s.c_str()); // prints 3
// on failure find_first_of return string::npos
if(s.find_first_of('a') != string::npos)
cout<<s<<" has an a"<<endl;
else
cout<<s<<" has no a"<<endl;
Run Code Online (Sandbox Code Playgroud)
注意:我给出了strlen
一个函数的例子char*
.
小智 6
令人惊讶的是,std :;字符串比C风格的字符串具有更多的功能.您可能需要find_first_of()方法.通常,如果您发现自己在C++ std :: strings上使用strxxx()函数,那么您几乎肯定会做错事.
像许多C++标准库一样,字符串类是一个复杂的野兽.为了充分利用它,你真的需要一本好的参考书.我推荐Nicolai Josuttis撰写的C++标准库.