0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

競プロで入出力が便利になるであろう万能関数とかのテンプレート

Last updated at Posted at 2025-03-16

経緯

C++のcout << anscin >> Nなんかが憂鬱に感じたから(昔)
配列の入出力を楽にするのとほかの人が読むときにわかりやすいように(今)

コード

template.cpp
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
#define v(type) vector<type>
template<class T>istream&operator>>(istream&i,vector<T>&v){for(auto&x:v){i>>x;};return i;}
template<class T>istream&operator>>(istream&i,vector<vector<T>>&v){for(auto&x:v){i>>x;};return i;}
template<class T>ostream&operator<<(ostream&o,const vector<T>&v){for(auto&x:v){o<<x;if(&x!=&v.back()){o<<' ';};};return o;}
template<class T>ostream&operator<<(ostream&o,const vector<vector<T>>&v){for(auto&x:v){o<<x;if(&x!=&v.back()){o<<'\n';};};return o;}
template<class T,class U>inline basic_ostream<T,U>&endo(basic_ostream<T,U>&o){return o.put('\n');}

class Sub {
public:
int solve() {
    print_set("Yes","No");
    
    // ここにコードを書いていきましょう!!!

    return 0;
};
private:
void print(){cout<<'\n';}
template<class T>void print(vector<T>v,bool m){if(m){for(const auto&x:v){cout<<x;};print();}else{for(const auto&x:v){print(x);};};}
template<class T>void print(vector<vector<T>>v,bool m){print(v,m);}
string YesPrintBollSetting="",NoPrintBollSetting="";
void print_set(string Y,string N){YesPrintBollSetting=Y;NoPrintBollSetting=N;}
void print(bool v){cout<<(v?YesPrintBollSetting:NoPrintBollSetting)<<'\n';}
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return true;}else{return false;};}
template<class T>bool chmin(T&a,T b){if(a>b){a=b;return true;}else{return false;};}
template<class T>T armax(const vector<T>&v){return *max_element(v.begin(),v.end());}
template<class T>T armin(const vector<T>&v){return *min_element(v.begin(),v.end());}
template<class T>T arsum(const vector<T>&v){return reduce(v.begin(),v.end());}
template<class T>T arsum(const vector<vector<T>>&v){T M=0;for(auto x:v){M+=arsum(x);}return M;}
template<class T>void sort_u(vector<T>&v){sort(v.begin(),v.end());}
template<class T,class...U>void sort_u(vector<T>&a,vector<U>&...b){sort_u(a);sort_u(b...);}
template<class T>void sort_d(vector<T>&v){sort(v.rbegin(),v.rend());}
template<class T,class...U>void sort_d(vector<T>&a,vector<U>&...b){sort_d(a);sort_d(b...);}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios::fixed);
    cout.precision(16);
    Sub *sub = new Sub();
    int back = sub->solve();
    delete sub;
    return back;
}

分解して解説

言わずもがな

#include <bits/stdc++.h>
using namespace std;

コンパイルらへん

上の奴は配列の範囲外を指定したときにエラー出してくれる奴
#if#endifが何か分からない!という方は 条件付きコンパイル と検索してみよう!
__has_includeというのは、それをインクルードできるなら1を返すというもの
下にしれっという奴はvectorの省略ですね

#define _GLIBCXX_DEBUG
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
#define v(type) vector<type>

入出力オペレーター

配列Aを入力するときcin >> A;と書けるのが強い!
出力の時もcout << A;と書ける!1
一番下の奴はcout << endo;と書いたら改行してくれる奴(endoな理由はendlにキー配置が近かっただけ)

template<class T>istream&operator>>(istream&i,vector<T>&v){for(auto&x:v){i>>x;};return i;}
template<class T>istream&operator>>(istream&i,vector<vector<T>>&v){for(auto&x:v){i>>x;};return i;}
template<class T>ostream&operator<<(ostream&o,const vector<T>&v){for(auto&x:v){o<<x;if(&x!=&v.back()){o<<' ';};};return o;}
template<class T>ostream&operator<<(ostream&o,const vector<vector<T>>&v){for(auto&x:v){o<<x;if(&x!=&v.back()){o<<'\n';};};return o;}
template<class T,class U>inline basic_ostream<T,U>&endo(basic_ostream<T,U>&o){return o.put('\n');}

print関数

さっきのcoutじゃ賄いきれないところをカバーしてくれる
print_setsolve関数の一番上で宣言してるよ

void print(){cout<<'\n';}
template<class T>void print(vector<T>v,bool m){if(m){for(const auto&x:v){cout<<x;};print();}else{for(const auto&x:v){print(x);};};}
template<class T>void print(vector<vector<T>>v,bool m){print(v,m);}
string YesPrintBollSetting="",NoPrintBollSetting="";
void print_set(string Y,string N){YesPrintBollSetting=Y;NoPrintBollSetting=N;}
void print(bool v){cout<<(v?YesPrintBollSetting:NoPrintBollSetting)<<'\n';}

色々な関数

ご存知の通りのchoose比較関数
配列対応したmax,min関数。max_elementとか便利なもの作ってくれますね(感謝)
二次元配列には対応してなかったので自分で実装
可変長引数に対応したソートしてくれる奴

