STL学习总结(1)
STL(Standard Template Library,标准模板库)
STL从广义上分为:容器(container)算法(algorithm)迭代器(iterator).
在我们 STL 中容器分为:序列式容器和关联式容器。
学习STL,需要了解主要容器的特性,以及常用API。、
一、STL基本语法
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector> //动态数组 可变数组
#include<algorithm> //算法
using namespace std;
void PrintVector(int v) {
cout << v << " ";
}
//STL基本语法
void test01() {
//定义一个容器,并且制指定这个容器存放的元素类型是Int
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//通过STL提供的for_each算法
//容器提供的迭代器
//vector<int>::iterator 迭代器类型
vector<int>::iterator pBegin = v.begin();
vector<int>::iterator pEnd = v.end();
for_each(pBegin, pEnd, PrintVector);
}
//容器也可以存放自定义数据类型
class Person {
public:
Person(int age, int id) :age(age), id(id) {}
public:
int age;
int id;
};
void test02() {
//创建容器,并且指定容器的元素类型是Person
vector<Person> v;
Person p1(10, 20), p2(30, 40), p3(50, 60);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
//遍历
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
cout << (*it).age << " " << (*it).id << endl;
}
}
int main(void)
{
//test01();
test02();
return 0;
}
test01运行结果:
test02运行结果:
要想学好一个容器,需要知道容器的特性,以及常用API
二、string容器
string容器封装了char*
char*是一个指针,而string是一个类
string也封装了很多实用的方法
查找 find,拷贝copy,删除delete 替换replace,插入insert
string的优势:
string 管理 char*所分配的内存。每一次 string 的复制,取值都由 string 类负责维护,不用担心复制越界和取值越界等
看看一个string的基本用法
string的初始化
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
void test01(){
string s1;//调用无参构造
string s2(10,'a');
string s3("abcdefg");
string s4(s3);//拷贝构造
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
}
int main(void) {
test01();
return 0;
}
运行结果:
赋值操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
void test02()
{
string s1;
string s2("app");
s1 = "abcd";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
s1.assign("jk");
cout << s1 << endl;
}
int main(void) {
test02();
return 0;
}
运行结果:
取值操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
void test03() {
string s1 = "abcdefg";
//重载[]操作符
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;
//区别:[]方式 如果访问越界,直接挂了
//at方式 访问越界 抛异常out_of_range
try {
//cout << s1[100] << endl;
cout << s1.at(100) << endl;
}
catch (...) {
cout << "越界!" << endl;
}
}
int main(void) {
test03();
return 0;
}
运行结果:
拼接操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
void test04() {
string s1 = "abcd";
string s2 = "1111";
s1 += "abcd";
s1 += s2;
cout << s1 << endl;
string s3 = "2222";
s2.append(s3);
cout << s2 << endl;
string s4 = s2 + s3;
cout << s4 << endl;
}
int main(void) {
test04();
return 0;
}
运行结果:
查找操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
//查找操作
void test05() {
string s = "abcdefghjfgkl";
//查找第一次出现的位置
int pos = s.find("fg");
cout << "pos:" << pos << endl;
//查找最后一次出现的位置
pos = s.rfind("fg");
cout << "pos:" << pos << endl;
}
int main(void) {
test05();
return 0;
}
运行结果:
string替换
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
void test06() {
string s = "abcdefg";
s.replace(0, 2, "111");
//string& replace(int pos, int n, const char* s); //替换从 pos 开始的 n 个字符为字符串 s
cout << s << endl;
}
int main(void) {
test06();
return 0;
}
运行结果:
string比较大小
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
void test07() {
string s1 = "abcd";
string s2 = "abce";
if (s1.compare(s2) == 0) {
cout << "字符串相等!" << endl;
}
else {
cout << "字符串不相等!" << endl;
}
}
int main(void) {
test07();
return 0;
}
运行结果:
子串操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
void test08() {
string s = "abcdefg";
string mysubstr = s.substr(1, 3);
//string substr(int pos = 0, int n = npos) const;//返回由 pos 开始的 n 个字符组成的字符串
cout << mysubstr << endl;
}
int main(void) {
test08();
return 0;
}
运行结果:
插入和删除
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
void test09() {
string s = "abcdefg";
s.insert(3, "111");
//string& insert(int pos, const char* s); //插入字符串
cout << s << endl;
s.erase(0, 2);
//string& erase(int pos, int n = npos);//删除从 Pos 开始的 n 个字符
cout << s << endl;
}
int main(void) {
test09();
return 0;
}
运行结果:
三、vector容器
vector 容器是一个长度动态改变的动态数组,既然也是数组,那么其内存是一段连续的内存,具有数组的随机存取的优点。
初始化
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//初始化
void test01() {
vector<int>v1;//默认构造
int arr[] = {10,20,30,40};
vector<int> v2(arr,arr+sizeof(arr)/sizeof(int));
vector<int> v3(v2.begin(),v2.end());
vector<int> v4(v3);//拷贝构造
printVector(v1);
printVector(v2);
printVector(v3);
printVector(v4);
}
int main(void) {
test01();
system("pause");
return 0;
}
运行结果:
常用赋值操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//常用赋值操作
void test02() {
int arr[] = { 10, 20, 30, 40 };
vector<int> vl(arr, arr + sizeof(arr) / sizeof(int));//默认构造
//成员方法
vector<int> v2;
v2.assign(vl.begin(), vl.end());
//重载=
vector<int> v3;
v3 = v2;
int arr1[] = { 100, 200, 300, 400 };
vector<int> v4(arr1, arr1 + sizeof(arr) / sizeof(int));//默认构造
printVector(vl);
printVector(v2);
printVector(v3);
printVector(v4);
cout << "------------------" << endl;
v4.swap(vl);
printVector(vl);
printVector(v2);
printVector(v3);
printVector(v4);
}
int main(void) {
test02();
system("pause");
return 0;
}
运行结果:
大小操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//resize(int num);//重新指定容器的长度为 num,若容器变长,则以默认值填充新位置。如果容器变
//短,则末尾超出容器长度的元素被删除。
//resize(int num, elem);//重新指定容器的长度为 num,若容器变长,则以 elem 值填充新位置。如
//果容器变短,则末尾超出容器长>度的元素被删除。
//capacity();//容器的容量
//reserve(int len);//容器预留 len 个元素长度,预留位置不初始化,元素不可访问。
void test03() {
int arr1[] = { 100, 200, 300, 400 };
vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));//默认构造
cout << "size:" << v4.size() << endl;
if (v4.empty() == true) {
cout << "空!" << endl;
}
else {
cout << "不空!" << endl;
}
printVector(v4);
v4.resize(2);
printVector(v4);
//v4.resize(6);
printVector(v4);
v4.resize(6, 1);
printVector(v4);
for (int i = 0; i < 10000; i++) {
v4.push_back(i);
}
cout << "size:" << v4.size() << endl; //元素的个数 20
cout << "容量:" << v4.capacity() << endl; //容量
}
int main(void) {
test03();
system("pause");
return 0;
}
运行结果:
vector存取数据
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//vector存取数据
void test04() {
int arr1[] = { 100, 200, 300, 400 };
vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));//默认构造
for (int i = 0; i < v4.size(); i++) {
cout << v4[i] << " ";
}
cout << endl;
for (int i = 0; i < v4.size(); i++) {
cout << v4.at(i) << " ";
}
cout << endl;
//区别: at抛异常 []不抛异常
cout << "front:" << v4.front() << endl;
cout << "back:" << v4.back() << endl;
}
int main(void) {
test04();
system("pause");
return 0;
}
运行结果:
插入和删除
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test05() {
vector<int> v;
v.push_back(10);
v.push_back(20);
//头插法
v.insert(v.begin(), 30);
v.insert(v.end(), 40);
v.insert(v.begin() + 2, 100); //vector支持随机访问
//支持数组下标,一般都支持随机访问
//迭代器可以直接+2 +3 -2 -5操作
printVector(v);
//删除
v.erase(v.begin());
printVector(v);
v.erase(v.begin() + 1, v.end());
printVector(v);
v.clear();
cout << "size:" << v.size() << endl;
}
int main(void) {
test05();
system("pause");
return 0;
}
运行结果:
swap缩减空间
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
using namespace std;
void test06() {
//vector添加元素 他会自动增长 你删除元素时候,会自动减少吗?
//不会
vector<int> v;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
}
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
v.resize(10);
cout << "--------------" << endl;
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
//收缩空间
vector<int>(v).swap(v);
cout << "--------------" << endl;
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
}
int main(void) {
test06();
system("pause");
return 0;
}
运行结果:
reserve与resize的区别
reserve是预留空间;
resize是改变容器大小。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
using namespace std;
void test07() {
//reserve 预留空间 resize区别
int num = 0;
int* address = NULL;
vector<int> v;
v.reserve(100000);
for (int i = 0; i < 100000; i++) {
v.push_back(i);
if (address != &(v[0])) {
address = &(v[0]);
num++;
}
}
cout << "num:" << num << endl;
//如果你知道容器大概要存储的元素个数,那么你可以用reserve预留空间
}
int main(void) {
test07();
system("pause");
return 0;
}