前言
在前一章种我们介绍了C++中的模板的使用,这是一种泛型编程,模板的使用能让我们减少大量的相似代码,减少我们的代码量与工作量,写出更加高效简洁的代码,模板如此好用,但还是要我们先出写一个泛型类或函数,才能使用。有没有办法直接不写泛型类或函数就能使用一些常见的数据结构与算法的代码呢?答案是有的!C++给我们提供了标准模板库STL,这是一个相当重要的库,它就完美解决了上面的问题,相信学完以后你会对它爱不释手!
STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。
string类是STL中的容器,也是我们学习STL的开始。
char
类型没有字符串类型,字符串是以'\0'
结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数(如:strlen, strcmp,strcat,strcpy
等),但是这些库函数与字符串是分离开的,不太符合OOP(面向对象)的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。因此为了更加高效的使用和管理字符串,C++提出了string
类的概念,关于string
类的学习我们先学习string
类的使用,后面我们进一步深入时我们再去动手模拟实现string
类,在这里我们首先知道string
类的底层其实就是char
类型的顺序表就行了。
string类的文档介绍
从文档中我们能看到:
string类
是basic_string
模板类的一个实例,它使用char来实例化basic_string模板类。typedef basic_stringstring;
#include
以及using namespace std
;在上面的string类的文档介绍中我们可以看到string
类接近有140个不同的接口函数,在这么多的成员函数中,很多都是冗余不必要的,因此下面我只讲解最常用的接口。
在讲解这些函数接口之前,我们先来介绍npos
,npos
是string
类里面的一个静态成员变量,因此用string
类创建的所有对象访问的都是同一个npos
,其次我们来看pos的定义:
首先npos的值是被const
修饰了,其值不能更改,然后size_t
是无符号整形unsigned
的typedef
,定义将-1的值赋值给了npos,但npos是无符号整形,因此此时nops的值就是无符号整形的最大值65535。
string
类的对象另一个对象string
类的对象中的字符串的子串初始化一个对象,子串的起始位置是pos
,结束位置是pos向后的len
个字符,如果len
没有给缺省值,默认为npos
即从pos
位置后面有多少拿多少。#include
#include
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类的对象s4string s5("hello world",5); //from sequence(5) //用C风格的字符串前五个字符"hello"初始化string类的对象s5string s6(5, 'x'); //fill (6) //用5个'x'初始化string类的对象s6cout << s0 << endl;cout << s1 << endl;cout << s2 << endl;cout << s3 << endl;cout << s4 << endl;cout << s5 << endl;cout << s6 << endl;
}
#include
#include
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
#include
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 == capacitycout << s1.size() << endl;cout << s1.capacity() << endl;return 0;
}
观察会发现,经过reserve()
函数的处理后size(字符个数)没有变,但是capacity(容量)改变了。
经过shrink_to_fit()
函数处理后size(字符个数)没有变,但是capacity(容量)改变了。(虽然,shrink_to_fit()
函数能够减少空间的浪费,但是我们还是一般不缩容,因为缩容的消耗是很大的)
std::string::resize()
函数//std::string::resize()函数
#include
#include
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;
}
注意:
//string 里面关于单个元素的访问
#include
#include
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;
}
对于string类对象的遍历有以下四种方式:
operator
运算符重载我们已经学会了,我们可以利用如下代码进行遍历字符串:#include
#include
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
#include
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
#include
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
#include
using namespace std;
int main()
{string s1("hello world");for (auto& e : s1){cout << e;}cout << endl;return 0;
}
在前面已经简单的介绍了两种迭代器,(正向迭代器与反向迭代器)下面我们就把另外两种迭代器进行简单介绍补充:
const_iterator
类型的迭代器#include
#include
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
#include
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;
}
#include
#include
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
#include
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
#include
using namespace std;
int main()
{string s0("hello ");s0.push_back('!');cout << s0 << endl;
}
代码与append()
函数代码类似,不再演示
注意:
#include
#include
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
#include
using namespace std;
int main()
{string s0("hello ");s0.erase(2, 2);cout << s0 << endl;
}
#include
#include
using namespace std;
int main()
{string s0("hello ");//c-string (3)s0.replace(2, 2,"!!!!!");cout << s0 << endl;
}
#include
#include
using namespace std;
int main()
{string s0("hello ");string s1("world");s0.swap(s1);cout << s0 << endl;cout << s1 << endl;return 0;
}
#include
#include
using namespace std;
int main()
{string s0("hello");s0.pop_back();cout << s0 << endl;return 0;
}
#include
#include
using namespace std;
int main()
{string s1("hello world");const char* str1 = s1.c_str();cout << str1 << endl;return 0;
}
#include
#include
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
#include
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
#include
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
#include
using namespace std;
int main()
{string s1("hello world");int index = s1.find_first_of("wor");cout << index << endl;return 0;
}
#include
#include
using namespace std;
int main()
{string s1("hello world");string s2 = s1.substr(2, 3);cout << s2 << endl;return 0;
}
#include
#include
using namespace std;
int main()
{//string (1) string s1("hello world");string s2("iello");cout << s1.compare(s2) << endl;return 0;
}
getline的作用是读取一整行,直到遇到换行符才停止读取,期间能读取像空格、Tab等的空白符。
getline函数和cin一样,也会返回它的流参数,也就是cin,所以可以用getline循环读取带空格的字符串。
注意: getline 是非成员函数!
#include
#include
using namespace std;
int main()
{string s1;//这里我们输入"hello world!"getline(cin, s1);cout << s1 << endl;return 0;
}
本章的内容都不是很难,但是琐碎的函数与细节特别多,想要掌握好它们还需要多加练习。多去使用它们,相信你会越用越熟悉的。