zoukankan      html  css  js  c++  java
  • STL数据结构

    STL数据结构

    1.priority_queue

    #include<queue>

    pritority<int>q;(大根堆)

    priority_queue<int,vector<int>,greater<int> >q;(小根堆)

    struct no{

        int x,v;

        bool operator <(const no &T)const{return v>T.v;}

    // v值xiao的优先

    };

    queue<no>q;


    2.vector

    #include<vector>

    vector<int>vec;

    vec.push_back();(加入,从0开始)

    vec.size() 

    vec.pop_back()  (删除末尾)

    vec.clear();

    使用迭代器访问元素.

    vector<int>::iterator it;
    for(it=vec.begin();it!=vec.end();it++)
        cout<<*it<<endl;

    插入元素:    vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;

    删除元素:    vec.erase(vec.begin()+2);删除第3个元素

    vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始

    排序 sort(vec.begin(),vec.end(),cmp)

    元素翻转 reverse(vec.begin(),vec.end());


    3.map

    #include<map>

    map<int,int>

    直接用


    4.bitset

    #include<bitset>

    bitset<100> s;

    相当于flag[100]

    也可以当做100位的二进制数

    效率O(n/32)


    5.set

    include<set>

    set<int>S//去重

    multiset<int>S//不去重

    S.begin()第一个

    S.end()最后一个的下一位(没有东西)

    S.clear()

    S.empty() ,判断set容器是否为空

    S.count(x);x出现次数

    erase(iterator)  ,删除定位器iterator指向的值

    erase(key_value),删除键值key_value的值

    S.size()

    lower_bound(key_value) ,返回第一个大于等于key_value的定位器

    upper_bound(key_value),返回第一个大于key_value的定位器

    插入 S.inset(x);

    迭代器 set<int>::iterator pos;

    遍历 for(pos=S.begin();pos!=S.end();pos++)

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<set> 
    using namespace std;
    int main(){
    	set<int>S;
    	S.clear(); 
    	S.insert(1);S.insert(2);S.insert(3);
    	set<int>::iterator it;
    	it=S.begin();
    	cout<<*it<<' '<<*S.begin()<<endl; 
    	
    	//查找 
        set<int>::iterator t1,t2;
    	t1=S.lower_bound(1);t2=S.upper_bound(1);
        cout<<*t1<<' '<<*t2<<endl;
        set<int>::iterator t3;
    	t3=S.upper_bound(2);
    	cout<<"? "<<*t3<<endl; 
        //找不到就指向end 
        
    	//遍历 
    	for(it=S.begin();it!=S.end();++it){
    		cout<<*it<<endl;
    	}
    	//统计/ 判断是否出现 
    	cout<<S.count(1)<<endl; 
    	
    	//删除
    	it=S.end();
    	S.erase(it); 
    	cout<<*S.end()<<endl;
    	
    	return 0;
    }
    

    6.next_permutation(a+1,a+n+1)

    7.nth_element(first,kth,last,cmp)

     nth_element(now+1,now+m+1,now+n+1,greater<ll>());

    注意前m个是无序的,但保证是前m大

  • 相关阅读:
    Kubernetes节点维护
    Kubernetes helm配置国内镜像源
    windows universal app中使用mvvm light
    windows phone 开发常用小技巧
    异步编程中的最佳做法(Async/Await) --转
    windows phone 开发常用小技巧
    windows phone 开发常用小技巧
    windows phone 开发常用小技巧
    #假期归来# 看看国外开发者第一时间用swift写的Flappy Bird (2014.6.3)
    vs2013 TFS如何彻底删除团队项目
  • 原文地址:https://www.cnblogs.com/liankewei/p/10358868.html
Copyright © 2011-2022 走看看