Skip to content
View evanhyd's full-sized avatar
  • Box

Highlights

  • Pro

Block or report evanhyd

Block user

Prevent this user from interacting with your repositories and sending you notifications. Learn more about blocking users.

You must be logged in to block users.

Please don't include any personal information such as legal names or email addresses. Maximum 100 characters, markdown supported. This note will be visible to only you.
Report abuse

Contact GitHub support about this user’s behavior. Learn more about reporting abuse.

Report abuse
evanhyd/README.md

How to cheese CS midterm exam

When the professor forgot to specify "recursion only" cheese_midterm

bad std::string constructor

Too Perfect Forwarding

  class FlagTree {
    T _data;

    template <typename ...Args>
    FlagTree(Args&&... args) : 
      _data(std::forward<Args>(args)...) {
    }

    FlagTree(const FlagTree& rhs) : 
      _data(rhs._data) {
    }

    FlagTree(FlagTree& rhs) :
      FlagTree(const_cast<const FlagTree&>(rhs)) {
      //override perfect forwarding from the variadic constructor
      //otherwise it doesn't even compile, wtf's going on
    }
  };

Smuggle default arguments into variadic function (credit to M.M)

struct Bar {
    int a;
    const std::source_location srce;
    Bar(int b, const std::source_location s = std::source_location::current()) : a(b), srce(s) {}
}

void Foo(Bar arg0, const auto&... args) {
  //do something
}

// integer "1" implicitly converts to Bar
Foo(1, 2, 3, 4, 5, 6);

Hmmmm.... arr[i] = *(arr + i) = *(i + arr) = i[arr]

int arr[] = {1, 2, 3};
for (int i = 0; i < 3; ++i) {
    i[arr] = 0;
}

Modern C++ duck typing meme

auto Add(const auto&... a) {
    return (... + a);
}

int main() {
    auto c0 = Add(-1, 2, -3);
    auto c1 = Add(1.2, 3.4, -3.1415926535);
    auto c2 = Add(string("unbox"), string("the"), string("cat"));
}

Extract keys from map

map<int, string> mp = {{1, "foo"}, {2, "bar"}};

auto entry = mp.extract(1);
entry.key() = 3;
mp.insert(std::move(entry));

Good variable names to write obfuscated codes

src, srce                   (source)
dst, dest                   (destination)
first, last                 (denote range [a, b])
i, j, k, x, y, z            (index for nested loops)
prev, curr, next            (parent/child nodes)
arr                         (array)
vec                         (vector)
deq                         (deque)
que                         (queue)
ls, lst                     (list)
mp                          (map/hashmap)
addr                        (address)
ptr                         (pointer)
fn, fun, func               (function)

Competitive Programming Hacks

const auto _ = cin.tie(nullptr)->sync_with_stdio(false);

//not portable
#if defined _WIN32 || defined _WIN64
inline char getchar_unlocked() { return static_cast<char>(_getchar_nolock()); }
#endif

template <std::signed_integral T>
T Read() {
    T x; bool neg = false; char c{};
    do { c = getchar_unlocked(); if (c == '-') neg = true; } while (c < '0');
    for (x = c - '0'; '0' <= (c = getchar_unlocked()); x = (x << 3) + (x << 1) + c - '0') {}
    return neg ? -x : x;
}

template <std::unsigned_integral T>
T Read() {
    T x; char c{};
    do { c = getchar_unlocked(); } while (c < '0');
    for (x = c - '0'; '0' <= (c = getchar_unlocked()); x = (x << 3) + (x << 1) + c - '0');
    return x;
}

Cool resources

Godbolt
Benchmark
C++ operator overloading guides
C++ Core Guidelines
Contest Events
Collaboration Coding
visitor counter

Pinned Loading

  1. MeowyPlayer MeowyPlayer Public

    Go 20 5

  2. CSGO_SneakyKitty CSGO_SneakyKitty Public

    C++ 12

  3. sgl sgl Public

    Standard-Generic-Library (SGL)

    Go 2

  4. DataStructure DataStructure Public

    Data Structure

    C++ 3

  5. TetrioDestroyer TetrioDestroyer Public

    A Tetris bot that plays on tetr.io with the pure intention of destroying the opponent.

    Go 4

  6. KittyEngineV4 KittyEngineV4 Public

    C++ 5