目录
什么是STL?
stl中的东西可以重复利用,stl是为了建立数据结构与算法的一套标准
stl分为容器,算法,迭代器
stl六大组件:容器,算法,迭代器,仿函数,适配器,空间配置器
vector容器
可以理解为比较强大的数组
构造函数
cpp
#include<iostream>
#include<vector>
using namespace std;
void print(vector<int>& a) {
for (int i = 0; i < a.size (); i++) {
cout << a[i] << " ";
}
cout << endl;
}
int main() {
//默认构造
vector<int> a;
for (int i = 0; i < 10; i++) {
a.push_back(i);
}
print(a);
//区间构造
vector<int> b(a.begin(), a.end());
print(b);
//n个elem方式构造
vector<int> c(10, 100);
print(c);
//拷贝构造
vector<int> d(a);
print(d);
return 0;
}

赋值操作
cpp
#include<iostream>
#include<vector>
using namespace std;
void print(vector<int>& a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}cout << endl;
}
int main() {
vector<int> a;
for (int i = 0; i < 10; i++) {
a.push_back(i);
}
print(a);
vector<int> b;
b = a;
print(b);
//assign
vector<int> c;
c.assign(a.begin(), a.end());
print(c);
//n个elem
vector<int> d;
d.assign(10, 100);
print(d);
return 0;
}

vector容量和大小
cpp
#include<vector>
#include<iostream>
using namespace std;
void p(vector<int>& a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}cout << endl;
}
int main() {
vector<int> a;
for (int i = 0; i < 10; i++) {
a.push_back(i);
}
p(a);
if (a.empty()) {
cout << "空" << endl;
}
else {
cout << "不空" << endl;
cout << "容量为:" << a.capacity() << endl;
cout << "大小为:" << a.size() << endl;
}
//重新指定大小
a.resize(15,100);
p(a);//长度过大默认用0填充,如果指定数字则用指定数字填充
a.resize(5);
p(a);//超出部分删除
return 0;
}

vector存放内置数据类型
cpp
#include<iostream>
#include<algorithm>//算法头文件
#include<vector>//该容器的头文件
using namespace std;
//第三种遍历方式的函数
void print(int t) {
cout << t << endl;
}
int main() {
//创建容器
vector<int> a;
//插入数据
a.push_back(10);
a.push_back(20);
a.push_back(30);
//通过迭代器访问数据
//起始迭代器,指向第一个元素
vector<int>::iterator itBegin = a.begin();
//结束迭代器,最后一个元素的下一个位置
vector<int>::iterator itEnd = a.end();
//遍历
//第一种方式(复杂)
while (itBegin != itEnd) {
cout << *itBegin << endl;
itBegin++;
}
//第二种方式
for (vector<int> ::iterator it = a.begin(); it != a.end(); it++) {
cout << *it << endl;
}
//第三种方式(STL遍历算法)
for_each(a.begin(), a.end(),print);
return 0;
}
vector存放自定义数据类型
cpp
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
class person {
public:
person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
int main() {
vector<person> a;
person p1("aaa", 10);
person p2("bbb", 20);
person p3("ccc", 30);
//放入数据
a.push_back(p1);
a.push_back(p2);
a.push_back(p3);
//遍历
for (vector<person> ::iterator it = a.begin(); it != a.end(); it++) {
cout << "姓名:" << (*it).m_Name << " " << (*it).m_Age << endl;
}
return 0;
}

存放指针
cpp
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
class person {
public:
person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
int main() {
vector<person*> a;
person p1("aaa", 10);
person p2("bbb", 20);
person p3("ccc", 30);
//放入数据
a.push_back(&p1);
a.push_back(&p2);
a.push_back(&p3);
//遍历
for (vector<person *> ::iterator it = a.begin(); it != a.end(); it++) {
cout << "姓名:" << (*it)->m_Name << " " << (*it)->m_Age << endl;
}
return 0;
}

vector容器嵌套容器
类似二维数组
cpp
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
vector<vector<int>> a;
//创建小容器
vector<int> a1;
vector<int> a2;
vector<int> a3;
//向小容器中添加数据
for (int i = 0; i < 3; i++) {
a1.push_back(i + 1);
a2.push_back(i + 2);
a3.push_back(i + 3);
}
//小容器放入大容器
a.push_back(a1);
a.push_back(a2);
a.push_back(a3);
//通过大容器把所有数据遍历一遍
for (vector<vector<int>> ::iterator it = a.begin(); it != a.end(); it++) {
for (vector<int> ::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
cout << (*vit) << " ";
}
cout << endl;
}
return 0;
}

string容器
string可以看成c++的字符串,string本质上是一个类
构造函数
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
//默认构造
string s1;
//使用字符串s初始化
const char* str = "hello world";
string s2(str);
cout << "s2=" << s2 << endl;
//拷贝构造
string s3(s2);
cout << "s3=" << s3 << endl;
//用n个字符初始化
string s4(10, 'a');
cout << "s4=" << s4 << endl;
return 0;
}

