string的使用(P52)

/*

声明string对象:

格式:
string 变量名;

使用字符数组对string变量进行初始化
char name[ ]="C++程序";
string s1 = name;

声明一个string对象数组,即数组中每个元素都是字符串。
string city[ ] = {"Beijing","Shanghai","Tianjin","Chongqing"};
cout<<citys[1]<<endl;
cout<<sizof(citys)/sizeof(string)<<endl;

string对象的操作:

    string对象之间可以用“<” "<=" "==" "!=" ">=" ">"运算符进行比较。大小的判定标准是按字典序进行的,而且是大小写相关的。

*/

#include <iostream>
#include <string>
using namespace std;

int main()
{
string s1, s2;
s1 = "c++程序";
s2 = s1;
string s3;
cout << "s3=" << s3 << endl;
s3 = s1 + s2;
cout << s1 + s2 << endl;
cout << "s3=" << s3 << endl;
bool b = s1 < s3;
cout << "bool=" << b << endl;
char c = s1[2];
cout << "c=" << c << endl;
cout << s1[2] << endl;
char arrstr[] = "Hello";
s3 = s1 + arrstr;
cout << s3 << endl;
system("pause");
return 0;
}

posted @ 2020-02-25 11:27  CollisionDimension  阅读(129)  评论(0)    收藏  举报