|
From: Ioannis V. <no...@ya...> - 2001-10-05 04:44:17
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Jason Hardman > Sent: Friday, October 05, 2001 3:32 AM > To: Dev-C++ > Subject: Re: [Dev-C++] Help with functions >=20 >=20 > Thanks for pointing out the casting. The only reason I did it=20 > was because I had had to look up the functions and had=20 > noticed the type differences, and I figured it's best to cast=20 > rather than use implicit casting. As for using C style casts=20 > instead of C++, just call me a lazy typer. Using=20 > static_cast<unsigned> is twice as much to type. :) Yes but the C style casting is equivallent with reinterpret_cast the most unsafe casting. C++ style casts are as restrictive as possible for a particular situation and must be used instead of the C cast. It is ugly in writing because you *very rarely* have to use casts, and casts may hide potential errors. Much usage of casts in C++ is an indication of bad code. In a few words, you should almost never need casting in normal C++ applications. >=20 > As for sizeof(), it was just the first thing that came to my=20 > head at the time, and as you said it was a const string . As=20 > for using the a C string as opposed to a C++ string=20 > container, that was deliberate. For the task I was performing=20 > I decided that a C string was more efficient than the=20 > alternative, and converting from a string container isn't=20 > hard. I tend to write my C++ programs as more of a hybrid=20 > C/C++ style, as I did learn C first then expanded from it to C++. The notion that the C subset is fastest than C++ is a myth. See http://www.research.att.com/~bs/esc99.html and http://www.research.att.com/~bs/crc.pdf C++ string is a typedef of basic_string<char> type that is: typedef basic_string<char> string; basic_string is a template class and template classes tend to expanded inline when compiled. Also a good compiler creates instances only of the member functions of a template class which are used and ignores the rest. Thus since the code is expanded inline in the application you have 0 runtime overhead. >=20 > The reason that I had written the sort algorithm was because=20 > someone had asked how to do it. As is my habit I went=20 > overkill on it. I really like taking on problems like that,=20 > although I probably should have let the person figure it out=20 > for himself. Looking at my sortv2 algorithm I think I may=20 > have given him a headache. :-D While I'm no expert on the=20 > standard algorithm's sorting fuctions, I think they mainly=20 > cover sorting into literal order, and not sorting same=20 > letters in to the order they appear in sequence, grouping=20 > them together (skipping spaces), i.e. MISSISSIPPI MUD to=20 > MMIIIISSSSPPUD. I am currently studying C++ myself too and i haven;t reached at the algorithms chapters but as far as i know C++ sorting algorithms can be provided with the rules of how to sort (here the number of appearances of the letters). Also i think they are template functions too so they are expanded inline as i mentioned above. >=20 > Jason. Keep coding :) Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys |