STL我相信各位学C++的肯定都不会陌生,C++自从模版出来之后就发生了革命性的意义。有了模版这个东西我们就可以只书写一个库来不给不同类型的数据使用。
STL(standard template libaray-标准模板库
):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。
STL主要是由四大组件组成的,前面说了STL 是一个包罗数据结构与算法的软件框架 其中里面的容器就是数据结构库含有各种常用的数据结构
但是由于历史原因,string是先出来的 STL 是后面由惠普实验室后开发出来开源所以人们并没有把string 归类到STL 之中。
在C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数。
所以在C++中 专门把字符串操作封装成了 string 容器,来给开发者更好的调用接口支持。不用去管理底层的空间分配使得使用更加省心。
在使用string类时,必须包含#include头文件以及using namespace std;
构造函数介绍我们初始化string 对象的几种方法
int main()
{
string s1();
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
string s2(s1, 6, 4);
cout << s2 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1(4,'x');
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
string s2(s1);
cout << s2 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
string s2= s1;
cout << s2 << endl;
return 0;
}
迭代器是C++提供的一种新的遍历方式,其底层是一种类似指针的实现方式。可能很多人觉得这有什么可说的,但是迭代器不仅可以遍历string还能遍历二叉树链表是一种通用的遍历方式。
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
//使用迭代器遍历
string::iterator it1 = s1.begin();
while (it1 != s1.end())
{
cout << *it1 << " ";
it1++;
}
cout << endl;
//使用迭代器修改
string::iterator it2 = s1.begin();
while (it2 != s1.end())
{
*it2 -= 1;
cout << *it2 << " ";
it2++;
}
cout << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << ' ';
}
cout << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
for (auto e : s1)
{
cout << e << ' ';
}
cout << endl;
return 0;
}
这俩就是反向迭代器,使用他们打印出来的结果是从后往前
int main()
{
string s1("hello gugu");
//使用迭代器遍历
string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1.capacity() << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1.size() << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
s1.resize(3, 'x');
cout << s1 << endl;
s1.resize(5);
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1.capacity() << endl;
s1.reserve(6);
cout << s1.capacity() << endl;
s1.resize(100);
cout << s1.capacity() << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1 << endl;
s1.clear();
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1.empty() << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1.capacity() << endl;
s1.reserve(100);
cout << s1.capacity() << endl;
s1.shrink_to_fit();
cout << s1.capacity() << endl;
return 0;
}
```cpp
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << ' ';
}
cout << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
string s2("xxxxx");
s1 += s2;
cout << s1 << endl;
s1 += "vvvv";
cout << s1 << endl;
s1 += 'x';
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
string s2("xxxxx");
s1.append(s2);
cout << s1 << endl;
s1.append("vvvv");
cout << s1 << endl;
s1.append(4,'c');
cout << s1 << endl;
s1.append("abcdef",3);
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
s1.push_back('x');
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
string s2("xxxxxxx");
s1.assign(s2);
cout << s1 << endl;
s1.assign("Linux C++");
cout << s1 << endl;
s1.assign(5,'c');
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
string s2("C++");
s1.insert(6, s2);
cout << s1 << endl;
s1.insert(2, "xxxx");
cout << s1 << endl;
s1.insert(6, 2,'v');
cout << s1 << endl;
s1.insert(6,"bbbbbb",2);
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.erase(5);
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
s1.replace(5,1,"C++");
cout << s1 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
string s2("C++ Linux");
cout << s1 << endl;
cout << s2 << endl;
swap(s1, s2);
cout << s1 << endl;
cout << s2 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
int pos = s1.find('g');
cout << s1[pos];
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
int pos = s1.rfind('g');
cout << pos << endl;
cout << s1[pos];
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1("hello gugu");
cout << s1.c_str() << endl;
return 0;
}