当前位置: 首页 > news >正文

C++语法小技巧

前言

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

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

强制内联

#define Inline __inline__ __attribute__((always_inline))

本地测试结果:

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

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

当然也可以强制不inline

直接在函数名前加

__attribute__((noinline))

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

可以这么写

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

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

enum类型

这玩意儿叫“枚举”

格式如下:

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

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

一个简单的栗子

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

将会输出2 22

自定义输入输出流

这部分有点硬核啊。。

一个简单的栗子是这样的

#include<bits/stdc++.h>
using namespace std;
class Pair {
private: 
    int id;
    string s;
public:
    friend ostream& operator << (ostream& os, Pair& a) {
        os << a.s << ":" << a.id << "\n";
        return os;      
    }
    friend istream& operator >> (istream& is, Pair& a) {
        is >> a.s >> a.id;
        return is;      
    }
};
int main( ) {
    Pair a;
    cin >> a;
    cout << a;
    return 0;
}
//input: abc 123
//output : abc:123

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

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

struct InputOutputStream {
    enum { SIZE = 1000001 };
    char ibuf[SIZE], *s, *t, obuf[SIZE], *oh;

    InputOutputStream() : s(), t(), oh(obuf) {}
    ~InputOutputStream() { fwrite(obuf, 1, oh - obuf, stdout); }

    inline char read() {
        if (s == t) t = (s = ibuf) + fread(ibuf, 1, SIZE, stdin);
        return s == t ? -1 : *s++;
    }

    template <typename T>
    inline InputOutputStream &operator>>(T &x) {
        static char c;
        static bool iosig;
        for (c = read(), iosig = false; !isdigit(c); c = read()) {
            if (c == -1) return *this;
            iosig |= c == '-';
        }
        for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
        if (iosig) x = -x;
        return *this;
    }

    inline void print(char c) {
        if (oh == obuf + SIZE) {
            fwrite(obuf, 1, SIZE, stdout);
            oh = obuf;
        }
        *oh++ = c;
    }

    template <typename T>
    inline void print(T x) {
        static int buf[23], cnt;
        if (x != 0) {
            if (x < 0) print('-'), x = -x;
            for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
            while (cnt) print((char)buf[cnt--]);
        } else print('0');
    }

    template <typename T>
    inline InputOutputStream &operator<<(const T &x) {
        print(x);
        return *this;
    }
} io;

template

template,中文名:模板

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

类模板我用的不多

函数模板用的多一些

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

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

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

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

    int a = 1e9;
    long long b = 1e18;
    long long c = max<int>(a, b);
//the output is 1e9

    int a = 1e9;
    long long b = 1e18;
    long long c = max<long long>(a, b);
//the output is 1e18

预编译黑科技

第一条是强制开栈空间

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

#pragma comment(linker, "/STACK:102400000,102400000") 
#pragma GCC diagnostic error "-std=c++11"
#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)
#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的个数

指针的骚操作

通过指针实现负下标数组

#include<bits/stdc++.h>
using namespace std;
int main() {
    int __a[21];
    for(int i = 0; i <= 20; i++) __a[i] = i;
    int *const a = &__a[10];
    printf("%d %d %d", a[7], a[0], a[-7]);
}

相关文章:

  • MeiTuanLocateCity仿美团城市列表选择界面
  • React Native安卓模拟器调出Dev Setting菜单
  • Hibernate各保存方法之间的差 (save,persist,update,saveOrUpdte,merge,flush,lock)等一下
  • 少侠请重新来过 - Vue学习笔记(五) - 指令
  • AIX 系统 EBS form 打开报错FRM-92101: FORM server在启动过程中失败
  • JS面向对象编程
  • 解决Eclipse报errors running builder ‘javascript validator’ on project
  • 测试代码覆盖率工具学习(Android Emma)
  • c语言学习三
  • 微信群里的这些文章,都是谣言!赶紧给爸妈看看
  • 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
  • JFreeChart绘制XY折线图(工具类设计)
  • ORACLE数据库笔记之PL/SQL
  • ByteTCC 0.5.0-ALPHA1 发布,基于 TCC 的分布式事务管理器
  • 马哥-51CTO-Linux培训-0901-linux文件系统
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • es6(二):字符串的扩展
  • ES6系列(二)变量的解构赋值
  • javascript 总结(常用工具类的封装)
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • Python学习笔记 字符串拼接
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 普通函数和构造函数的区别
  • 使用 QuickBI 搭建酷炫可视化分析
  • 字符串匹配基础上
  • ​一文看懂数据清洗:缺失值、异常值和重复值的处理
  • ###项目技术发展史
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (层次遍历)104. 二叉树的最大深度
  • (二)学习JVM —— 垃圾回收机制
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (七)Knockout 创建自定义绑定
  • (万字长文)Spring的核心知识尽揽其中
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • (转载)利用webkit抓取动态网页和链接
  • .net 7 上传文件踩坑
  • .net core 控制台应用程序读取配置文件app.config
  • .Net Core和.Net Standard直观理解
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。
  • @angular/cli项目构建--http(2)
  • [<MySQL优化总结>]
  • [20140403]查询是否产生日志
  • [8-27]正则表达式、扩展表达式以及相关实战
  • [AUTOSAR][诊断管理][ECU][$37] 请求退出传输。终止数据传输的(上传/下载)
  • [BZOJ 3282] Tree 【LCT】
  • [bzoj1901]: Zju2112 Dynamic Rankings
  • [C++] 默认构造函数、参数化构造函数、拷贝构造函数、移动构造函数及其使用案例
  • [C++数据结构](31)哈夫曼树,哈夫曼编码与解码
  • [cogs2652]秘术「天文密葬法」