15stl模板

1.stack

复制代码
#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;

int main(){
    stack<int>mystack;//
    mystack.push(16);//插入元素
    mystack.push(64);
    mystack.push(32);
    printf("大小:%d\n",mystack.size());//大小

    while(!mystack.empty()){//非空
        printf("%d\n",mystack.top());//栈顶元素
        mystack.pop();//栈顶出栈
    }

    return 0;
}
View Code
复制代码

2.queue

复制代码
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;

int main(){
    queue<int>myqueue;//
    myqueue.push(16);//插入元素
    myqueue.push(64);
    myqueue.push(32);
    printf("大小:%d\n",myqueue.size());//大小

    while(!myqueue.empty()){//非空
        printf("%d\n",myqueue.front());//队首元素
        myqueue.pop();//队首出队
    }

    return 0;
}
View Code
复制代码

3.priority_queue

复制代码
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;

int main(){
    priority_queue<int>mypq;//
    mypq.push(16);//插入元素
    mypq.push(64);
    mypq.push(32);
    printf("大小:%d\n",mypq.size());//大小

    while(!mypq.empty()){//非空
        printf("%d\n",mypq.top());//队首元素
        mypq.pop();//队首出队
    }

    return 0;
}
View Code
复制代码

4.vector

复制代码
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;

int main(){
    vector<int>myvector;//
    myvector.push_back(16);//在最后插入元素
    myvector.push_back(64);
    myvector.push_back(32);
    printf("大小:%d\n",myvector.size());//大小

    vector<int>::iterator it;//迭代器
    for(it=myvector.begin();it!=myvector.end();++it){//开头到结尾
        printf("%d\n",*it);
    }

    return 0;
}
View Code
复制代码

5.set

复制代码
#include<iostream>
#include<stdio.h>
#include<set>
using namespace std;

//以类为比较器
struct classCompare{
    bool operator()(const int &a,const int &b)const{
        return a>b;//降序
    }
};
//以指针函数为比较器
bool cmp(int a,int b){
    return a>b;//降序
}

int main(){
    //
    set<int>myset;//默认升序

    //set<int,classCompare>myset;//降序

    //bool(*p)(int,int)=cmp;
    //set<int,bool(*)(int,int)>myset(p);//降序

    myset.insert(16);//插入元素
    myset.insert(64);
    myset.insert(32);
    printf("大小:%d\n",myset.size());//大小

    set<int>::iterator it;//迭代器
    for(it=myset.begin();it!=myset.end();++it){//开头到结尾
        printf("%d\n",*it);
    }

    return 0;
}
View Code
复制代码

6.map

复制代码
#include<iostream>
#include<stdio.h>
#include<map>
using namespace std;

int main(){
    map<char,int>mymap;//
    mymap['b']=16;//插入元素
    mymap.insert(pair<char,int>('a',64));
    mymap.insert(pair<char,int>('c',32));
    printf("大小:%d\n",mymap.size());//大小

    map<char,int>::iterator it;//迭代器
    for(it=mymap.begin();it!=mymap.end();++it){//开头到结尾
        printf("%c %d\n",it->first,it->second);
    }

    return 0;
}
View Code
复制代码

 

posted @   gongpixin  阅读(224)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示