已知一个string的对象str的内容为“we are here!”, 使用多种方法输出字符“h”-简单

源程序:

#include < iostream >

#include < functional >

#include < algorithm >

#include < string >

using namespace std;

void main()

{

  string str1("We are here!");

  cout<< str1[7]<< endl;//通过数组得到字符“h”

  string str2=str1.substr(7,1);//通过子串函数得到字符“h”

  cout<< str2<< endl;

  char *p=find(str1.begin(),str1.end(),'h');//通过find 函数得到字符“h”

  if(p)

    cout<<*p<< endl;

}

 

posted @ 2019-11-19 10:32  bobo哥  阅读(268)  评论(0编辑  收藏  举报