Code Monkey home page Code Monkey logo

atcoder's People

Contributors

708yamaguchi avatar itohdak avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

708yamaguchi

atcoder's Issues

色んな型の変数を統一的に表示できるマクロを書きたい!

概要

競技プログラミングをしている最中、

int a = 10;
check(a)
->
a: 10

のように変数の中身を表示できると便利だということで、良い感じのマクロを考えていました。 with @ykawamura96

現状

僕が色々なサイトを参考にして出来たのは以下の通りです。
良い感じに表示できる型

  • 数字
  • 文字列
  • 真偽値
  • map
  • set
  • deque
  • array

良い感じに表示できない型(現状の仕様だと、ポインタが表示されてしまう。)

  • 普通の静的配列

参考にしたサイト:
https://kurenaif.hatenablog.com/entry/2015/12/12/005841
https://qiita.com/hidachinoiro/items/b866966eef77ac84f8f2

実行結果・問題点

サンプルコードと実行結果を以下に示します。a: 10のような文字列は、bashでは赤字で書かれています。

現状の問題点は以下の2つだと考えています。

  • 上で挙げたように、int j[3]のような静的配列を表示できない。
  • コードが長く、読みづらい。

もうちょっと綺麗にかけたらプルリクエストにしようと思うんですが、どうしたらいいでしょう? @itohdak

#include <array>
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include <set>
#include <deque>

using namespace std;

#define check(val) \
  cout << "\033[31m"; \
  cout << #val << ": "; \
  Print(val);                                   \
  cout << "\033[m" << endl;

// Ignore<>::typeとした時に、必ずvoidを返して欲しい
template <class>
struct Ignore{
  typedef void type;
};

// 1変数出力用のPrintObj。
// Print関数内ではPrintObjの第二引数が常に省略されているので、配列出力用PrintObjでT::iteratorがなかった時は、必ずこちらが呼ばれるようになっている。
template <class T, class X=void >
struct PrintObj{
    void operator()(T value){
      cout << value;
    }
};

// 配列出力用PrintObj。
// 関数オブジェクトPrintObjの第二テンプレートに無理やりT::iteratorを入れることで、Tがイテレータを持つクラスかを判定する。
template<class T>
struct PrintObj<T, typename Ignore<typename T::iterator>::type>{
    void operator()(T value){
        cout << "[";
        for(typename T::iterator itr = value.begin(); itr != value.end(); itr++) {
          if (itr != value.begin())
            cout << " ";
          PrintObj<typename T::value_type>()(*itr);
        }
        cout << "]";
    }
};

template<class T>
void Print(T value){
  PrintObj<T>()(value);
}


// std::mapなど、イテレータがstd::pairのオブジェクトを指すクラスをcoutに渡すため
template<class T, class U>
ostream& operator << (ostream& os, const pair<T, U> p){
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

int main(void){

    int a = 10;
    cout << "int" << endl;
    check(a)

    vector<double> b(3, 4);
    cout << "vector" << endl;
    check(b);

    string c = "hello";
    cout << "string" << endl;
    check(c);

    map<string, int> d;
    d["hoge"] = 1;
    cout << "map" << endl;
    check(d);

    set<int> e{3, 1, 4, 1};
    cout << "set" << endl;
    check(e);

    deque<int> f(2, 5);
    cout << "deque" << endl;
    check(f);

    array<int, 5> g = {1, 2, 3, 4, 5};
    cout << "array" << endl;
    check(g);

    // array cannot be used //
    int h[3] = {0, 1, 2};
    cout << "int[]" << endl;
    check(h);


    return 0;
}

実行結果

$ g++ -std=c++1y test.cpp; ./a.out                 
int                      
a: 10                    
long long int            
b: 999999999999999999    
vector                   
c: [4 4 4]               
string                   
d: [h e l l o]           
bool                     
e: 1                     
map                      
f: [(hoge,1)]            
set                      
g: [1 3 4]               
deque                    
h: [5 5]                 
array                    
i: [1 2 3 4 5]           
int[]                    
j: 0x7ffcfa4f55a0 

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.