#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int main(){
string s="zhuyan",s2;
//通过下标访问
for(int i=0;i<s.length();i++)
printf("%c",s[i]);
printf("\n");
//C++中可直接输出
cout<<s<<endl;
//通过迭代器访问
for(auto it=s.begin();it!=s.end();it++)
printf("%c",*it);
printf("\n");
//C++输入字符串
cin>>s2;
cout<<s2<<endl;
printf("%s\n",s2.c_str()); //c_str函数把string类型转换成字符数组
//字符串拼接+=
string str1="abc",str2="xyz",str3;
str3=str1+str2;
str1+=str2;
cout<<str1<<endl;
cout<<str3<<endl;
//字符串比较(根据字典序)
string a="yes",b="hello",c="offline";
if(a>b) printf("ok1\n");
if(a!=c) printf("ok2\n");
if(b<=c) printf("ok3\n");
if(str1==str3) printf("ok4\n");
//返回string的长度,length和size基本相同
printf("%d %d\n",str1.length(),str1.size());
//插入字符串
c.insert(c.find('l'),b);
cout<<c<<endl;
//string::npos是一个常数,本身值为-1,是unsigned_int类型
if(string::npos==-1)
cout<<"-1 is true."<<endl;
// cout<<string::npos<<endl;
if(string::npos==18446744073709551615)
cout<<"18446744073709551615 is also true."<<endl;
string str4="Thank you for your smile.";
string str5="you";
string str6="me";
//find函数
if(str4.find(str5)!=string::npos)
cout<<str4.find(str5)<<endl; //返回str5在str4中第一次出现的位置
if(str4.find(str5,7)!=string::npos)
cout<<str4.find(str5,7)<<endl; //从str4的7号位开始匹配str5,返回值与上相同
if(str4.find(str6)!=string::npos)
cout<<str4.find(str6)<<endl;
else
cout<<"I know there is no position for me."<<endl;
//replace函数
string str7="Maybe you will turn around.";
string str8="will not";
string str9="surely";
//str7从10号位开始,长度为4的子串替换为str8
cout<<str7.replace(10,4,str8)<<endl;
//把str7的迭代器[str7.begin(),str7.begin()+5)范围的子串替换为str9
cout<<str7.replace(str7.begin(),str7.begin()+5,str9)<<endl;
return 0;
}