|
From: Jason H. <jas...@bt...> - 2001-10-05 01:32:20
|
Thanks for pointing out the casting. The only reason I did it was because I
had had to look up the functions and had noticed the type differences, and I
figured it's best to cast rather than use implicit casting. As for using C
style casts instead of C++, just call me a lazy typer. Using
static_cast<unsigned> is twice as much to type. :)
As for sizeof(), it was just the first thing that came to my head at the
time, and as you said it was a const string . As for using the a C string as
opposed to a C++ string container, that was deliberate. For the task I was
performing I decided that a C string was more efficient than the
alternative, and converting from a string container isn't hard. I tend to
write my C++ programs as more of a hybrid C/C++ style, as I did learn C
first then expanded from it to C++.
The reason that I had written the sort algorithm was because someone had
asked how to do it. As is my habit I went overkill on it. I really like
taking on problems like that, although I probably should have let the person
figure it out for himself. Looking at my sortv2 algorithm I think I may have
given him a headache. :-D While I'm no expert on the standard algorithm's
sorting fuctions, I think they mainly cover sorting into literal order, and
not sorting same letters in to the order they appear in sequence, grouping
them together (skipping spaces), i.e. MISSISSIPPI MUD to MMIIIISSSSPPUD.
Jason.
> srand((unsigned) time(NULL)); // give the rand() function a seed
> // I use the time because it's not likely to be the same each
> time
> // the program is run. There may be a better way of doing
> this?
There is no need for casting since it returns always positive values,
and -1 only in case of error. The conversion is a regular built in
conversion from an integer type to an unsigned integer type. Also
casting a negative number to an unsigned type has the same truncation
effects as the direct assignment without casting. Also in case of
casting always try to use the C++ style casts and not the C style cast.
> // Getting a character out of a string
> char a_string[] = "This is a string of text. Hello World!";
> cout << a_string << endl << "Enter a number between 1 and "
> << sizeof(a_string) - 1 << ": ";
> // the string is null terminated, so sizeof reports one
> bigger
> // than text
sizeof() always works for arrays declared locally or globally (and not
passed as a pointer argument to a function). So be careful with it, and
in case of char pointer arguments to function us strlen(p)+1 style; Also
always try to use C++ string type than C-style string type.
> cin >> num;
> cout << "The character at that number is: " << a_string[num-1] <<
> endl;
> // You don't need a function to get a single character out of
> a string
> // just use array access. This applies to std::string too.
> // don't forget... arrays are indexed from 0
>
> cout << endl;
>
> // Sorting a string in sequence
C++ provides enough sorting algorithms, try using one of them (probably
C++ sort() ).
> char str[80], str2[80]; // up to 80 chars
> cout << "Enter a string to be sorted in sequence (80 char MAX!)"
> << endl << ": ";
> gets(str); // get a string from user (part of <cstdio>)
> strcpy(str2, str); // make a copy
>
> // Begin sort (brute force)
> int i = 0, j = 0, k = 0; // index's
> char sorted[80], c;
> while(str[i])
> {
> while(str[i] == ' ') ++i; // skip spaces
> if(!str[i]) break; // avoid end of line
> j = i;
> c = str[i];
> while(str[j])
> {
> if(str[j] == c)
> {
> sorted[k] = c;
> str[j] = ' '; // don't get the same char twice
> ++k; // increment sorted index
> }
> ++j; // increment search index
> }
> ++i; // increment input index
> }
> sorted[k] = 0; // don't forget to null terminate string!
> cout << "Sorted string is: " << sorted << endl;
> // NB: there may be a better way to do it, but this works and
> you
> // don't really need anything faster for just 80 chars.
> // I leave it to you to perform size and type checking
> anywhere
>
> sortv2(str2); // I made a faster one anyway :)
> cout << "Sorted by V2 is : " << str2 << endl;
>
> system("PAUSE");
> return 0;
> }
>
>
> // another sort that should be faster (in theory)
> // for one it uses pointer arithmatic mainly which is faster // than
> array access, secondly it employs two single passes // of the string,
> rather than the multiple in the other version // one thing you could
> note is that if you give this function // an already sorted string it
> will take longer than the v1 sort // method. void sortv2(char str[])
> // pass it the string to be sorted {
> char ts[80], *pstr, *pts;
> int count[128], *pc;
> pc = count;
> for(int i = 0; i < 128; ++i) *pc++ = 0; // clear all of
> count to zero
> pstr = str;
> while(*pstr) ++count[*pstr++]; // count instances of each
> character
> pstr = str;
> pts = ts;
> while(*pstr)
> {
> while(*pstr == ' ') ++pstr; // ignore spaces
> if(!*pstr) break; // stop at EOL
> for(; count[*pstr]; --count[*pstr])
> *pts++ = *pstr;
> ++pstr;
> }
> *pts = 0; // NULL terminate
> strcpy(str, ts); // copy it over original string
>
> }
>
>
> _______________________________________________
> Dev-cpp-users mailing list Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|