笔记
#include <string>
#include <cstdio>
using namespace std;
int main(){
char buf[101];//申请一个字符
//scanf("%s",buf);//只能读取一个单词
fgets(buf,100,stdin); //读取一整行,包括换行符
string str = buf; //用一个字符数组生成一个c++风格的字符串
// printf("length of str = %u\n",str.size());
str.erase(5);//删除下标为5的字符 str.erase(str,size()-1)
str.clear();//清空操作
str = str + "world"; // +, 在C++风格的字符串中表示连接操作
printf("str = %s\n",str.c_str()); //将C++风格的字符串转换成字符数组
// for(unsigned int i=0;i<str.size();++i){
// printf("%c\n",str[i]); //打印字符
// }
//另一种方法
// for(string::iterator it =str.begin();it != str.end();++it)
// //++it 更改迭代器的指向,到下一个元素
// printf("%c\n",*it);//*it访问地址元素
/*
* find 查找字符或子串 ,如果找到了,返回匹配内容的起点下标;否则,返回string::npos
*/
}
#include <cstdio>
#include <string>
using namespace std;
int main(){
string str = "howareyou";
int pos = str.find("arre");
//判断一下are有没有在str当中出现过
if(pos != string::npos){
//有匹配
printf("Found,pos = %d\n",pos);
}
else{
//没有匹配项
printf("Not Found\n");
}
}