STL初识
一、什么是STL
STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。
二、STL的六大组件

三、为什么学习string类
c语言中的字符串
C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。
string类的常用接口说明
string类对象的的常见构造 
函数名称 | 功能说明 |
---|
string() | 构造空的string类对象,即空字符串 | string(const char* s) | 用C-string来构造string类对象 | string(const string&s) | 拷贝构造函数 | string(size_t n, char c) | string类对象中包含n个字符c |
void Teststring()
{
string s1;
string s2("hello bit");
string s3(s2);
}
string类对象的容量操作
函数名称 | 功能说明 |
---|
size(重点) | 返回字符串有效字符长度 | length | 返回字符串有效字符长度 | capacity | 返回空间总大小 | empty (重点) | 检测字符串释放为空串,是返回true,否则返回false | clear (重点) | 清空有效字符 | reserve (重点) | 为字符串预留空间** | resize (重点) | 将有效字符的个数该成n个,多出的空间用字符c填充 |
void Teststring1()
{
string s("hello, bit!!!");
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s <<endl;
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(10, 'a');
cout << s.size() << endl;
cout << s.capacity() << endl;
s.resize(15);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
}
====
void Teststring2()
{
string s;
s.reserve(100);
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(50);
cout << s.size() << endl;
cout << s.capacity() << endl;
}
====
void TestPushBack()
{
string s;
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
void TestPushBackReserve()
{
string s;
s.reserve(100);
size_t sz = s.capacity();
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
三种遍历
1.下标+[]
int main()
{
string s1;
string s2("hello bit");
for (size_t i = 0; i < s2.size(); ++i)
{
s2[i] = 'x';
}
cout << endl;
for (size_t i = 0; i < s2.size(); ++i)
{
cout << s2[i] << " ";
}
cout << endl;
return 0;
}
2.迭代器 [begin(),end()) end()返回的不是最后一个数据位置的迭代器,返回是最后一个位置的下一个位置。 迭代器的用法是模拟指针。
string::iterator it = s2.begin();
while (it != s2.end())
{
cout << *it << " ";
++it;
}
cout << endl;
迭代器的意义,像string、vector支持[]遍历,但是List、map等等容器不支持[],我们就要用迭代器遍历。 3.c++提供,范围for 依次取容器中的数据,赋值给e,自动判断结束。
for (auto e : s3)
{
cout << e << " ";
}
cout << endl;
反向迭代器
string s3("123456");
string::reverse_iterator rit = s3.rbegin;
while (rit != s3.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;

四、尾插
push_back 格式: void push_back (char c); Appends character c to the end of the string, increasing its length by one. 插入一个字符到字符串的末尾。
int main()
{
string s1;
s1.push_back('h');
s1.push_back('e');
s1.push_back('l');
s1.push_back('l');
s1.push_back('o');
return 0;
}
append string& append (const char* s); 直接插入一个字符串
int main()
{
string s1;
s1.push_back('h');
s1.push_back('e');
s1.push_back('l');
s1.push_back('l');
s1.push_back('o');
s1.append("world");
return 0;
}
operator+= string& operator+= (const string& str); string& operator+= (const char* s); string& operator+= (char c);
#include <iostream>
#include <string>
int main ()
{
std::string name ("John");
std::string family ("Smith");
name += " K. ";
name += family;
name += '\n';
std::cout << name;
return 0;
}
|