《C++面向对象程序设计》课程笔记 lessen8 string 类

1. string 类

1 string 对象的初始化

  • string 类是模板类:
typedef basic_string <char> string; 
  •  使用 string 类要包含头文件 <string>
  • string 对象的初始化:
string s1("MARCH");
string s2 = "APRIL";
string s3(8,'x');
  •  错误的初始化方法:(不能通过一个字符和一个整数来初始化 sstring)
string error1 = 'c';
string error2('u');
string error3 = 22;
string error4(8);
  •  可以将字符赋值给 string 对象。但不能用字符初始化 string 对象。
string s;
s = 'n';
  • string 对象的长度用成员函数 length() 读取 
string s("hello");
cout << s.length() << endl;
  •  string 支持流读取运算符
string stringObject;
cin >> stringObject;
  •  string 支持 getline 函数
string s;
getline(cin , s);

 2 string 对象的赋值和连接

  • 用 “=” 赋值 
string s1("cat");
string s2;
s2 = s1;
  •  用 assign 成员函数复制
string s1("cat");
string s3;
s3.assign(s1);
  •  用 assign 成员函数部分复制
string s1("catpig");
string s3;
s3.assign(s1,1,3);
//从 s1 中下标为1的字符开始复制3个字符
  • 单个字符复制 
s2[5] = s1[3] = 'a';
  • 逐个访问 string 对象中的字符
string s1("Hello");
for(int i=0;i<s1.length();i++)
	cout << s1.at(i) << endl;

 成员函数 at 会做范围检查,如果超出范围,会抛出 out_of_range 异常,而下标运算符 [ ] 不做范围检查。

  • 用 “+” 运算符连接字符串 
string s1("good");
string s2("morning!");
s1 += s2;
cout << s1;
  •  用成员函数 append 连接字符串
string s1("good");
string s2("morning!");
s1.append(s2);
cout << s1;
s2.append(s1,3,s1.size()); //s1.stze(),s1字符数
//下标为3开始,s1,size()个字符,如果字符串内没有足够字符,则复制到字符串最后一个字符
cout << s2;

 3 比较 string 对象

  • 用关系运算符比较 string 大小:==,>,>=,<,<=,!= 。返回值都是 bool 类型,成立返回 true,否则返回 false。
  • 用compare 成员函数比较 string 对象的大小。
string s1("hello"),s2("hello"),s3("hell");
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1,2,s3,0,3);//s1:1-2; s3:0-3
int f5 = s1.compare(0,s1.size(),s3); //s1:0-end
cout << f1 << endl << f2 << endl << f3 << endl << f4 << endl << f5 << endl;

//输出:0 // hello == hello
//      1 // hello > hell
//	   -1 // hell  < hello
//	   -1 // el < hell
//   	1 // hello > hell

4 string 对象子串

  • 成员函数 substr
string s1("hello world");
string s2;
s2 = s1.substr(4,5); //下标为4开始的5个字符
cout << s2 <<endl;

 5 交换 string 对象

  • 成员函数 swap
string s1("hello world");
string s2("really");
s1.swap(s2);
cout << s1 <<endl;
cout << s2 <<endl;

 6 寻找 string 对象中的字符

  •  成员函数 find()
string s1("hello world");
s1.find("lo");

在 s1 中从前向后查找 “lo” 第一次出现的地方,如果找到,返回 “lo” 开始的位置,即 “l” 所在的位置下标。如果找不到,返回 string::npos ( string 中定义的静态常量)

  • 成员函数 rfind()
string s1("hello world");
s1.rfind("lo");

在 s1 中从后向前查找 “lo” 第一次出现的地方,如果找到,返回 “lo” 开始的位置,即 “l” 所在的位置下标。如果找不到,返回 string::npos ( string 中定义的静态常量)

  • 成员函数 find() 还可以指定查找开始的位置
string s1("hello worlld");
cout << s1.find("ll",1) << endl;
cout << s1.find("ll",2) << endl;
cout << s1.find("ll",3) << endl;
//分别从下标1,2,3开始查找 "ll"。
  •  成员函数 find_first_of()  (寻找 string 中的字符)
string s1("hello world");
s1.find_first_of("abcd");

在 s1 中从前向后查找 “abcd” 中任何一个字符第一次出现的地方,如果找到,返回找到字母的位置。如果找不到,返回 string::npos。

  • 成员函数 find_last_of()  (寻找 string 中的字符)
string s1("hello world");
s1.find_last_of("abcd");

 在 s1 中查找 “abcd” 中任何一个字符最后一次出现的地方,如果找到,返回找到字母的位置。如果找不到,返回 string::npos。

  •  成员函数 find_first_not_of()  (寻找 string 中的第一个不在 “abcd” 中的字符)
  •  成员函数 find_last_not_of()  (寻找 string 中的最后一个不在 “abcd” 中的字符)

7 删除 string 对象中的字符

  • 成员函数 erase() 
string s1("hello world");
s1.erase(5); //去掉下标5及之后的字符
  •  成员函数 replace()
string s1("hello world");
s1.replace(2,3."haha"); //将s1中下标2开始的3个字符换成 "haha"

 8 在 string 对象中插入字符

  • 成员函数 insert() 
string s1("hello world");
string s2("show insert");
s1.insert(5,s2);//将s2插入s1下标5的位置
cout << s1 << endl;
s1.insert(2,s2,5,3);
//将s2中下标5开始的3个字符插入s1下标2的位置
cout << s1 << endl;

9 转换成C语言式 char * 字符串

  • 成员函数 c_str()
string s1("hello world");
printf("%s\n",s1.c_str());
//s1.c_str()返回传统的 const char * 类型字符串,且该字符串以 '\0' 结尾

猜你喜欢

转载自blog.csdn.net/sinat_35483329/article/details/85229754