zoukankan      html  css  js  c++  java
  • C++的一些奇奇怪怪的小技巧(有用到过持续更)

    1.next_permutaion(a,a+n):

    用于一个数组的全排列(从当前数组状况开始,所以要真正生成全排列之前先排序)。用法如下。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int main(){
     5     int a[3];
     6     a[0]=1;a[1]=2;a[2]=3;
     7     do{
     8         cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
     9     }while (next_permutation(a,a+3)); 
    10 }

    2.__builtin_popcount(i):

    用于计算i的二进制位数中共有几个1.

    3.bitset<maxn>:

    相当于一个数组,但每位只能为0或1.暴力时用到可以增加效率。

    4.mt19937:

    生成随机数(范围比rand()更大,可以不指定也可以指定)。使用方法如下:

    1 #include <bits/stdc++.h>
    2 using namespace std;
    3 mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
    4 int main(){
    5     uniform_int_distribution<int> dist(0,1000);
    6     cout<<mt()<<"
    ";   //不指定范围
    7     cout<<dist(mt)<<"
    ";   //限制范围
    8     return 0;
    9 }

    5.shuffle:

    用于随机排列数组。用法: shuffle(a,a+maxn,mt); 

    6.rotate:

    讲第二个参数到第三个参数区间内(左闭右开)的元素移到第一个参数之前。用法: rotate(b,b+n-1,b+n); 

    7.__builtin_clz(i):

    计算i的二进制有多少个前导零。

  • 相关阅读:
    URAL 1658. Sum of Digits(DP)
    HDU 4433 locker(SPFA+DP)
    Vijos 1092 全排列
    POJ 1141 Brackets Sequence(DP)
    HDU 4597 Play Game
    HDU 1693 Eat the Trees(插头DP)
    USACO 5.4 Twofive(DP)
    USACO 5.5 Hidden Password(搜索+优化)
    USACO 5.5 Picture(周长并)
    USACO 5.4 Telecowmunication(最大流+枚举)
  • 原文地址:https://www.cnblogs.com/charles1999/p/12389947.html
Copyright © 2011-2022 走看看