赋值操作
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
//char*类型字符串,赋值给当前的字符串
string s1 = "hello";
cout << "s1=" << s1 << endl;
//把字符串s赋值给当前的字符串
string s2 = s1;
cout << "s2=" << s2 << endl;
//字符赋值给当前的字符串
string s3 = "a";
cout << "s3=" << s3 << endl;
//把字符串s赋值给当前的字符串
string s4;
s4.assign("c++");
cout << "s4=" << s4 << endl;
//把字符串s的前n个字符赋给当前字符串
string s5;
s5.assign("c++", 2);
cout << "s5=" << s5 << endl;
//把字符串s赋值给当前的字符串
string s6;
s6.assign(s5);
cout << "s6=" << s6 << endl;
//用n个字符c赋给当前字符串
string s7;
s7.assign(10, 'W');
cout << "s7=" << s7 << endl;
return 0;
}

字符串拼接
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1 = "我";
s1 += "爱玩游戏";
cout << "s1=" << s1 << endl;
s1 += ':';
cout << "s1=" << s1 << endl;
string s2 = "LOL";
s1 += s2;
cout << "s1=" << s1 << endl;
string s3 = "I ";
s3.append("love ");
cout << "s3=" << s3 << endl;
s3.append("game abcde", 4);//前4个
cout << "s3=" << s3 << endl;
s3.append(s2);
cout << "s3=" << s3 << endl;
s3.append(s2, 0, 2);//截取s2中的字符,第0个开始,截取2个
cout << "s3=" << s3 << endl;
return 0;
}

查找和替换
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
//查找
string s1 = "abcdefgde";
int pos = s1.find("de");
cout << pos << endl;//d的位置
pos = s1.rfind("de");
cout << pos << endl;
//find从左往右查,rfind从右往左查
//替换
string s2 = "abcdefg";
s2.replace(1, 3, "11111111");//1~3位置全部替换为11111111
cout << s2 << endl;
return 0;
}

string字符串比较
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
//比较方式:ASC||码
//=返回0
//>返回1
//<返回-1
string s1 = "xello";
string s2 = "hello";
if (s1.compare(s2) == 0) {
cout << "s1=s2" << endl;
}
else if (s1.compare(s2) > 0) {
cout << "s1>s2" << endl;
}
else {
cout << "s1<s2" << endl;
}
return 0;
}

string字符存取
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1 = "hello";
cout << s1 << endl;
//通过[]访问单独字符
for (int i = 0; i < s1.size(); i++) {
cout << s1[i] << " ";
}
cout << endl;
//通过at方式访问单个字符
for (int i = 0; i < s1.size(); i++) {
cout << s1.at(i) << " ";
}
cout << endl;
//修改单个字符
s1[0] = 'x';
cout << s1 << endl;
return 0;
}

string插入和删除
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "hello";
//插入
s.insert(1, "111");
cout << s << endl;
//删除
s.erase(1, 3);
cout << s << endl;
return 0;
}

string子串
cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "abcdef";
string sub = s.substr(1, 3);
cout << sub << endl;
//实用操作
string email = "[email protected]";
int pos=email.find("@");
string userName = email.substr(0, pos);
cout << userName << endl;
return 0;
}

未完待续。。。。。。。。。。。。。。。
