C++面向对象思想笔记

string类

C++中的string类定义了string类型,他包含很多有用的函数,可以方便的操作字符串.

c++处理字符串的两种方法:
1. 把字符串看做是以空终结符(‘\0’)结尾的字符数组,称之为c字符串。
2. 使用string类来处理字符串。

构造一个字符串

方法1. string s = "Welcome to C++";   #需要两步:1. 创建字符串对象;2. 把对象拷贝给s。
方法2. string s("Welcome to C++");
方法3. string s;    #使用string类的无参构造函数,创建一个空字符串。

追加字符串

+append(s: string): string 将字符串s追加在当前字符串后
+append(s: string, index: int, n: int): string 将s中从下边index起的n个字符串追加在当前字符串后
+append(s: string, n: int): string 将s的前n个字符追加在当前字符串之后
+append(n: int, ch: char): string 将n个ch追加在当前字符串后

例子:


1.string s1("welcome");
  s1.append(" to C++");  #s1 now become Welcome to C++.
2.string s2("Welcome");
  s2.append(" to C and C++", 0, 5);  #s2 now beclome Welcome to C.
3.string s3("Welcome");
  s3.append(" to C and C++", 5);  #s3 now beclome Welcome to C.
4.string s4("Welcome");
  s4.append(4, 'G');   #s4 now become WelcomeGGGG.

字符串赋值

+assign(s[]: char): string 将一个字符数组或一个字符串s赋予当前字符串
+assign(s: string): string 将字符串s赋予当前字符串
+assign(s: string, index: int, n: int): string 将s中从下标index起的n个字符赋予当前字符串
+assign(s: string, n: int): string 将s的前n个字符赋予当前字符串
+assign(n: int, ch: char): string 将当前字符串赋值为ch的n次重复

例子:

string s1("Welcome");
s1.assign("Dallas");  #s1 now becomes Dallas.

string s2("Welcome");
s2.assign("Dallas, Texas", 0, 5);  #s2 now becomes Dalla.

string s3("Welcome");
s3.assign("Dallas, Texas", 5);  #s3 now becomes Dalla

string s4("Welcome");
s4.assign(4, 'G');  #s4 now becomes GGGG

函数at,clear, erase以及empty

at(index)函数提取字符串中指定位置的字符,clear()清空一个字符串, erase(index, n)删除字符串指定部分, empty()检测一个字符串是否为空

+at(index: int): char 返回当前字符串下标index处的字符
+clear(): void 清除当前字符串中所有字符
+erase(index: int, n: int): string 删除当前字符串从下标index开始的n个字符
+empty(): bool 若当前字符为空,则返回true

例子:

string s1("Welcome");
cout<3)<//return c.
cout<2,3)<//s1 is now Weme.
s1.clear();  //s1 is now empty.
cout<//s1.empty returns 1(means true).

函数length,size,capacity和c_str()

函数length(), size(), capacity()分别用来获取字符串长度,大小,分配的存储空间的大小, c_str()返回一个C字符串。

+length(): int 返回当前字符串中字符个数
+size(): int 与length()相同
+capacity(): int 返回为当前字符串分配的存储空间大小
+c_str(): int 返回当前字符串对象的C字符串
+data(): char[] 与c_str()相同

例子:

string s1("Welcome");
cout<//length is 7.
cout<//size is 7. 
cout<//capacity is 15.

s1.erase(1, 2);
cout<//length is now 5.
cout<//size is now 5.
cout<//capacity is still 15.

当字符串s1被创建时(1行),其存储容量(capacity)被设置为15。在第六航删除两个字符后,容量仍为15,但长度与大小都变为5

字符串比较

使用compare函数来进行字符串的比较,根据当前字符串大于,小于,等于另一个字符串的不同情况,分别返回大于0的值,等于0的值,小于0的值。

+compare(s: string): int 根据当前字符串大于,小于,等于另一个字符串的不同情况,分别返回大于0的值,等于0的值,小于0的值

