刷题注意点

1、我在使用Code::Blocks16.01写C++代码的时候,尽管我已经勾选了编译器"-std=c++11"的选项,但在使用stoi()函数时,仍然会报错“error: 'stoi' was not declared in this scope”,查了一下,发现是这个版本编译器本身的问题,详见这里的说明,下载最新的Code::Blocks版本应该就没问题了。不过,鉴于考场编译器都不是最新的,可用比较繁琐的替代方案,即用C的atoi()函数(头文件<stdlib.h>),如下:

string str="-123";
//int number=stoi(str);//改成如下
int number=atoi(str.c_str());

2、set集合自动排序的问题,看下面代码,注意区别(我也是今天刚发现)

#include <iostream>
#include <string>
#include <set>
using namespace std;

int main()
{
    set<string> st1;
    st1.insert("-123");
    st1.insert("-124");
    st1.insert("-125");
    for(auto it:st1) cout<<it<<' ';//输出-123 -124 -125
    cout<<'\n';
set<int> st2; st2.insert(-123); st2.insert(-124); st2.insert(-125); for(auto it:st2) cout<<it<<' ';//输出-125 -124 -123 return 0; }

若set集合里的元素是string,则其自动排序的规则是逐个字符进行比较,所以才会输出"-123 -124 -125",再看下面

#include <iostream>
#include <string>
#include <set>
using namespace std;

int main()
{
    set<string> st1;
    st1.insert("-123");
    st1.insert("-124");
    st1.insert("-125");
    st1.insert("-1234");
    for(auto it:st1) cout<<it<<' ';//输出-123 -1234 -124 -125
  
    return 0;
}

可见,之所以"-1234"排在"-124"的前面,是因为两者前3个位置一致,而第4个位置"-1234"更小;同理,"-1234"排在"-123"的后面。综上,若set集合元素是string型的,而存的又恰是数字型字符串,其排序结果和我们直觉认为的按数值大小进行排序是不一样的,慎用!!!

3、在Code::Blocks里批量替换变量名用Ctrl+R

猜你喜欢

转载自www.cnblogs.com/kkmjy/p/9537134.html