From: "Traveler" Newsgroups: comp.lang.c++,comp.os.msdos.djgpp Subject: More out from "cout".... Date: Mon, 8 Oct 2001 04:10:50 +0300 Organization: SAUNALAHDEN asiakas Lines: 97 Message-ID: <9pque5$eui$1@tron.sci.fi> NNTP-Posting-Host: mccciii.tdyn.saunalahti.fi Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: tron.sci.fi 1002503429 15314 195.197.82.203 (8 Oct 2001 01:10:29 GMT) X-Complaints-To: newsmaster AT saunalahti DOT fi NNTP-Posting-Date: 8 Oct 2001 01:10:29 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id f981F5l12617 Reply-To: djgpp AT delorie DOT com Hi! Have you ever be annoyed to write code like this every time you just want to list elements of some array ? "int someArray[] = {4,54,12,9,234,89,5}; unsigned long length = sizeof(table)/sizeof(table[0]); for(unsigned long index = 0;index < length;index++) cout << someArray[index] << endl;" Wouldn´t it be nice to have it like this ? "int someArray[] = {4,54,12,9,234,89,5}; cout << someArray << endl;" Well, no fear when the Traveler is here ! ;) I have just the thing you are looking for. I have overloaded the "<<" operator for few table types and I trust that you can do the trick for rest of the ones you want. Heres the code: template ostream& operator<<(ostream& output,int (&table)[n]) { unsigned long length = sizeof(table)/sizeof(table[0]); for(unsigned long index = 0;index < length;index++) output << '[' << table[index] << ']'; return output; } template ostream& operator<<(ostream& output,double (&table)[n]) { unsigned long length = sizeof(table)/sizeof(table[0]); for(unsigned long index = 0;index < length;index++) output << '[' << table[index] << ']'; return output; } // A little more trickier...table of pointer´s to char.... template ostream& operator<<(ostream& output,char* (&table)[n]) { unsigned long length = sizeof(table)/sizeof(table[0]); for(unsigned long index = 0;index < length;index++) output << '[' << table[index] << ']'; // Maybe not the best possible format for this // one...make your own in case your not happy... return output; } Testing.... int intTable[] = {45,345,22,546}; double doubleTable[] = {4.45,2.34,6.56}; char* pointerToString[] = {"Boy","Cat","Dog"}; char string[] = {"Hello"} cout << intTable << endl; // [45][345][22][546] cout << doubleTable << endl; // [4.45][2.34][6.56] cout << pointerToString << endl; // [Boy][Cat][Dog] cout << string << endl; // Hello. The default way the "cout" handless this is just fine.... See you! traveler AT netti DOT fi "No matter how many times you have failed at the end you will succeed..." PS: Does the code work in Borland compilers and others ? Anybody with access to them please tell me... PS2: The following question is for Finish computer teacher. Becourse Im a lazy person I but the question and this code in the same message. Sorry :) Heippa Kauko! Ajattelin vain kysäistä että pidätkö vielä niitä C++ kursseja ? Olisi kiva jos ilmoittaisit heti kun mahdollinen C++ jatkokurssi pidetään. Oletko muuten kirjoittanut kirjan "Algoritmit - C++" ? T: Stefan Fröberg