/* This program is written by Alexander Shirokov on Sunday November 13, and Thursday night November 17 2011 in Toronto Canada, on a personal initiative. The program is available for anyone who wishes to use it, without any restrictions or warranties whatsoever. The program is originally available for download at: http://www.cita.utoronto.ca/~shirokov/soft/table */ #include"table.h" int main() { // Specify the volumn names in the header const std::vector header = vlist_of ("A")("B")("E")("E2")("C") ; // Declare a table #ifdef MEMORYTABLE Table::MemoryTableCls T(header); #else Table::PipeTableCls T(std::cout,header,true,"","","\t"); #endif // Work with the first two rows { bool chk_nodup = true; // Declare a row Table::RowCls row(chk_nodup); // Fill it out with values { row["A"] = "Aval"; row["C"] = "CVAL"; } // Display the row in standard output std::cout << "Adding the row: " << row << " - twice!" << endl_r; // Add two copies of these rows to the table. T += row; T += row; } // Work with the third row { bool chk_nodup = true; // Declare a row Table::RowCls row(chk_nodup); // Fill it out with values { row["A"] = "Aval"; row["E"] = "Eval"; } std::cout << "Adding the row: " << row << endl_r; // Add two copies of these rows to the table. T += row; } // Work with the fourth row { bool chk_nodup = false; // Declare a row Table::RowCls row(chk_nodup); // Fill it out with values { row["B"] = "Bval"; row["A"] = "1"; row["A"] = "12"; // Note: this will overwrite // value "1" just previously defined, which is okay // since chk_nodup = false. row["C"] = "HHH"; row["HA"] = "ha-ha"; // Note: the key "HA" is not in the header } // Display the row in standard output std::cout << "Adding the row: " << row << endl_r; // Add this row to the table. T += row; } #ifdef MEMORYTABLE // Displaying the table std::cout << endl_r; std::cout << "Displaying the table with header tolerance on:" << endl_r; std::cout << T << endl_r; std::cout << endl_r; // Displaying the table, while checking the column names // against the values in header. // This should fail { const bool chk_header = true; std::cout << "Displaying the table with header tolerance off (should fail) since name HA is not in the header:" << endl_r; std::cout << T.tablestring(chk_header,"","", CHAR::STR::TAB) << endl_r; } #endif return 0; } /* With commands g++ table-main.cpp -D MEMORYTABLE && ./a.out g++ table-main.cpp && ./a.out This program should produce the following output: $ g++ table-main.cpp -D MEMORYTABLE && ./a.out Adding the row: A:Aval,C:CVAL - twice! Adding the row: A:Aval,E:Eval Adding the row: A:12,B:Bval,C:HHH,HA:ha-ha Displaying the table with header tolerance on: A B E E2 C Aval CVAL Aval CVAL Aval Eval 12 Bval HHH Displaying the table with header tolerance off (should fail) since name HA is not in the header: terminate called after throwing an instance of 'std::domain_error' what(): chk_header: invalid column name being inserted: HA Aborted ashirokov@procion:~/initdir/ashirokov/C++/table$ $ g++ table-main.cpp && ./a.out A B E E2 C Adding the row: A:Aval,C:CVAL - twice! Aval CVAL Aval CVAL Adding the row: A:Aval,E:Eval Aval Eval Adding the row: A:12,B:Bval,C:HHH,HA:ha-ha terminate called after throwing an instance of 'std::domain_error' what(): chk_header: invalid column name being inserted: HA Aborted */