C++ 语法糖实用举例

set 从大到小

原理未知(菜

set<int,greater<int> >s;

int main(){
    s={1,2,3,4,5,6};
    for(int v:s)
        cout<<v<<" ";//6 5 4 3 2 1

    return 0;
}

priority_queue从小到大

priority_queue<int,vector<int>,greater<int> >que;

int main(){
    que.emplace(1);
    que.emplace(2);
    que.emplace(3);
    while(!que.empty()){
        cout<<que.top()<<" ";
        //1 2 3
        que.pop();
    }
    return 0;
}

一种树状数组写法

inline int lowbit(int x){return x&-x;}
struct bit{
    vector<ll> v;
    bit(int n):v(n+1){}//!
    ll sum(int x){
        ll res=0;
        while(x){
            res+=v[x];
            x-=lowbit(x);
        }
        return res;
    }
    void add(int x,int y){
        while(x<v.size()){
            v[x]+=y;
            x+=lowbit(x);
        }
    }
};

你可能感兴趣的:(技巧)