经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
C++语法小技巧
来源:cnblogs  作者:自为风月马前卒  时间:2018/10/16 9:36:07  对本文有异议

前言

写的很乱,各种内容都有。仅仅是为了记录一下

而且内容极其不严谨(没错,只有实践,没有理论)!请各位谨慎驾驶!

强制内联

  1. #define Inline __inline__ __attribute__((always_inline))

本地测试结果:

  • 开O2之后inline和Inline加不加没啥用

  • 不开O2时inline可能会有负优化,而Inline会让程序快很多

当然也可以强制不inline

直接在函数名前加

  1. __attribute__((noinline))

利用位运算实现大小写转化

可以这么写

  1. char ToUpper(char a) {return (a >= 'a' && a <= 'z') ? a ^ ' ' : a;}

实测比c++内置的toupper快6倍。。

enum类型

这玩意儿叫“枚举”

格式如下:

enum [enumeration name] {enumerator1[=value1], enumerator2[=value2], ...};

其中,第二个变量的取值默认是第一个变量取值+1,第一个默认是0,当然也可以自己设定

一个简单的栗子

  1. enum NOIP {a, b, c, d = 22};
  2. cout << c << " " << d;

将会输出2 22

自定义输入输出流

这部分有点硬核啊。。

一个简单的栗子是这样的

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Pair {
  4. private:
  5. int id;
  6. string s;
  7. public:
  8. friend ostream& operator << (ostream& os, Pair& a) {
  9. os << a.s << ":" << a.id << "\n";
  10. return os;
  11. }
  12. friend istream& operator >> (istream& is, Pair& a) {
  13. is >> a.s >> a.id;
  14. return is;
  15. }
  16. };
  17. int main( ) {
  18. Pair a;
  19. cin >> a;
  20. cout << a;
  21. return 0;
  22. }
  23. //input: abc 123
  24. //output : abc:123

注意这里我们实际上还是在用cin / cout输入输出

输入输出流在OI中常常应用于输入输出优化。

  1. struct InputOutputStream {
  2. enum { SIZE = 1000001 };
  3. char ibuf[SIZE], *s, *t, obuf[SIZE], *oh;
  4. InputOutputStream() : s(), t(), oh(obuf) {}
  5. ~InputOutputStream() { fwrite(obuf, 1, oh - obuf, stdout); }
  6. inline char read() {
  7. if (s == t) t = (s = ibuf) + fread(ibuf, 1, SIZE, stdin);
  8. return s == t ? -1 : *s++;
  9. }
  10. template <typename T>
  11. inline InputOutputStream &operator>>(T &x) {
  12. static char c;
  13. static bool iosig;
  14. for (c = read(), iosig = false; !isdigit(c); c = read()) {
  15. if (c == -1) return *this;
  16. iosig |= c == '-';
  17. }
  18. for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
  19. if (iosig) x = -x;
  20. return *this;
  21. }
  22. inline void print(char c) {
  23. if (oh == obuf + SIZE) {
  24. fwrite(obuf, 1, SIZE, stdout);
  25. oh = obuf;
  26. }
  27. *oh++ = c;
  28. }
  29. template <typename T>
  30. inline void print(T x) {
  31. static int buf[23], cnt;
  32. if (x != 0) {
  33. if (x < 0) print('-'), x = -x;
  34. for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
  35. while (cnt) print((char)buf[cnt--]);
  36. } else print('0');
  37. }
  38. template <typename T>
  39. inline InputOutputStream &operator<<(const T &x) {
  40. print(x);
  41. return *this;
  42. }
  43. } io;

template

template,中文名:模板

分为两类,一种叫类模板,一种叫函数模板

类模板我用的不多

函数模板用的多一些

下面是一个求最大值的模板,c++的标准库中也是这么实现的,因此同时存在的话会引起CE

  1. template <typename T>
  2. inline T const& max(T const &a, T const &b) {
  3. return a > b ? a : b;
  4. }

如果直接调用的话,当\(a, b\)的类型不同时会引起CE。

这时可以直接强制类型转化

  1. int a = 1e9;
  2. long long b = 1e18;
  3. long long c = max<int>(a, b);
  4. //the output is 1e9
  5. int a = 1e9;
  6. long long b = 1e18;
  7. long long c = max<long long>(a, b);
  8. //the output is 1e18

预编译黑科技

第一条是强制开栈空间

后面的并不清楚在干啥,貌似可以强制\(O_2\)

  1. #pragma comment(linker, "/STACK:102400000,102400000")
  2. #pragma GCC diagnostic error "-std=c++11"
  3. #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)
  4. #pragma GCC target("avx","sse2")

__builtin系列

  • __builtin_popcount(unsigned int n)

计算\(n\)的二进制表示中有多少个1

  • __builtin_parity(unsigned int n)

判断\(n\)的二进制表示中1的个数奇偶性(要你何用?)

  • __builtin_ffs(unsigned int n)

判断\(n\)的二进制末尾最后一个1的位置,从1开始

  • __builtin_ctz(unsigned int n)

判断\(n\)的二进制末尾\(0\)的个数

  • __builtin_clz(unsigned int n)

判断\(n\)的二进制前导0的个数

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号