template<class T>bool chmax(T&a,T b){if(a<b){a=b;return true;}else{return false;};}
template<class T>bool chmin(T&a,T b){if(a>b){a=b;return true;}else{return false;};}
template<class T>T armax(const vector<T>&v){return *max_element(v.begin(),v.end());}
template<class T>T armin(const vector<T>&v){return *min_element(v.begin(),v.end());}
template<class T>T arsum(const vector<T>&v){return reduce(v.begin(),v.end());}
template<class T>T arsum(const vector<vector<T>>&v){T M=0;for(auto x:v){M+=arsum(x);}return M;}
template<class T>void sort_u(vector<T>&v){sort(v.begin(),v.end());}
template<class T,class...U>void sort_u(vector<T>&a,vector<U>&...b){sort_u(a);sort_u(b...);}
template<class T>void sort_d(vector<T>&v){sort(v.rbegin(),v.rend());}
template<class T,class...U>void sort_d(vector<T>&a,vector<U>&...b){sort_d(a);sort_d(b...);}

main関数

上から
C言語の標準入出力との連携を解除する。
通常ではbasic_ostream::flush()(フラッシュ)を入力を受けるたびに実行するためそれを辞めさせる。
少数が3.14e10みたいな指数表記になるのを防ぐ。
少数の桁数が引数分まで表示されるようになる。(doubleの15桁の一つ上に設定)
ここから下はクラスのメモリ管理など

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios::fixed);
    cout.precision(16);
    Sub *sub = new Sub();
    int back = sub->solve();
    delete sub;
    return back;
}

見たい人用

コードを整形した奴.cpp
#include <bits/stdc++.h>
using namespace std;
// 配列の範囲外参照を防ぐ
#define _GLIBCXX_DEBUG
// atcoderライブラリのインクルード
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
// vectorの省略
#define v(type) vector<type>
// 入出力オペレーター
template<class T>
istream& operator>>(istream &i, vector<T> &v) {
    for (auto &x: v) {
        i >> x;
    };
    return i;
}
template<class T>
istream &operator>>(istream &i, vector<vector<T>> &v) {
    for (auto &x: v) {
        i >> x;
    };
    return i;
}
template<class T>
ostream &operator<<(ostream &o, const vector<T> &v) {
    for (auto &x: v) {
        o << x;
        if (&x != &v.back()) {
            o << ' ';
        };
    };
    return o;
}
template<class T>
ostream &operator<<(ostream &o, const vector<vector<T>> &v) {
    for (auto &x: v) {
        o << x;
        if (&x != &v.back()) {
            o << '\n';
        };
    };
    return o;
}
template<class T, lass U>
inline basic_ostream<T, U> &endo(basic_ostream<T, U> &o) {
    return o.put('\n');
}
// 実行クラス
class Sub {
public:
int solve() {
    print_set("Yes","No");
    
    // ここにコードを書いていきましょう!!!

    return 0;
};
private:
// print関数
void print() {
    cout << '\n';
}
template<class T>
void print(const vector<T> &v, bool m) {
    if (m) {
        for (const auto&x :v) {
            cout << x;
        };
        print();
    } else {
        for(const auto&x :v) {
            print(x);
        };
    };
}
template<class T>
void print(const vector<vector<T>> &v, bool m) {
    print(v, m);
}
// 競プロ出力
string YesPrintBollSetting="",NoPrintBollSetting="";
void print_set(string Y, string N) {
    YesPrintBollSetting=Y;
    NoPrintBollSetting=N;
}
void print(bool v) {
    cout << (v ? YesPrintBollSetting : NoPrintBollSetting) << '\n;
}
// chooseのmax,min
template<class T>
bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    };
}
template<class T>
bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    };
}
// max,minの配列対応
template<class T>
T armax(const vector<T> &v) {
    return *max_element(v.begin(), v.end());
}
template<class T>
T armin(const vector<T> &v) {
    return *min_element(v.begin(), v.end());
}
// sum配列
template<class T>
T arsum(const vector<T> &v) {
    return reduce(v.begin(), v.end());
}
template<class T>
T arsum(const vector<vector<T>> &v) {
    T M=0;
    for (auto x: v) {
        M += arsum(x);
    };
    return M;
}
//sort関数
template<class T>
void sort_u(vector<T> &v) {
    sort(v.begin(), v.end());
}
template<class T, class...U>
void sort_u(vector<T> &a, vector<U> &...b) {
    sort_u(a);
    sort_u(b...);
}
template<class T>
void sort_d(vector<T> &v) {
    sort(v.rbegin(), v.rend());
}
template<class T ,class...U>
void sort_d(vector<T> &a, vector<U> &...b) {
    sort_d(a);
    sort_d(b...);
}
};
// main関数
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.setf(ios::fixed);
    cout.precision(16);
    Sub *sub = new Sub();
    int back = sub->solve();
    delete sub;
    return back;
}

注意点

・これらのコードは勝手に使ってもよい
・テンプレート作るのが楽しくなってきたまである

  1. 新しく記事が書けるレベルのアドレスの使い方してるかも...

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?