Jekuper's blog

By Jekuper, 3 years ago, In English

It is my own DEBUG function(class) it works with GNU G++ 17 and higher. Here is code:

class LOG{
public:
    //check if type iterable function
    template<typename C>
    struct is_iterable{
      typedef long false_type;
      typedef char true_type;

      template<class T> static false_type check(...);
      template<class T> static true_type  check(int,
                        typename T::const_iterator = C().end());

      enum { value = sizeof(check<C>(0)) == sizeof(true_type) };
    };
    //string print
    static void print_value(string starting, string value, string ending = "\n"){
        cout << starting<< "\"" << value<< "\"" << ending;
    }
    //main print function
    template<class T>
    static void print_value(string starting, T value, string ending = "\n"){
        //if iterable than recursevly print ech value
        if constexpr (is_iterable<T>::value){
            typename T :: iterator it = value.begin();
            cout << starting << "{";
            while (it != value.end()){
               print_value ("", *it, (next(it, 1) == value.end() ? "" : ", "));
                it++;
            }
            cout << "}";
        }
        else{
            // else we probably have char, int, long long, and etc.
            cout << starting << value;
        }
        cout << ending;
    }
    //pair print
    template<class T, class M>
    static void print_value(string starting, pair<T, M> value, string ending = "\n"){
        cout << starting << "{";
        print_value("", value.ff, "");
        cout << ", ";
        print_value("", value.ss, "");
        cout << "}" << ending;
    }
    //map recursive print
    template<class T, class M>
    static void print_value(string starting, map<T, M> value, string ending = "\n"){
        cout << starting << "{" << endl;
        ll cur = 0;
        for (pair<T, M> v: value){
            print_value ("  {", v.ff, ": ");
            print_value ("", v.ss, (cur == value.size() - 1 ? "}" : "},"));
            cur++;
            cout << endl;
        }
        cout << "}" << ending;
    }
    //array print
    /*
    ARRAY PRINT DOES'T WORK CORRECTLY
      I HIGHLY RECOMMEND TO NOT USE IT
    */
    template<class T>
    static void print_value(string starting, vector<size_t> len, T* value, int dim){
        if (len.size() == 1){
            for (long long i = 0; i < len[0]; i++){
                cout << *(value + i) << " ";
            }
        }
        if (len.size() == 2){
            for (long long i = 0; i < len[0]; i++){
                for (long long j = 0; j < len[1]; j++){
                    cout << *(*(value + i) + j) << " ";
                }
                cout << endl;
            }
        }
        if (len.size() == 3){
            for (long long i = 0; i < len[0]; i++){
                for (long long j = 0; j < len[1]; j++){
                    for (long long z = 0; z < len[2]; z++){
                        cout << *(*(*(value + i)+ j) + z) << " ";
                    }
                    cout << endl;
                }
                cout << endl;
            }
        }
        if (len.size() == 4){
            for (long long i = 0; i < len[0]; i++){
                for (long long j = 0; j < len[1]; j++){
                    for (long long z = 0; z < len[2]; z++){
                        for (long long x = 0; x < len[3]; x++){
                            cout << (*(*(*(value + i) + j) + z) + x) << " ";
                        }
                        cout << endl;
                    }
                    cout << endl;
                }
                cout << endl;
            }
        }
    }
};

now the examples:

#include<bits/stdc++.h>
using namespace std;
//----LOG class code---
int main(){
    vector<long long> v(5);
    for (long long i = 0; i < 5; i++){
        cin >> v[i];
    }
    LOG::print_value ("starting string ", v, " ending string");
}

this is for all types exept arrays(like this: a[5]) now for arrays:

#include<bits/stdc++.h>
using namespace std;
//----LOG class code---
int main(){
    long long v[5][3];
    for (long long i = 0; i < 5; i++){
        for (long long j = 0; i < 3; j++){  
            cin >> v[i][j];
        }   
    }
    LOG::print_value ("starting string ", {5, 3}, a, 0);
// starting string, vector of lengths, variable name, starting dimension index
}

thath's it. Also check my friend account — ShirChoi. He helped me a lot with this. Wish you luck.

  • Vote: I like it
  • +20
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it -9 Vote: I do not like it

I have a very naive question Why do we need debug functions?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    it's faster and easier to write one function than write for loop every time you want to debug something

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      What do you mean by debugging ,for me debugging is correct compilation / runtime / logical error , for which you don't need functions , I guess , Pardon me if my doubt is too naive.

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it +11 Vote: I do not like it

        To print out the values in variables, to see if they don't match your idea.

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Kudos for the efforts .