ticc_prettyprint - format C++ containers

NAME  SYNOPSIS  DESCRIPTION  EXAMPLES  AUTHORS 

NAME

ticc_prettyprint - format C++ containers

SYNOPSIS

#include ticcutils/PrettyPrint.h

using namespace TiCC;

DESCRIPTION

TiCC Pretty Print provides some C++ templates to output C++ containers in an orderly fashion. It’s not rocket science, nor complete. But for instance for debugging it is convenient to be able to output containers a bit readable.

To get usefull output, a well defined output operator is required for the elements in the container too.

TiCC output operators are currenly defined for:

set<T>

list<T>

vector<T>

map<S,T>

map<S,T,U>

multimap<S,T>

To get usefull output, it is needed that an output operator is defined for the elements S and T in the container too.

EXAMPLES

Example 1

#include <vector>
#include <string>
#include <iostream>
#include "ticcutils/PrettyPrint.h"

using namespace std;
using namespace TiCC;

int main(){
vector<string> vec;
vec.push_back("one");
vec.push_back("two");
vec.push_back("three");
cout << vec << endl;
}

The output of this simple program will be:

[one,two,three]

Example 2

#include <vector>
#include <string>
#include <iostream>
#include "ticcutils/PrettyPrint.h"

using namespace std;
using namespace TiCC;

class myClass {
friend ostream& operator<< (ostream&, const myClass& );
public:
myClass( int i, const string& s ): _i(i),_s(s){}
private:
int _i;
string _s;
};

ostream& operator<< (ostream& os, const myClass& mc ){
os << "Myclass(" << mc._i << "," << mc._s << ")";
return os;
}

int main(){
vector<myClass> vec;
vec.push_back( myClass(1,"one") );
vec.push_back( myClass(2,"two") );
vec.push_back( myClass(3,"three") );
cout << vec << endl;
}

The output of this example program will be:

[Myclass(1,one),Myclass(2,two),Myclass(3,three)]

Example 3

#include <vector>
#include <string>
#include <iostream>
#include "ticcutils/PrettyPrint.h"

using namespace std;
using namespace TiCC;

class myClass {
friend ostream& operator<< (ostream&, const myClass& );
public:
myClass( int i, const string& s ): _i(i),_s(s){}
private:
int _i;
string _s;
};

ostream& operator<< (ostream& os, const myClass& mc ){
os << "Myclass(" << mc._i << "," << mc._s << ")";
return os;
}

int main(){
vector<*myClass> vec;
vec.push_back( new myClass(1,"one") );
vec.push_back( new myClass(2,"two") );
vec.push_back( new myClass(3,"three") );
cout << vec << endl;
}

The output of this program will be:

[0x2363040,0x23630b0,0x2363060] Or something similar.

Why is this so? Well, we didn’t provide an output operator for myClass pointers!

To fix this we have to change de definition of the output operator for myClass or just add a second definition, e.g. add:

friend ostream& operator<< (ostream&, const myClass* );

to myClass, and also add:

ostream& operator<< (ostream& os, const myClass *mc ){
if ( mc )
os << *mc;
else
os << "zero pointer";
return os;
}

The output now indeed is what we expected:

[Myclass(1,one),Myclass(2,two),Myclass(3,three)]

AUTHORS

Ko van der Sloot [email protected]


Updated 2024-01-29 - jenkler.se | uex.se