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?

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

Posted at

経緯

C++のcout << anscin >> Nなんかが憂鬱に感じたから

コード

template.cpp
#include <bits/stdc++.h>
using namespace std;
// vector省略
#define v(type) vector<type>
// 二次vector省略
#define vv(type) vector<vector<type>>
// print関数
void print(){cout<<'\n';};
template<class T>void print(const T&value){cout<<value;print();};
template<class T>void print(const vector<T>&value){for(const auto&x:value){cout<<x;if(&x!=&value.back()){cout<<' ';};};print();};
template<class T>void print(const vector<vector<T>>&value){for(const auto&x:value){print(x);};};
template<class T,class...U>void print(const T&first,const U&...second){cout<<first;print(second...);};
template<class T,class...U>void print(const vector<T>&first,const U&...second){print(first);print(second...);};
template<class T,class...U>void print(const vector<vector<T>>&first,const U&...second){print(first);print(second...);};
// scanf関数(vectorは可変長引数未対応)
template<class T>void scanf(T&value){cin>>value;};
template<class T>void scanf(vector<T>&value){for(auto&x:value){scanf(x);};};
template<class T>void scanf(vector<vector<T>>&value){for(auto&x:value){scanf(x);};};
template<class T,class...U>void scanf(T&first,U&...second){scanf(first);scanf(second...);};
template<class T,class...U>void scanf(vector<T>&first,U&...second){scanf(first);scanf(second...);};
template<class T,class...U>void scanf(vector<vector<T>>&first,U&...second){scanf(first);scanf(second...);};
// changeのmax,min #include <algorithm>が必須
template<class T>void chmax(T&first,const T&second){first=max(first,second);};
template<class T>void chmin(T&first,const T&second){first=min(first,second);};
// max,minの配列対応
template<class T>T armax(const vector<T>&value,const T&stand){T M;for(int i=0;i<(int)value.size();i++){chmax(M,value[i]);};return M;};
template<class T>T armin(const vector<T>&value,const T&stand){T M;for(int i=0;i<(int)value.size();i++){chmin(M,value[i]);};return M;};
// 競プロ出力
void print(const bool&bo){bo?print("Yes"):print("No");};
void print(const bool&bo,const int&mode){if(mode==0){print(bo);}else if(mode==1){bo?print("YES"):print("NO");}else if(mode==2){bo?print("Even"):print("Odd");};};

分解して解説

言わずもがな

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

vector

これの便利なところは二次元配列のサイズを指定するときvv(int) A(N,v(int)(N))って書ける所

// vector省略
#define v(type) vector<type>
// 二次vector省略
#define vv(type) vector<vector<type>>

print関数

一番上は改行
そこからの前半は実際の入力に持っていく部分
後半は配列かを判定している(ようなもの)

// print関数
void print(){cout<<'\n';};
template<class T>void print(const T&value){cout<<value;print();};
template<class T>void print(const vector<T>&value){for(const auto&x:value){cout<<x;if(&x!=&value.back()){cout<<' ';};};print();};
template<class T>void print(const vector<vector<T>>&value){for(const auto&x:value){print(x);};};
template<class T,class...U>void print(const T&first,const U&...second){cout<<first;print(second...);};
template<class T,class...U>void print(const vector<T>&first,const U&...second){print(first);print(second...);};
template<class T,class...U>void print(const vector<vector<T>>&first,const U&...second){print(first);print(second...);};

scanf関数

前半は実際の入力に持っていく部分
後半は配列かを判定(するようなもの)

template<class T>void scanf(T&value){cin>>value;};
template<class T>void scanf(vector<T>&value){for(auto&x:value){scanf(x);};};
template<class T>void scanf(vector<vector<T>>&value){for(auto&x:value){scanf(x);};};
template<class T,class...U>void scanf(T&first,U&...second){scanf(first);scanf(second...);};
template<class T,class...U>void scanf(vector<T>&first,U&...second){scanf(first);scanf(second...);};
template<class T,class...U>void scanf(vector<vector<T>>&first,U&...second){scanf(first);scanf(second...);};