+compare(index: int, n: int, s: string): int 比较当前字符串与子字符串s(index,…, index+n-1)

例子:

string s1("Welcome");
string s2("Welcomg");
cout<//returns -1.
cout<//returns 1.
cout<"Welcome")<//returns 0.

获取子串

substr函数获取字符串的一个子串。

+substr(index: int, n: int): string 返回当前字符串从下标index开始的n个字符组成的子串。

+substr(index: int): string 返回当前字符串从下标index开始的子串。

例子:

string s1("Welcome");
cout<0, 1)<// returns W.
cout<3)<//returns come.
cout<3, 3)<//returns com.

字符串搜索

find函数可以在字符串中搜索一个字符或者是一个子串,没有找到则返回string::npos(表示not a position)

+find(ch: char): unsigned 返回当前字符串中字符ch出现的第一个位置
+find(ch: char, index: int): unsigned 返回当前字符串中从下标index开始ch出现的第一个位置

+find(s: string): unsigned 返回当前字符串中子串s出现的第一个位置
+find(s: string, index: int): unsigned 返回当前字符串中从下标index开始s出现的第一个位置

例子:

string s1("Welcome to HTML");
cout<"co")<//returns 3.
cout<"co", 6)<//returns string::npos.
cout<'o')<//returns 4.
cout<'o', 6);  //returns 9.

字符串插入与替换

使用insertreplace函数在字符串中插入和替换一个子串。

+insert(index: int, s: string): string 将字符串s插入本字符串下标index处。
+insert(index:int, n: int, ch: char): string 将n个ch插入本字符串下标index处。
+replace(index: int, n: int, s: string): string 将本字符串从下标index开始的n个字符替换为s的内容。

例子:

string s1("Welcome to HTML");
s1.string(11, "C++ and ");
cout<//s1 becomes Welcome to C++ and HTML.

string s2("AA");
s2.insert(1, 4, 'B');
cout<//s2 becomes to ABBBBA.

string s3("Welcome to HTML");
s3.replace(11, 4, "C++");
cout<//s3 becomes Welcome to C++.

字符串运算符

运算符 描述
[] 用数组下标运算符访问字符串中字符
= 将一个字符串的内容复制到另一个字符串
+ 连接两个字符串得到新串
+= 将一个字符串追加到另一个字符串的末尾
<< 将一个字符串插入一个流
>> 从一个流提取一个字符串,分界符为空格或空终结符
==, !=, <, <=, >, >= 用于字符串比较的6个比较运算符

例子:

string s1 = "ABC";
string s2 = s1;
for (int i = s2.size() - 1; i >= 0; i--)
    cout<//The [] operator.

string s3 = s1 + "DEFG";  //The + operator.
cout<//s3 becomes ABCDEFG.

s1 += "ABC";
cout<//s1 becomes ABCABC.

s1 = "ABC";
s2 = "ABE";
cout<<(s1 == s2)<//Displays 0(means false).
cout<<(s1 != s2)<//Displays 1(means true).
cout<<(s1 > s2)<//Displays 0(means false).
cout<<(s1 >= s2)<//Displays 0(means false).
cout<<(s1 < s2)<//Displays 1(means true).
cout<<(s1 <= s2)<//Displays 1(means true).

字符串分割

经常需要从字符串中抽取单词。在这里假设单词由空格分割,可以哦使用stringstream类来完成,位于头文件中。

例子:

#include 
#include 
#include 
using namespace std;

int main()
{
    string text("Programming is fun");
    stringstream ss(text); //为字符串创建了stringstream对象

    cout<<"The words in the text are "<string word;
    while (!ss.eof())
    {
        ss>>word;
        cout<return 0;
}

对象作为函数参数

对象可以通过值或者引用传递给函数作参数,但通过引用传递更有效。

你可能感兴趣的:(c/c++)