常用的STL

list

可以高效的在任意地方删除和插入,都是常数时间

  1. 适用于插入和删除频繁,随机访问较少
  2. 双向链表
  3. 复杂度:O(1)
  4. erase 函数:使作为参数的迭代器失效,并返回指向该迭代器下一参数的迭代器。 注意下一个参数

用法 : hdu 1276 士兵训练问题

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        int k=2;
        list<int>mylist;
        for(int i=1;i<=n;i++) mylist.push_back(i);
        while(mylist.size()>3){
            int num=1;
            for(auto it=mylist.begin();it!=mylist.end();){
                if(num++%k==0) it=mylist.erase(it);
                else it++;
            }
            k==2?k=3:k=2;
        }
        for(auto it=mylist.begin();it!=mylist.end();it++)
            it!=mylist.begin()?cout<<' '<<*it:cout<<*it;
        cout<<endl;
    }
    return 0;
}

vector

  1. 适用于插入和删除少,随机访问频繁

queue

先进先出
queue&stack 用法 hdu 1702 ACboy needs your help again

priority_queue

优先级高的出队列。

  1. 队列和排序的完美结合
  2. 复杂度:O(nlgn)
  3. 结构体优先设置,需要重载运算符,bool operator<(const node&b) const.从数学意义上来说只需要重载小于运算符。这样内部就是数值大的优先级高
  4. 第3条的cmp函数与sort相反的作用。

用法: hdu 1873 看病要排队

#include <bits/stdc++.h>
using namespace std;
struct node{
    int id,p;
    bool operator<(const node&b) const{//两个数一样,不交换
        if(p!=b.p) return p<b.p;
        return id>b.id;//id的大小代表了时间顺序
    }
};
int main(){
    int n;
    while(cin>>n){
        priority_queue<node> res[4];
        int cnt=0;
        while(n--){
            string s;
            cin>>s;
            if(s=="IN"){
                int a,b;
                cin>>a>>b;
                res[a].push({++cnt,b});
            }else if(s=="OUT"){
                int a;
                cin>>a;
                if(res[a].size()==0) cout<<"EMPTY"<<endl;
                else {
                    cout<<res[a].top().id<<endl;
                    res[a].pop();
                }
            }
        }
    }
    return 0;
}

sort

  1. 复杂度:O(nlgn)
  2. 排列范围: [first,last)
  3. 自定义:less(), greater(), less_equal(), greater_equal(), 还有自定义的cmp

示例:
greater<int>()

map

  1. 复杂度:O(lgn)
    用法 hdu 2648 Shopping
#include <bits/stdc++.h>
using namespace std;
map<string ,int> res;
int main(){
    int m,n,d;
    string s;
    while(cin>>m){
        for(int i=0;i<m;i++) cin>>s;
        cin>>n;
        for(int i=0;i<n;i++){
            for(int i=0;i<m;i++){
                cin>>d>>s;
                res[s]+=d;
            }
            int rank=1;
            for(auto it=res.begin();it!=res.end();it++)
                if(it->second>res["memory"]) rank++;
            cout<<rank<<endl;
        }
        res.clear();
    }
    return 0;
}

set

  1. 复杂度:O(lgn)

用法 hdu 2094 产生冠军

#include <bits/stdc++.h>
using namespace std;
set<string> all,lose;
int main(){
    int n;
    while(cin>>n&&n){
        string a,b;
        for(int i=0;i<n;i++){
            cin>>a>>b;
            all.insert(a);
            all.insert(b);
            lose.insert(b);
        }
        if(all.size()-lose.size()==1) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
        all.clear();lose.clear();
    }
    return 0;
}

next_permutation

每执行一次,把新的排列放在原来的空间里。一般来说,初始序列(用sort)得到最小序列

  1. 返回值:bool 类型
  2. 复杂度:O(n)
  3. 排列范围:[first,last)

用法 hdu 1027

#include <bits/stdc++.h>
using namespace std;
int a[1010];
int main(){
    int n,m;
    while(cin>>n>>m){
        for(int i=1;i<=n;i++)a[i]=i;
        for(int i=1;i<m;i++) next_permutation(a+1,a+n+1);
        for(int i=1;i<=n;i++) i-1==0?cout<<a[i]:cout<<' '<<a[i];
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++竞赛中,常用STL(Standard Template Library)组件有vector、stack以及算法函数next_permutation。vector是一个动态数组容器,可以方便地进行元素的插入、删除和访问。stack是一个后进先出(LIFO)的容器,常用于实现递归、深度优先搜索等算法。next_permutation是一个算法函数,用于按照字典序生成某个序列的所有排列。 在竞赛中,我们可以利用vector来存储元素,使用push_back函数添加元素,使用[]操作符或迭代器进行元素的访问。可以使用stack来模拟递归过程,实现一些深度优先搜索的问题。而使用next_permutation函数可以方便地生成某个序列的所有排列。 举个例子,如果我们想要按照字典序输出1到n的全排列,可以使用next_permutation函数结合vector来实现。首先,我们可以使用一个for循环将1到n的元素添加到vector中。然后,使用do-while循环来不断调用next_permutation函数,每次生成下一个排列并输出。最后,当next_permutation函数返回false时,表示已经生成了所有的排列,循环结束。 另外,如果我们需要使用栈来解决一些问题,可以使用stack容器。可以使用push函数将元素入栈,使用pop函数将元素出栈。栈的特点是后进先出,所以可以用来模拟递归过程或实现一些需要回溯的算法。 总结起来,在C++竞赛中,常用STL组件有vector、stack和算法函数next_permutation。vector可以方便地进行元素的插入、删除和访问,stack可以模拟递归过程,而next_permutation函数可以生成某个序列的所有排列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值