From 4231140106029960165 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,b83a4267f4ac775 X-Google-Attributes: gidf78e5,public From: snowball3@usa.net (Mark E.) Subject: Re: string/cow/algorithm Date: 1998/08/09 Message-ID: <35ccc2b4.832003@news3.ibm.net>#1/1 X-Deja-AN: 379296003 X-NNTP-Posting-Host: 129.37.91.225 Approved: stephen.clamage@sun.com (comp.std.c++) References: <6qfr41$oue$1@pigpen.csrlink.net> <6qg3ld$r7o$1@shell7.ba.best.com> <6qgs0r$92e$1@pigpen.csrlink.net> X-UID: 0000000001 X-Notice: should be reported to postmaster@ibm.net X-Status: $$$T X-Complaints-To: postmaster@ibm.net X-Trace: 8 Aug 1998 22:39:50 GMT, 129.37.91.225 Organization: Snowball's Web Newsgroups: comp.std.c++ Originator: clamage@taumet The program segment under discussion and whether the output is conforming: >#include >#include >#include >using namespace std; >int main () { > string s("Hello world"); > string c(s); > sort(c.begin(), c.end()); // undefined behavior? > cout << s << endl; > } jpotter@falcon.lhup.edu (John Potter) wrote: >Yep, but that program made a copy of a string and sorted it. The >result was that the original was also sorted. There was no "cow" >done! I didn't find that reasonable. > >I have interpreted your answer as saying the implementation which did >that nasty thing to me is not conforming. I am sure glad since it >would mean that no generic algorithms could be used with strings. > >John > I have written my own string class using cow and my class produces the same answer. As far as I'm concerned, s shouldn't also be sorted so my class would be considered "non-conforming". In my class, I set a flag in the non-const versions of begin() and end() so when you write a character, a cow is performed. begin() and end() return pointers to the internal character array, so if you write: string s("abc"); string c(s); *(c.begin()) = 'd'; // Where do I fix this? The solutions I see to bring my class into conformance is to perform a clone() type function in the c.begin() statement or write a iterator class that performs a clone() in operator*. For reference, my class can be found at http://members.xoom.com/snowball3/cpp/ ) Mark -- Mark E.: snowball3@usa.net http://snowball3.home.ml.org/ [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]