#include<iostream>
#include<string>
using namespace std;
int main(){
string s1="hello";
string s2=" world";
string s3=s1+s2;//两个string相加,是允许的
string s4="hello"+" word";//只有字符串字面值相加,是不允许的
cout<<s3<<endl;//正确的
cout<<s4<<endl;//是错误的
cout<<"hello "+"world"<<endl;//是错误的
cout<<s1+" world"<<endl;//是正确的
}
//一句话,和字符串字面值连接,至少有一个是string类型的。
用string::size_type类型接收string返回的size()值,string下标是从0开始的,到string::size()-1
#include<iostream>
using namespace std;
int main(){
string s="hello";
for(string::size_type i=0;i<s.size();i++){
cout<<s[i];
}
cout<<endl;
return 0;
}
C++中string可以使用c语言中的(cctype头文件中)提供的函数。