标准模板库(STL,Standard Template Library)

目录

vector(可变数组)

list(双向链表)

stack(栈)

queue(队列)

priority_queue(优先队列)

set(集合)

map(映射)


vector(可变数组)

1、声明

vector v;
vector v(v0);
vector v(n,i);//n elements of which the value is i

2、使用

#include 
#include 
using namespace std;
vector v;
int main(){
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);// v:1 2 3
    cout<<"first: "<::iterator iter = v.begin();
    while (iter != v.end()){//traversal
        cout<<*iter<

list(双向链表)

1、声明

list l;
list l(l0);
list l(n,i);//n elements of which the value is i

2、使用

#include 
#include 
using namespace std;
list l;
int main(){
    l.push_back(1);
    l.push_back(2);
    l.push_front(3);
    l.push_front(4);//l: 4 3 1 2
    l.pop_back();//l: 4 3 1
    l.pop_front();//l: 3 1
    cout<<"size: "<::iterator iter = l.begin();
    while(iter != l.end()){
        cout<<*iter<

stack(栈)

1、声明

stack s;

2、使用

#include 
#include 
using namespace std;
stack s;
int main(){
    s.push(1);
    s.push(2);
    s.push(3);
    while (!s.empty()){
        cout<

queue(队列)

1、声明

queue q;

2、使用

#include 
#include 
using namespace std;
queue q;
int main(){
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);//tail 4 3 2 1 head 
    cout<<"first: "<

priority_queue(优先队列)

1、声明

priority_queue pq;// default: bigger first
priority_queue,cmp> pq;// according to "cmp"
//greater : smaller first
//less : bigger first

2、使用

#include 
#include 
using namespace std;
priority_queue, greater > pq;
int main(){
    pq.push(1);
    pq.push(3);
    pq.push(2);
    pq.push(4);//tail 4 3 2 1 head
    while (!pq.empty()){
        cout<<"top: "<

set(集合)

1、声明

set s;

2、使用

#include 
#include 
using namespace std;
set s;
int main(){
    s.insert(1);
    s.insert(1);
    s.insert(3);
    s.insert(2);
    if(s.find(4) == s.end())
        cout<<"Not Found"<::iterator iter = s.begin();
    while (iter != s.end()){
        cout<<*iter<

map(映射)

1、声明

map m;

2、使用

#include 
#include 
using namespace std;
map m;
int main(){
    m.insert(pair(1,"aoa"));
    m.insert(map::value_type(2,"bob"));
    m[3] = "coc";
    cout<::iterator iter = m.begin();
    while (iter != m.end()) {
        cout<<(*iter).first<<" "<<(*iter).second<

 

你可能感兴趣的:(C++)