👩‍💻

競プロC++のコードスニペット

2023/12/05に公開

これはKogakuin Univ Advent Calendar 2023の記事です.

書くことがなかったので競プロ(C++)のコードスニペットを載せます.

コードスニペット

C++スニペット
{
  "kyopro": {
    "prefix": "kyopro",
    "body": [
      "using namespace std;",
      "",
      "#include <algorithm>",
      "#include <array>",
      "#include <bitset>",
      "#include <cassert>",
      "#include <cctype>",
      "#include <cfenv>",
      "#include <cfloat>",
      "#include <chrono>",
      "#include <cinttypes>",
      "#include <climits>",
      "#include <cmath>",
      "#include <complex>",
      "#include <cstdarg>",
      "#include <cstddef>",
      "#include <cstdint>",
      "#include <cstdio>",
      "#include <cstdlib>",
      "#include <cstring>",
      "#include <deque>",
      "#include <fstream>",
      "#include <functional>",
      "#include <initializer_list>",
      "#include <iomanip>",
      "#include <ios>",
      "#include <iostream>",
      "#include <istream>",
      "#include <iterator>",
      "#include <limits>",
      "#include <list>",
      "#include <map>",
      "#include <memory>",
      "#include <new>",
      "#include <numeric>",
      "#include <ostream>",
      "#include <queue>",
      "#include <random>",
      "#include <set>",
      "#include <sstream>",
      "#include <stack>",
      "#include <streambuf>",
      "#include <string>",
      "#include <tuple>",
      "#include <type_traits>",
      "#include <typeinfo>",
      "#include <unordered_map>",
      "#include <unordered_set>",
      "#include <utility>",
      "#include <vector>",
      "",
      "#define ll long long",
      "",
      "template <typename T>",
      "using V = vector<T>;",
      "template <typename T>",
      "using VV = vector<vector<T>>;",
      "using vi = vector<int>;",
      "using vl = vector<long long>;",
      "using vd = V<double>;",
      "using vs = V<string>;",
      "using vvi = vector<vector<int>>;",
      "using vvl = vector<vector<long long>>;",
      "using vvc = vector<vector<char>>;",
      "",
      "template <typename T, typename U>",
      "struct P : pair<T, U> {",
      "    template <typename... Args>",
      "    P(Args... args) : pair<T, U>(args...) {}",
      "",
      "    using pair<T, U>::first;",
      "    using pair<T, U>::second;",
      "",
      "    P &operator+=(const P &r) {",
      "        first += r.first;",
      "        second += r.second;",
      "        return *this;",
      "    }",
      "    P &operator-=(const P &r) {",
      "        first -= r.first;",
      "        second -= r.second;",
      "        return *this;",
      "    }",
      "    P &operator*=(const P &r) {",
      "        first *= r.first;",
      "        second *= r.second;",
      "        return *this;",
      "    }",
      "    template <typename S>",
      "    P &operator*=(const S &r) {",
      "        first *= r, second *= r;",
      "        return *this;",
      "    }",
      "    P operator+(const P &r) const { return P(*this) += r; }",
      "    P operator-(const P &r) const { return P(*this) -= r; }",
      "    P operator*(const P &r) const { return P(*this) *= r; }",
      "    template <typename S>",
      "    P operator*(const S &r) const {",
      "        return P(*this) *= r;",
      "    }",
      "    P operator-() const { return P{-first, -second}; }",
      "};",
      "template <typename T>",
      "using minpq = priority_queue<T, vector<T>, greater<T>>;",
      "using pl = P<long long, long long>;",
      "using pi = P<int, int>;",
      "using vp = V<pl>;",
      "",
      "#define all(v) (v).begin(), (v).end()",
      "#define rall(v) (v).rbegin(), v.rend()",
      "#define each(i, v) for (auto i : v)",
      "#define each2(x, y, v) for (auto [x, y] : v)",
      "#define rep(i, N) for (long long i = 0; i < (long long)(N); i++)",
      "#define repr(i, N) for (long long i = (long long)(N)-1; i >= 0; i--)",
      "#define rep1(i, N) for (long long i = 1; i <= (long long)(N); i++)",
      "#define repr1(i, N) for (long long i = (N); (long long)(i) > 0; i--)",
      "#define reg(i, a, b) for (long long i = (a); i < (b); i++)",
      "#define regr(i, a, b) for (long long i = (b)-1; i >= (a); i--)",
      "// #define fi first",
      "// #define se second",
      "",
      "#define in(a) cin >> a",
      "#define out2(a, b) cout << a << b",
      "#define out(a) cout << a << \"\\n\"",
      "constexpr int INF = 0x3f3f3f3f;",
      "constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;",
      "constexpr double EPS = 1e-8;",
      "constexpr int MOD = 998244353;",
      "// constexpr int MOD = 1000000007;",
      "#define pb push_back",
      "",
      "int main() {",
      "    $1",
      "    return 0;",
      "}",
      ""
    ]
  }
}

拡張機能は「Easy Snippet」を使用しています.
https://marketplace.visualstudio.com/items?itemName=inu1255.easy-snippet

入出力

以下のようにdefineして簡単に書けるようにしています.このような書き方は好ましくないかもしれませんが,今のところ問題は起きてません(灰色).

スニペットの一部
・・・
#define in(a) cin >> a
#define out2(a, b) cout << a << b
#define out(a) cout << a << "\n"
・・・

プログラム例

sample1.cpp
・・・
int main() {
    int N;
    in(N);
    out(N);
・・・
input
1234

output
1234

$1

main関数内の$1はコードスニペットの記法で,$1と書いた位置がタイプカーソルの初期位置になります.
こうすることでコードスニペットを呼び出すと,タイプカーソルがmain関数内にあり,すぐにコードが書き出せる状態になります.

スニペットの一部
・・・
int main() {
    $1
    return 0;
}
・・・


あとは大体よくある競プロテンプレートです.


お疲れ様でした

Discussion