GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
ticc_prettyprint(1) FreeBSD General Commands Manual ticc_prettyprint(1)

ticc_prettyprint - format C++ containers

#include ticcutils/PrettyPrint.h

using namespace TiCC;

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.

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)]

Ko van der Sloot Timbl@uvt.nl
2012 August 21

Search for    or go to Top of page |  Section 1 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.