快速认识STL及string类
STL的简介
STL(standard template library标准模板库)是C++标准库的重要组成部分,包含一系列预定义的模板类和函数,用于实现常见的数据结构和算法。
STL的版本
STL最初由Alexander Stepanov设计开发,他在20世纪70年代末至80年代初提出了泛型编程(Generic Programming)的思想,并着手开发STL。Stepanov的设计理念是创建一组高效、可复用的算法和数据结构,通过抽象化数据类型,使得程序员能够编写与数据类型无关的代码。
- 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的六大组件
string类
std::string 是 C++ 标准模板库中用于表示和处理字符串的一个类,位于头文件 中。它封装了字符数组的操作,提供了方便、安全且高效的字符串处理功能,是 C++ 中处理文本数据的核心工具之一
string类的常用接口说明
== string类对象的常见构造==
- string ();
构造空的string类对象(默认构造函数default constructor) - string(const string& str);
拷贝构造函数
#include<iostream>
using namespace std;
int main()
{
string s1("哈哈");
cout << s1 << endl;
string s2(s1);
cout << s2 << endl;
return 0;
}
- string (const string& str, size_t pos, size_t len = npos);
从str中下标为pos的位置(下标位置从零开始)处开始,复制其后长度为len的字符;当len的长度超过了字符串的长度,会从pos开始将其全部复制;如果没有给出len的长度,就会从pos位置开始将其全部复制
#include<iostream>
using namespace std;
int main()
{
string s1("nothing is impossible");
cout << s1 << endl;
string s2(s1,8,2);
string s3(s1,8,100);
cout << s2 << endl;
cout<< s3 << endl;
return 0;
}
- string (const char s);*
用C语言的格式来构造string类的对象 - string (const char s, size_t n);*
从 s 指向的字符数组中复制前 n 个字符到新创建的 std::string 对象中。
int main()
{
string s1("abcdefghigk", 6);
cout << s1 << endl;
return 0;
}
- string (size_t n, char c);
string类对象中包含n个字符c
#include<iostream>
using namespace std;
int main()
{
string s5(10, '*');
cout << s5 << endl;
return 0;
}
string类对象的容量操作
- size_t size() const;
返回字符串有效字符的长度,指向最后一个字符的下一个
int main()
{
string s("hello wor");
cout << s.size() << endl;
cout << s[9] << endl;
return 0;
}
- size_t length() const;
返回字符串的有效长度,size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一
致,一般情况下基本都是用size()。
int main()
{
string s("hello wor");
cout << s.length() << endl;
cout << s[9] << endl;
return 0;
}
- size_t capacity() const;
返回为当前字符分配空间的总大小(字节),可以和length相等或比它大
int main()
{
string s("hello wor");
cout << s.length() << endl;
cout << s.capacity() << endl;
return 0;
}
- void reserve (size_t n = 0);
为string预留空间,不改变当前有效元素个数,当reserve的参数小于string的底层空间总大小时,reserve不会改变容量大小,提前分配足够内存,避免后续操作(append,push_back)多从触发内存分配
int main()
{
string s("hello wor");
cout << s.length() << endl;
cout << s.capacity() << endl;
s.reserve(100);
cout << s.capacity() << endl;
return 0;
}
- void resize (size_t n);
void resize (size_t n, char c);
都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。当n小于capacity,capacity不会缩减,当n大于capacity,capacity会自动扩容。
int main()
{
string s("hello wor");
cout << s.length() << endl;
cout << s.capacity() << endl;
s.resize(100,'*');
cout << s << endl;
cout << s.capacity() << endl;
return 0;
}
- void clear();
清除字符串中的内容,使其变为空字符串,但不改变其底层大小(capacity)
int main()
{
string s("hello wor");
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.clear();
cout << s << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
return 0;
}
-
bool empty() const;
检查字符串是否为空字符串,如果为空,就返回true,否则返回false -
void shrink_to_fit();
改变字符串底层空间大小,不影响字符串的长度与内容
int main()
{
string s("h");
s.resize(100);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.resize(10);
cout << s.length() << endl;
cout << s.capacity() << endl;
s.shrink_to_fit();
cout << s.length() << endl;
cout << s.capacity() << endl;
return 0;
}
string类对象的访问及遍历操作
- char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
返回pos位置字符,使用const修饰,可以使const string 类对象调用 - begin和end
begin是获取字符串第一个字符的迭代器,end是获取最后一个字符的下一个位置的迭代器
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s("string");
cout << s << endl;
for (string::iterator a = s.begin(); a != s.end(); a++)
{
cout << *a << endl;
}
return 0;
}
- rbegin和rend
int main()
{
string s("string");
cout << s << endl;
for (string::reverse_iterator a = s.rbegin(); a != s.rend(); a++)
{
cout << *a << endl;
}
return 0;
}
string类对象的修改操作
- void push_back (char c);
在字符串尾部添加一个字符
int main()
{
string s("string");
cout << s << endl;
s.push_back('a');
cout << s << endl;
return 0;
}
- append
追加字符串
(1)是追加一个字符串对象,(2)追加一个字符串的子串,从subpos位置开始,长度为sublen
(3)追加一个C语言形式的字符串(4)追加字符串S的前n个(5)追加n个字符c
int main()
{
string s("string");
string s1("world");
cout << s << endl;
s.append(s1);
cout << s << endl;
s.append(10, 'q');
cout << s << endl;
s.append("dream", 2, 2);
cout << s << endl;
s.append("life", 2);
cout << s << endl;
s.append("append");
cout << s << endl;
return 0;
}
- std::string::operator+=
在字符串后追加字符串 - c_str
const char c_str() const; - find
find 是一个用于在字符串中查找子字符串或字符的成员函数。它返回子字符串或字符第一次出现的位置索引,如果未找到则返回 std::string::npos(size_t的最大值)
(1)从pos位置开始查找子字符串str,返回第一次出现的位置索引
(2)c语言风格字符串
(3)从位置 pos 开始,查找字符数组 s 的前 n 个字符组成的子字符串,返回第一次出现的位置索引。
(4)从pos位置开始查找字符c,返回第一次出现的位置索引
int main()
{
string s("hello world");
string s1("world");
size_t n1 = s.find(s1,0);
size_t n2 = s.find(s1);
size_t n3 = s.find("world");
size_t n4 = s.find("world",2);
size_t n5 = s.find("woqqrld", 0,2);
size_t n6 = s.find('l', 2);
cout << n1 << endl;
cout << n2 << endl;
cout << n3 << endl;
cout << n4 << endl;
cout << n5 << endl;
cout << n6 << endl;
return 0;
}
- rfind
find 是一个用于在字符串中查找子字符串或字符的成员函数。它返回子字符串或字符第一次出现的位置索引(从末尾向前查找) - substr
string substr (size_t pos = 0, size_t len = npos) const;
从pos位置开始截取长度为len的字符串
int main()
{
string s("hello world");
string s1=s.substr(6,5);
cout << s << endl;
cout << s1 << endl;
return 0;
}