CCF刷题遇坑笔记————后期不断完善

    科技2022-08-09  111

    文章目录

    0 背景1 pair错误2 unordered_map错误3 复制数组4 调试崩溃5 题目给的测试案例正确,但是提交后却错误6 int转string7 尽量使用math.h头文件,而不是cmath

    0 背景

    最近刷ccf的时候,总是会遇到在Dev CPP上编译、运行都没有问题,结果一复制到ccf的网页编译器中,一提交就爆出编译错误(天啦噜,什么上古编译器??!! )。 结果就只有逐语句注释,来寻找编译出错的地方和原因,此文就是用于记录这样的错误以及解决办法。

    1 pair错误

    如果使用

    #include<vector> #include<utility> std::vector<std::pair<int, int>> test;

    就会爆出编译错误,解决方法,使用结构体(因为pair本身就是有两个元素的结构体):

    typedef struct Data{ int first, second; Data(int _first, int _second):first(_first), second(_second){}//初始化列表 }data; std::vector<data> test;

    2 unordered_map错误

    使用unordered_map会爆出编译错误,解决方法:

    #include <tr1/unordered_map> namespace std{ using std::tr1::unordered_map; }

    就可以正常使用:

    //例如 std::unordered_map<int, bool> A; int index, value; scanf("%d%d", &index, &value); A.push_back(data(index, value));

    3 复制数组

    使用下面代码复制数组时,会报错:

    #include<algorithm> const int MAXV = 510; int inDegree[MAXV] = {0};//入度 int tempInDegree[MAXV];//临时存储入度 std::copy(std::begin(inDegree), std::end(inDegree), std::begin(tempInDegree));

    解决方法,使用memcpy:

    #include<string.h> memcpy(tempInDegree, inDegree, (m+n)* sizeof(int));//复制的字节数

    4 调试崩溃

    如果调试程序时,软件崩溃,如下图所示: 解决方法:把软件模式从发行版本模式变为调试模式。

    5 题目给的测试案例正确,但是提交后却错误

    自己多自己模拟数据,比如边界测试等。

    6 int转string

    使用C++11的全局函数std::to_string()会爆出编译错误(难道连C++11都不支持嘛。。。 ),结果只好使用流转换。

    //#include <sstream> string itoString(int num){ stringstream stream; stream << num; return stream.str(); } string str("hello"); str += itoString(1234); cout << str;

    7 尽量使用math.h头文件,而不是cmath

    如果使用cmath头文件,当使用std::round时,系统就会报错 但是如果使用math.h头文件,使用round时,系统就不会报错。

    Processed: 0.015, SQL: 9