C++ Grammar Tips

Keywords: C++ Linker

Preface

Written in disorder, all kinds of content. Just to record it.

And the content is extremely imprecise (yes, only practice, no theory)! Please drive carefully!

Compulsory inline

#define Inline __inline__ __attribute__((always_inline))

Local test results:

  • It's no use adding or not adding inline and inline after O 2 is turned on.

  • Inline may have negative optimization without O 2, and Inline will make the program much faster.

Of course, inline can also be mandatory

Add directly to the function name

__attribute__((noinline))

Using Bit Operations to Realize Case Conversion

It can be written like this.

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

The actual measurement is 6 times faster than the built-in toupper in c++.

enum type

It's called enumeration.

The format is as follows:

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

Among them, the default value of the second variable is + 1 for the first variable, and 0 for the first variable. Of course, you can set it by yourself.

A simple chestnut

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

The output will be 222

Custom Input and Output Stream

This part is a bit hard core.

A simple chestnut is like this.

#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

Note that we're actually still using cin / cout input and output here

Input and output streams are often used in OI for input and output optimization.

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, Chinese name: template

There are two kinds, one is called class template and the other is called function template.

I don't use many class templates

Function templates are used more often

Here's a template for maximizing, which is also implemented in the standard library of c++, so it can cause CE if it exists at the same time.

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

If called directly, CE is generated when the type of(a, b) is different.

At this point, type conversion can be directly enforced.

    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

Pre-compiled Black Technology

The first is mandatory stack space

It's not clear what's going on behind it. It seems that it can be mandatory (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 series

  • __builtin_popcount(unsigned int n)

How many 1 are there in computing the binary representation of (n)

  • __builtin_parity(unsigned int n)

Judge the number parity of one in the binary representation of (n) )

  • __builtin_ffs(unsigned int n)

Judge the position of the last 1 at the end of the binary system of(n) from 1

  • __builtin_ctz(unsigned int n)

Judging the Number of Binary End(0)of(n)

  • __builtin_clz(unsigned int n)

Judging the Number of Binary Leader 0 of(n)

Posted by Moocat on Sun, 03 Feb 2019 01:03:15 -0800