GithubHelp home page GithubHelp logo

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

UnboxTheCat's Projects

assault-cube-external-trainer icon assault-cube-external-trainer

External trainer made in C++ that utilizes winapi to read/write process memory. Can change health, ammo, armour, and teleport to co-ordinates.

kittyalarm icon kittyalarm

A simple concept that proves you don't have to invest 12 * 7 hours a week to develop a server for submitting the attendance, lol

mazesolver icon mazesolver

A maze solver that scans a maze image and generate solution in animated gif file.

neuralnetwork icon neuralnetwork

A tutorial code from David Miller http://www.millermattson.com/dave/

reversithegoose icon reversithegoose

Reversi Engine, rotated bitboard for move generation, and custom neural network for board evaluation.

sgl icon sgl

Standard-Generic-Library (SGL)

tetriodestroyer icon tetriodestroyer

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

theforestv2 icon theforestv2

Unique features: Change player skins + clothes, invisible, teleporting outside of the game boundary, spawn death bunny to crash the server,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.