[C++基础] tips

1. 在g++ 中使支持C++11

https://askubuntu.com/questions/773283/how-do-i-use-c11-with-g

This you can do by using the -std=c++11 flag. Here's an example:

g++ -std=c++11 -Wall -Wextra -Werror main.cpp -o main

This mode can be selected with the -std=c++11 command-line flag, or -std=gnu++11 to enable GNU extensions as well.(source)

See the explanation of the other flags below. I deeply believe that using at least those error flags will make your life easier in the long run. Once you have better knowledge of what your script does, you can omit warnings if needed to achieve a result but it should not be the standard. Hope this helps you. Here's a good place to start reading.

  • -Wall — enables all major warnings.
  • -Wextra — enables other important warnings.
  • -Werror — make all warnings into errors, causing compilations to fail if any warnings are reported.

2. ios_base::sync_with_stdio(false)

决定C++标准streams(cin,cout,cerr...)是否与相应的C标准程序库文件(stdin,stdout,stderr)同步,也就是是否使用相同的stream缓冲区,缺省情况是同步的,但由于同步会带来某些不必要的负担,因此该函数作用就是我们自己可以取消同步 std::ios::sync_with_stdio(false);   
注意:必须在任何io操作之前取消同步   
函数返回前一次被调用的参数值,如果未被调用过,返回true,反映标准stream的默认的值

3. cin.tie(NULL)

在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。

4. C++删除标点符号和空格的函数

1 s.erase(remove_if (s.begin(), s.end(), static_cast<int(*)(int)>(&ispunct) ), s.end());
2         
3 s.erase(remove_if(s.begin(), s.end(), static_cast<int(*)(int)>(&isspace) ), s.end());