STL及各种技巧

版权声明:转载注明出处就好啦~ https://blog.csdn.net/qq_36693533/article/details/78447479

常用函数:

floor(a);//对a下取整
ceil(a);//对a上取整
log(a);/log2(a);/log10(a)//以e为底的对数

cout<<clock()/double CLOCKS_PER_SEC;//输出程序运行时间
//clock函数返回进程运行时间,但是这个运行时间单位不是秒,而是CPU运行的时钟周期计数。
//需要除以CPU时钟频率,也就是CLOCKS_PER_SEC.以得到一个以秒为单位的数值
//需要头文件<ctime>


强制类型转换:

2LL/2ll = (long long)2;


部分STL:

默认include< algorithm>

reverse(a+1,a+n+1);//翻转一段序列的元素
                   //区间左闭右开
                   //时间复杂度:O(n)
int len=unique(a+1,a+n+1)-a-1;//对一段区间的元素进行去重,去重后将重复元素放于序列末尾
                              //只能去掉连续的重复元素,所以要先sort(...),复杂度O(n)
                              //返回值为序列最后一项的后一项的下标的地址
swap(a,b)//交换a,b的值
int pos=lower_bound(a,a+n+1,x)/upper_bound()-a;
//返回区间内第一个大于等于/大于给定值的元素地址


关于char和stinrg

//适用于char数组
strlen(s);//用于char数组,需要include<cstring>
char a[1010];//注意都要开char数组,只用于char数组
int pos=strstr(s,a)-s;//返回a在s中第一次出现的位置(从零开始)指针
                      //数据范围<=10^5速度快于KMP中next
char a[1010];
int ban=strcmp(s,a);//安装Ascll从左向右比较
                    //若s=a,返回0
                    //若s<a,返回负数为-1
                    //若s>a,返回正数为1

//适用于string
s.length()/s.size();//用于string,include<iostream>
char c;
s.push_back(c);//即s+c,注意c为char类型字符
s.empty(),s.clear(),s.erase(pos,len);//删除从pos处开始长度为len的子串
s.insert(pos,c);//在pos(从零开始)出插入字符或字符串c,注意c和s都必须为string类型
string c=s.substr(pos,len);//返回从pos处开始长度为len的子串
stirng a;
int pos=s.find(a);//返回a在s中第一次出现的位置,对是位置
//+,>,==,<等都可对string使用,但string实在太慢了!!乖乖getchar()后处理吧.


部分事项:
不介绍cout的格式化输出….
格式化输出请使用printf
不可重载int,char等基本类型的运算符。
异或^的优先级小于==

SPFA判负环:

if(!inq[x])
{
    q.push(x);
    inq[x]=1;
    cnt[x]=max(cnt[k]+1,cnt[x]);
    if(cnt[x]>=n+2)
    return;
}

数据生成:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
    srand(time(0));
    int n=rand()%100+1;
    printf("%d\n",n);
    for(int i=1;i<=n;i++)
    {
        int a=rand()%1000+1;//1-1000内的数
        printf("%d ",a);
    }
    return 0;
}



对拍:

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
    int tot=0;
    while(1)
    {
        system("data.exe");
        system("std.exe");
        system("boli.exe");
        if(system("fc std.out boli.out"))
        {
            printf("WA");
            system("pause");
        }
        tot++;
        printf("%d\n",tot);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36693533/article/details/78447479