STL之string

一,字符串的概念

1.字符串与字符指针的比较

  • string是STL封装的一个类,char *是一个指向字符的指针,string是对char *的封装,是一个char *的容器。
  • string不需要考虑内存的释放和越界,string会管理char *的内存,在构建,赋值,销毁等操作都是由string自己完成的。
  • string提供了一些列的成员函数,简化了我们对字符串的操作。

二,字符的构造函数

1.默认无参构造函数

string str; // 构造一个空字符串

2.有参构造函数

//1. 用字面量字符串初始化string
string str1("HelloString");
//2. 用n个字符'A'初始化string
string str2(10,'A');

3.拷贝构造函数

// 构造一个和str一样的字符,如string str2 = str1或者string str2(str1);
string str(const string& str)

4.析构函数

  string的析构函数用来释放分配的内存。

三,操作符重载函数

1.赋值操作符(=)

// 定义空字符串str1
string str1;
// 定义字符串str2
string str2("HelloWorld");
// 将字符串str2赋值给str1
str1 = str2

2.左移操作符(<<)

// 定义字符串str
string str("HelloWorld");
// 输出字符串
cout << str <<endl;

3.右移操作符(>>)

// 定义空字符串
string str;
// 标准输入到字符串str
cin >> str;
// 输出字符串
cout << str << endl;

4.数组下标操作符([])

// 定义字符串str
string str("HelloWorld");
// 获取第5个字符
char c5 = str[5];
cout << c5 << endl;
// 将第5个字符改为'A'
str[5] = 'A';
cout << str << endl;

5.加法运算符

// 定义空字符串str1
string str1;
// 定义字符串str2
string str2("Hello");
// 将字符串str2和"World"相加并赋值给str1
str1 = str2 + "World";
// 输出str1
cout << str1 << endl;

6.加法赋值运算符(+=)

// 定义空字符串str1
string str1("Hello");
// 定义字符串str2
string str2("World");
// 将字符串str1和str2相加
str1 += str2;
// 输出str1
cout << str1 << endl;

四,常用成员函数

1.存取字符

// 定义空字符串str
string str("HelloWorld");
// 获取第5个字符
char c5 = str.at(4);
cout << c5 << endl;
// 设置第5个字符为'A'
str.at(4) = 'A';
cout << str << endl;

2.获取字符串的字符指针

// 定义字符串str
string str("HelloWorld");
// 获取字符串str的字符指针
const char * ptr = str.c_str();
// 输出
cout << ptr << endl;

3.获取字符串的长度

// 定义字符串str
string str("HelloWorld");
// 获取字符串的长度
int len = str.length();
// 输出字符串长度
cout << "len = " << len << endl;

4.判断字符串是否为空

// 定义字符串str
string str("HelloWorld");
// 判断字符串是否为空
bool isEmpty = str.empty();
// 输出
cout << "isEmpty = " << isEmpty << endl;

5.字符串的比较

// 定义字符串str1
string str1("HelloWorld");
// 定义字符串str2
string str2("HelloWorld");
// 比较两个字符串:相等(0),str1>str2(1),str1<str2(-1)
bool result = str1.compare(str2);
// 输出
cout << "result = " << result << endl;

6.字符串的子串

// 定义字符串str
string str("HelloWorld");
// 截取从2位置开始的字符,截取5个
str = str.substr(2, 5);
// 输出str
cout << "substr = " << str << endl;

7.字符串的查找

string str("HelloWorld");
/*
字符串的正向查找
    find(char ch,int pos):返回字符ch的实际位置,从pos位置开始往后查找,没有返回-1
    find(char * str,int pos):返回字符串str的首字符位置,从pos位置开始往后查找,没有返回-1
*/
int pos1 = str.find('W', 3);
cout << "字符'W'出现的位置:" << pos1 << endl;
int pos2 = str.find("World", 3);
cout << "字符串\"World\"出现的位置:" << pos2 << endl;
/*
字符串的反向查找
    rfind(char ch,int pos):返回字符ch的实际位置,从pos位置开始往前查找,没有返回-1
    rfind(char * str,int pos):返回字符串str的首字符位置,从pos位置开始往前查找,没有返回-1
*/
int pos3 = str.rfind('o', 5);
cout << "字符'o'出现的位置:" << pos3 << endl;
int pos4 = str.rfind("World", 3);
cout << "字符\"World\"出现的位置:" << pos4 << endl;

8.字符串的替换

// 定义字符串str
string str("HelloWorld");
// 替换:删除从第2个字符串开始后面的5个字符,然后再插入字符串"Test"
str.replace(2, 5, "Test");
// 输出
cout << str << endl;

9.字符串的删除

// 定义字符串str
string str("HelloWorld");
// 删除字符串的从第2个位置开始的5个字符
str = str.erase(2, 5);
// 输出
cout << str << endl;

10.字符串的插入

// 定义字符串str
string str("HelloWorld");
// 在第2个位置插入字符串"Test"
str.insert(2, "Test");
// 在5个位置插入10个字符'A'
str.insert(5, 10, 'A');
// 输出
cout << str << endl;

11.字符串的赋值

// 定义字空字符串str1
string str1;
// 定义字符串str2
string str2("HelloWorld");
// 将字符串str2的从第5个位置后的5个字符赋值给str1
str1.assign(str2, 5, 5);
// 输出
cout << str1 << endl;

12.字符串的追加

// 定义字符串str1
string str1("HelloWorld");
string str2("GoodMoring");
// 给str1追加字符串"Good"
str1.append("Good");
// str1追加str2的从第4个开始的6个字符
str1.append(str2, 4, 6);
// str1追加10个字符'A'
str1.append(10, 'A');
// 输出
cout << str1 << endl;

13.把string拷贝到char *指向的内存空间

// 定义字符串str
string str("HelloWorld");
// 定义字符数组
char buf[256] = { 0 };
// 将str的从2开始的字符拷贝5个到buf中
str.copy(buf, 5, 2);
// 输出buf
cout << "buf = " << buf << endl;

 

posted @ 2017-01-16 22:38  MetalSteel  阅读(964)  评论(0编辑  收藏  举报