前言
在前一章种我们介绍了C++中的模板的使用,这是一种泛型编程,模板的使用能让我们减少大量的相似代码,减少我们的代码量与工作量,写出更加高效简洁的代码,模板如此好用,但还是要我们先出写一个泛型类或函数,才能使用。有没有办法直接不写泛型类或函数就能使用一些常见的数据结构与算法的代码呢?答案是有的!C++给我们提供了标准模板库STL,这是一个相当重要的库,它就完美解决了上面的问题,相信学完以后你会对它爱不释手!
C++标准模板库STL (一)
一、STL的基础介绍
1. 什么是STL
STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。
2. STL的版本
- 原始版本
Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本,本着开源精神,他们声明允许任何人任意运用、拷贝、修改、传播、商业使用这些代码,无需付费。唯一的条件就是也需要向原始版本一样做开源使用。 HP 版本–所有STL实现版本的始祖。 - P. J. 版本
由P. J. Plauger开发,继承自HP版本,被Windows Visual C++采用,不能公开或修改,缺陷:可读性比较低,符号命名比较怪异。 - RW版本
由Rouge Wage公司开发,继承自HP版本,被C+ + Builder 采用,不能公开或修改,可读性一般。 - SGI版本
由Silicon Graphics Computer Systems,Inc公司开发,继承自HP版 本。被GCC(Linux)采用,可移植性好,可公开、修改甚至贩卖,从命名风格和编程风格上看,阅读性非常高。如果你要深入学习STL想要阅读部分源代码,可以主要参考这个版本。
3. STL的六大组件
二、string类的介绍
string类是STL中的容器,也是我们学习STL的开始。
1. 为什么学习string类
- C语言中的字符串
C语言中基本类型有char
类型没有字符串类型,字符串是以'\0'
结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数(如:strlen, strcmp,strcat,strcpy
等),但是这些库函数与字符串是分离开的,不太符合OOP(面向对象)的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。
因此为了更加高效的使用和管理字符串,C++提出了string
类的概念,关于string
类的学习我们先学习string
类的使用,后面我们进一步深入时我们再去动手模拟实现string
类,在这里我们首先知道string
类的底层其实就是char
类型的顺序表就行了。
2. 标准库中的string类
string类的文档介绍
从文档中我们能看到:
string类
是basic_string
模板类的一个实例,它使用char来实例化basic_string模板类。- string在底层实际是:basic_string模板类的别名,
typedef basic_string<char>string;
- 不能操作多字节或者变长字符的序列。(如英文使用ASCII编码使用一个字节表示一个字母,汉字如果采用GBK编码占用两个字节表示一个汉字)
- 处理多字节或者变长字符的序列要用到以下三个类:
- 在使用string类时,必须包含
#include<string>
以及using namespace std
;
三、string类的常用接口说明
在上面的string类的文档介绍中我们可以看到string
类接近有140个不同的接口函数,在这么多的成员函数中,很多都是冗余不必要的,因此下面我只讲解最常用的接口。
在讲解这些函数接口之前,我们先来介绍npos
,npos
是string
类里面的一个静态成员变量,因此用string
类创建的所有对象访问的都是同一个npos
,其次我们来看pos的定义:
首先npos的值是被const
修饰了,其值不能更改,然后size_t
是无符号整形unsigned
的typedef
,定义将-1的值赋值给了npos,但npos是无符号整形,因此此时nops的值就是无符号整形的最大值65535。
1. string类对象的常见构造函数
- 缺省构造函数,用空字符串初始化string对象
- 拷贝构造函数,用
string
类的对象另一个对象 - 用
string
类的对象中的字符串的子串初始化一个对象,子串的起始位置是pos
,结束位置是pos向后的len
个字符,如果len
没有给缺省值,默认为npos
即从pos
位置后面有多少拿多少。 - 用C风格的字符串初始化string类的对象
- 用C风格的字符串前n个字符初始化string类的对象
- 用n个字符初始化string类的对象
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello world!");
string s1; //default (1) 缺省构造函数
//用空字符串初始化s1对象
string s2(s0); //copy (2) 拷贝构造函数
//用string类的对象s0初始化s2对象
string s3(s0, 6, 5); //substring (3) 用string类的对象s0的字符串的子串"world"初始化s3
//这里pos参数表示位置,len表示的是个数
string s4("hello string"); //from c-string (4)
//C风格的字符串初始化string类的对象s4
string s5("hello world",5); //from sequence(5)
//用C风格的字符串前五个字符"hello"初始化string类的对象s5
string s6(5, 'x'); //fill (6)
//用5个'x'初始化string类的对象s6
cout << s0 << endl;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
cout << s6 << endl;
}
2. string类对象的容量操作
- 我们先来看看不涉及扩容的函数
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << s1.capacity() << endl;
cout << s1.max_size() << endl;
s1.clear();
cout << s1.empty() << endl;
return 0;
}
- 我们再来看看涉及扩容的函数
std::string::reserve()
函数与std::string::shrink_to_fit()
函数
//std::string::reserve()函数
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.reserve(200); //提前把空间扩容到200字节,但不初始化
//由于string类内部的一些原因(如:对齐等原因),虽然向系统申请的的200字节,
//但不一定就是200字节,总之系统申请的空间会 >= 你给的指定值
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.shrink_to_fit(); //缩减容量让 size == capacity
cout << s1.size() << endl;
cout << s1.capacity() << endl;
return 0;
}
观察会发现,经过reserve()
函数的处理后size(字符个数)没有变,但是capacity(容量)改变了。
经过shrink_to_fit()
函数处理后size(字符个数)没有变,但是capacity(容量)改变了。(虽然,shrink_to_fit()
函数能够减少空间的浪费,但是我们还是一般不缩容,因为缩容的消耗是很大的)
- 我们再来看另一个扩容有关的函数:
std::string::resize()
函数
此函数有两个版本,这两个版本构成函数重载。
第一个参数是:调整后容量的大小,第二个参数是用什么字符来初始化新申请的空间中多余的没有被初始化部分空间,如果不给此参数,就默认用’\0’来初始化。
如果第一个参数给的没有原来的大,那就是缩容,里面的字符串就变成了只保留原先字符串从0位置开始到n位置的字符串。
//std::string::resize()函数
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.resize(30); //提前把空间与字符数量扩容到50字节,但不初始化
//由于string类内部的一些原因(如:对齐等原因),虽然向系统申请的的50字节,
//但不一定就是50字节,总之系统申请的空间会 >= 你给的指定值
cout << s1.size() << endl;
cout << s1.capacity() << endl;
return 0;
}
注意:
- size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
- clear()只是将string中有效字符清空,不改变底层空间大小。
- resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用’\0’来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
- reserve(size_t n=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。
3. string类对象的单个元素访问
//string 里面关于单个元素的访问
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world!");
char ch1 = s1[1]; //operator[]的运算符重载
char ch2 = s1[2]; //operator[]的运算符重载
char ch3 = s1.at(6);
cout << ch1 << endl;
cout << ch2 << endl;
cout << ch3 << endl;
cout << "---------------" << endl;
cout << s1.front() << endl;
cout << s1.back() << endl;
cout << "---------------" << endl;
//捕获异常
try
{
s1.at(100);
}
catch (const std::exception& e)
{
//显示异常原因
cout << e.what() << endl;
}
return 0;
}
4. string类对象的元素的遍历
对于string类对象的遍历有以下四种方式:
- 第一种
operator
运算符重载我们已经学会了,我们可以利用如下代码进行遍历字符串:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
for (int i = 0; i < s1.size(); ++i)
{
cout << s1[i];
}
cout << endl;
return 0;
}
- 第二种
begin
end
就需要借助STL里面的迭代器了,iterator
是迭代器类型,是一种自定义类型,现在你可以暂时把它理解为类似于指针的类型。
想要遍历字符串我们就应该这样遍历:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it;
++it;
}
cout << endl;
return 0;
}
- 第三种是
rbegin
rend
它们其实也是迭代器,不同的是,它们是反向迭代器,所以我们应该这样写才能遍历string对象。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
string::reverse_iterator it = s1.rbegin();
while (it != s1.rend())
{
cout << *it;
++it;
}
cout << endl;
return 0;
}
- 第三种是范围
for
,在前面的文章中我们也谈论过范围for,利用范围for,也可以遍历string类(范围for的底层实现就是迭代器)。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
for (auto& e : s1)
{
cout << e;
}
cout << endl;
return 0;
}
在前面已经简单的介绍了两种迭代器,(正向迭代器与反向迭代器)下面我们就把另外两种迭代器进行简单介绍补充:
const_iterator
类型的迭代器
此类型的迭代器,不能通过迭代器来改变指向的对象里的内容。
#include<iostream>
#include<string>
using namespace std;
int main()
{
const string s1("hello world");
string::const_iterator it = s1.begin();
while (it != s1.end())
{
(*it)++; //此行为非法!
it++; //此行为合法!
}
return 0;
}
const_reverse_iterator
类型的迭代器
此类型的迭代器是反向迭代器,并且不能通过迭代器来改变指向的对象里的内容。
#include<iostream>
#include<string>
using namespace std;
int main()
{
const string s1("hello world");
string::const_reverse_iterator it = s1.rbegin();
while (it != s1.rend())
{
(*it)++; //此行为非法!
it++; //此行为合法!
}
return 0;
}
5.string类对象的修改操作
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello ");
string s1("world");
s0 += s1;
cout << s0 << endl;
s0 += " string";
cout << s0 << endl;
s0 += '!';
cout << s0 << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello ");
string s1("world");
//substring (2)
s0.append(s1, 0, 3);
cout << s0 << endl;
//buffer (4)
s0.append("string", 3);
cout << s0 << endl;
//fill (5)
s0.append(5, '!');
cout << s0 << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello ");
s0.push_back('!');
cout << s0 << endl;
}
代码与append()
函数代码类似,不再演示
注意:
- 在string尾部追加字符时,s.push_back( c ) / s.append(1, c) / s += 'c’三种的实现方式差不多,一般
情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。 - 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello ");
string s1("world");
//string (1)
s0.insert(0, s1);
cout << s0 << endl;
//c-string (3)
s0.insert(3, "!!!!!!");
cout << s0 << endl;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello ");
s0.erase(2, 2);
cout << s0 << endl;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello ");
//c-string (3)
s0.replace(2, 2,"!!!!!");
cout << s0 << endl;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello ");
string s1("world");
s0.swap(s1);
cout << s0 << endl;
cout << s1 << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s0("hello");
s0.pop_back();
cout << s0 << endl;
return 0;
}
6.string类对象的一些其他操作
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
const char* str1 = s1.c_str();
cout << str1 << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
/*const char* str1 = s1.c_str();
cout << str1 << endl;*/
char ch = 'a';
char* str1 = &ch;
s1.copy(str1, 3, 2); // 注意此函数不会拷贝完后追加字符串,打印字符串时要小心越界。
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
//buffer (3)
string s1("hello world");
const char* str = "world";
int index = s1.find(str, 0, 3);
cout << index << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
//c-string (2)
string s1("hello world");
const char* str = "world";
int index = s1.rfind(str);
cout << index << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
int index = s1.find_first_of("wor");
cout << index << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello world");
string s2 = s1.substr(2, 3);
cout << s2 << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
//string (1)
string s1("hello world");
string s2("iello");
cout << s1.compare(s2) << endl;
return 0;
}
7.getline函数
getline的作用是读取一整行,直到遇到换行符才停止读取,期间能读取像空格、Tab等的空白符。
getline函数和cin一样,也会返回它的流参数,也就是cin,所以可以用getline循环读取带空格的字符串。
注意: getline 是非成员函数!
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
//这里我们输入"hello world!"
getline(cin, s1);
cout << s1 << endl;
return 0;
}
四、结语
本章的内容都不是很难,但是琐碎的函数与细节特别多,想要掌握好它们还需要多加练习。多去使用它们,相信你会越用越熟悉的。