写在前面
该文章为课堂笔记,如有补充,欢迎提出。
框架
#include<iostream>
#include<vector>
#include<stack>
#include<deque>
#include<queue>
#include<set>
#include<map>
using namespace std;
int main()
{
return 0;
}
STL:标准程序库
动态数组 vector
- 动态数组
- 动态扩容 -> 性能消耗
- 按索引访问性能高,在中间插入或删除性能低(相对的)
- push_back // 尾部添加
- pop_back //尾部删除
- operator[] //按数组
- front //第一个
- back //最后一个
- erase //删除
#include<iostream>
#include<vector>
using namespace std;
void show(const vector<int> &v)//遍历
{
cout<<"size "<<v.size()<<":";//输出元素个数
// for(auto it=v.begin(); it!=v.end();it++) //同下方循环结果相同
// {
// cout<<*it<<" ";
// }
for(int i=0;i<v,size();++i
{
cout<<v[i]<<endl;
}
cout<<endl;
}
int main()
{
vector<int> v1;
vector<int> v2(n:10);
vector<int> v3(n:10,value:100);
show(v1);
show(v2);
show(v3);
for(int i=0;i<10;++i)
{
v2.push_back(i);//增加
}
for(int i=0;i<10;++i)
{
v2[i]=i;//修改
}
v2.erase(position:v2.end()-1);//删除
show(v2);
while(!v3.empty())
{
cout<<v3.back<<" ";
v3.pop_back();
}
cout<<v3.empty()<<endl;//为空则返回1
return 0;
}
栈 stack(后进先出)
- 后进先出
- 容器适配器(基于其他基础容器来实现, vector, deque and list , deque为默认)
- push
- pop
#include<stack>
#include<iostream>
using namespace std;
int main()
{
stack<int> s;
for(int i=0;i<10;++i)
{
s.push(i);
}
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
}
队列 queue(先进先出)
- deque 双端队列
- operator[]
- front[]
- back[]
- push_back
- push_front
- pop_back
- pop_front
- clear
同vector f3代码
更换头文件即可:#include<deque>
- queue
- 先进先出,适配器(默认基于deque实现)
- push
- pop
- 先进先出,适配器(默认基于deque实现)
- priority_quene(类似大顶堆,自动维护顺序)
- 优先队列,默认基于vector实现
- Template parameters
- priority_queue::value_type.
- priority_queue::container_type
- Compare
- 成员函数
- push
- pop——头部,最大值
- top——头部最大值
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<queue>
using namespace std;
typedef priority_queue<int,vector<int>,greater<int>> pq;
class myCMP //自定义排序
{
public:
bool operator()(string s1,string s2)
{
return s1.size() < s2.size();//大顶堆,改变符号则为小顶堆
}
}
void f1;
void f2;
void f3;
int main()
{
f1();
cout<<"------"<<endl;
f2();
f3();
return 0;
}
void f1() //自然顺序输出
{
priority_queue<int> q; //创建对象
srand(seed:time(_timer:0));
for(int i=0;i<10;++i)
{
q.push(x:rand()%100);
}
while(!q.empty())
{
cout<<q.top()<<endl;
q.pop();
}
}
void f2() //从小到大输出
{
pq q; //用greater进行比较,小顶堆
srand(seed:time(_timer:0));
for(int i=0;i<10;++i)
{
q.push(x:rand()%100);
}
while(!q.empty())
{
cout<<q.top()<<endl;
q.pop();
}
}
void f3() //自定义比较器
{
priority_queue<string,vector<string>,myCMP> pq;
pq.push(x:"abc");
pq.push(x:"de");
while(!pq.empty())
{
cout<<pq.top()<<endl;
q.pop();
}
}
集合 set
- 不重复,维持元素顺序
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;
- insert
- erase
- clear
- find
- count
- lower_bound
- upper_bound
#include<iostram>
#include<set>
using namespace std;
struct Student //是类型,也是比较器
{
string name;
int age;
bool operator()(const Student &lh,,const Student &rh) const //自定义比较器:按照年龄排序
{
return lh.age<rh.age;
}
}
int main()
{
//set<Student> sty_set; //默认以第一个值升序排序
set<Student,Student> sty_set;
stu_set.insert(x:{name:"zhang",age:20});
stu_set.insert(x:{name:"zhao",age:20}); //set若值重复,只取第一个
stu_set.insert(x:{name:"li",age:30});
stu_set.insert(x:{name:"zhou",age:40});
stu_set.insert(x:{name:"wu",age:35});
for(auto it=stu_set.begin();it!=stu_set.end();it++)
{
cout<<it->name<<" "<<(*it).age<<endl;
}
return 0;
}
映射 map
- 存储键值对,键不重复,维护键的顺序
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
- operator[]
- insert
- erase
- clear
- find
- count
- lower_bound
- upper_bound
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string,string> m;//默认升序
//map<string,string,greater<string>> m;//倒序
m.insert(x:{x:"1",y:"zhang"});
m.insert(x:make_pair(x:"2",y:"li"));//造一个键值对
cout<<m["2"]<<endl;
m["1"]="AA";
for(auto it=m.bebgin();it!=m.end();it++)//迭代输出
{
it->first<<" "<<it->second<<endl;
}
return 0;
}
补充
容器都有的成员函数
- Iterators:
- begin
- end——指向最后一个元素的后一个位置
- Capacity:
- size
- empty
泛型
- 标签
- 类型参数
- 占位
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<sstresm>
using namespace std;
typedef priority_queue<int,vector<int>,greater<int>> pq;
template<typename T>//此处T为占位符,假设T是一个类别
class myCMP
{
public:
bool operator()(T s1,T s2)
{
stringstream ss;
string ss1;
string ss2;
ss<<s1;
ss>>ss1;
ss.clear();
ss<<s2;
ss>>ss2;
return s1.size() < s2.size();
}
}
void f3;
int main()
{
f3();
return 0;
}
void f3()
{
priority_queue<string,vector<string>,myCMP<string>> pq;//此时传进的string将替换T
pq.push(x:"abc");
pq.push(x:"de");
while(!pq.empty())
{
cout<<pq.top()<<endl;
q.pop();
}
}