chmax,chmin関数

前半に入力した部分の値を変える
ans = max (ans, sum);chmax (ans, sum);にしてくれる(結構便利)

// changeのmax,min #include <algorithm>が必須
template<class T>void chmax(T&first,const T&second){first=max(first,second);};
template<class T>void chmin(T&first,const T&second){first=min(first,second);};

競プロの入力形式

A問題とかでよく見るYes Noを簡単に入力してくれる
print("Yes")print(true)にしてくれる
したの部分は昔のYES NO判定や偶奇のEven Oddに対応した奴(使ったことない)

// 競プロ出力
void print(const bool&bo){bo?print("Yes"):print("No");};
void print(const bool&bo,const int&mode){if(mode==0){print(bo);}else if(mode==1){bo?print("YES"):print("NO");}else if(mode==2){bo?print("Even"):print("Odd");};};

見たい人用

コードを整形した奴.cpp
#include <bits/stdc++.h>
using namespace std;

// vector省略
#define v(type) vector<type>
// 二次vector省略
#define vv(type) vector<vector<type>>

// print関数
void print() {
    cout << '\n';
};

template <class T>
void print (const T &value) {
    cout << value;
    print();
};

template <class T>
void print (const vector<T> &value) {
    for (const auto &x : value) {
        cout << x;
        if (&x != &value.back()) {
            cout << ' ';
        };
    };
    print();
};

template <class T>
void print (const vector<vector<T>> &value) {
    for (const auto &x : value) {
        print(x);
    };
};

template <class T, class ...U>
void print (const T &first, const U &...second) {
    cout << first;
    print(second...);
};

template <class T, class ...U>
void print (const vector<T> &first, const U &...second) {
    print(first);
    print(second...);
};

template <class T, class ...U>
void print (const vector<vector<T>> &first, const U &...second) {
    print(first);
    print(second...);
};

template <class T, class ...U>
void print (const vector<vector<T>> &first, const U &...second) {
    
};

// scanf関数
template <class T>
void scanf (T &value) {
    cin >> value;
};

template <class T>
void scanf (vector<T> &value) {
    for (auto &x : value) {
        scanf(x);
    };
};

template <class T>
void scanf (vector<vector<T>> &value) {
    for (auto &x : value) {
        scanf(x);
    };
};

template <class T, class ...U>
void scanf (T &first, U &...second) {
    scanf(first);
    scanf(second...);
};

template <class T, class ...U>
void scanf (vector<T> &first, U &...second) {
    scanf(first);
    scanf(second...);
};

template <class T, class ...U>
void scanf (vector<vector<T>> &first, U &...second) {
    scanf(first);
    scanf(second...);
};

// changeのmax,min #include <algorithm>が必須
template <class T>
void chmax (T &first, const T &second) {
    first = max(first, second);
};

template <class T>
void chmin (T &first, const T &second) {
    first = min(first, second);
};

// max,minの配列対応
template <class T>
T armax(const vector<T> &value, const T &stand) {
    T M;
    for (int i = 0; i < (int)value.size(); i++) {
        chmax(M, value[i]);
    };
    return M;
};

template <class T>
T armin (const vector<T> &value, const T &stand) {
    T M;
    for (int i = 0; i < (int)value.size(); i++) {
        chmin(M, value[i]);
    };
    return M;
};

// 競プロ出力
void print (const bool &bo) {
    bo ? print("Yes"); : print("No");
};

void print(const bool &bo, const int &mode) {
    if (mode == 0) {
        print(bo);
    } else if (mode == 1) {
        bo ? print("YES"); : print("NO");
    } else if (mode == 2) {
        bo ? print("Even"); : print("Odd");
    };
};

留意点

・これらのコードは勝手に使ってもよい
・print関数の一次元配列や二次元配列を出力する場合値どうしに空白がある
1 2 3 4 5 6 7 8 9 10
なのでB問題のこの問題なんかで安直にprint関数を使ってしまうと文字との間に空白が開いてしまうので注意(新しく作ってね)

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?