少见使用函数收集(待更新)

比较少见的函数

高效位计算

  • int __builtin_ffs (unsigned int x)
    返回倒数第一个1的位置,0返回0
  • int __builtin_clz (unsigned int x)
    返回x有多少个前导0
  • int __builtin_ctz (unsigned int x)
    返回x有多少个后置0
  • int __builtin_popcount (unsigned int x)
    返回书有多少个1
  • int __builtin_parity(unsigned int x)
    奇偶检验奇数返回1,比如7返回1


    当调用的数位 long long型 在每个函数后添加ll类型比如:__builtin_clzll(x)
    样列:
int a=8;
cout<<__builtin_ffs(a)<<endl;
cout<<__builtin_clz(a)<<endl;
cout<<__builtin_ctz(a)<<endl;
int b=7;
cout<<__builtin_popcount(b)<<endl;
cout<<__builtin_parity(b)<<endl;

输出:

4
28
3
3
1

生成字典序

  • template< class BidirIt >bool next_permutation( BidirIt first, BidirIt last );
    按默认定义大小找第一个字典序大于他的序列
  • template< class BidirIt, class Compare >bool next_permutation( BidirIt first, BidirIt last, Compare comp );
    自己定义大小comp函数,找第一个字典序大于他的序列
    找上一个函数是prev_permutation();
    样列:
int ar[10]={2,6,5,4,9,3};
int br[10]={2,6,5,4,9,3};

prev_permutation(ar,ar+6);
next_permutation(br,br+6);

for(int i=0;i<6;i++) cout<<ar[i]<<" ";
cout<<endl;

for(int i=0;i<6;i++) cout<<br[i]<<" ";
cout<<endl;

输出:

2 6 5 4 3 9
2 6 5 9 3 4
posted @ 2020-07-22 20:15  Grisaia  阅读(116)  评论(0)    收藏  举报