[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());

 

转载于:https://www.cnblogs.com/shiyublog/p/10629307.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值