Thread: Re: [Dev-C++] Help with functions
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2001-10-03 13:12:05
|
Hi, I notice you have done three posts so I'll answer them all here as best
I can. I have written a small console app to demonstrate them all too. I
don't know why as I guess that you should be learning this for yourself, but
it was fun for me.
I tried to post this earlier, but it never got through, so this is my second
try.
First of all for a random number you can use rand(). Some compilers provide
a RANDOM() macro, but I don't know about MinGW. It is best to give rand() a
seed using srand(), otherwise you are likely to get the same numbers every
time you run the program. I would use some value you know won't be the same
each time you run a program i.e. the time. To use rand() you need to include
<stdlib.h>; or <cstdlib> if you want to be c++ standard compliant.
To get a single character from a string, either a C string or a C++
std::string, then just use the [] operator with the relevant index. i.e.
char my_string[] = "HELLO";
char c = my_string[4]; // c is now "L"
if you want you can write your own function to do it in the way you want.
I'm not going to do that for you.
As for the sorting of a string in the way you want, I've written an
algorithm that works by going through your string and picking out each of
the letter as it comes to them in order, searching for the same ones and
skipping spaces. Have a look at my code below. There may be a better
algorithm around to do this, but why spend loads of time doing that unless
you are working hundreds of thousands of strings.
Heres my demo, it has been briefly tested and I can't see any great problems
with it:
#include <cstdio> // all around useful header
#include <cstdlib> // the rand() fuction is in this header, so don't forget
it
#include <ctime> // for time()
#include <iostream> // for cout and cin
int main(int argc, char *argv[])
{
using namespace std; // allow implicit use of std namespace
// Random number generation
int num;
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?
for(int i = 0; i < 10; ++i)
{
num = (rand() % 99) + 1; // A random number between 1 and 100.
cout << "A random number " << i + 1 << ": " << num << endl;
}
cout << endl;
// 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
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
char str[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>)
// 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
system("PAUSE");
return 0;
}
----- Original Message -----
From: "Mansoor Nathani" <man...@sy...>
To: <dev...@li...>
Sent: Wednesday, October 03, 2001 12:12 AM
Subject: [Dev-C++] Help with functions
> Does any one know the functions for :
>
> Generating a random number (integer) between a given range.
>
> Extracting a character from a string: eg:
>
> ( "HELLO", 4 )
>
> that should output the fourth character : L
>
> Any help would be appreciated
>
> Mansoor
>
>
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Jason H. <jas...@bt...> - 2001-10-04 10:35:16
|
I went ahead and created a faster algorithm anyway, it's the sortv2()
function a the bottom.
Note that the e-mail line length is cropped, so some of the comments will
need to be straightened out before it will compile again.
Jason.
#include <cstdio> // all around useful header
#include <cstdlib> // the rand() fuction is in this header
#include <ctime> // for time()
#include <iostream> // for cout and cin
#include <cstring> // for strcpy()
void sortv2(char str[]);
int main(int argc, char *argv[])
{
using namespace std; // allow implicit use of std namespace
// Random number generation
int num;
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?
for(int i = 0; i < 10; ++i)
{
num = (rand() % 99) + 1; // A random number between 1 and 100.
cout << "A random number " << i + 1 << ": " << num << endl;
}
cout << endl;
// 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
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
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
}
|
|
From: Ioannis V. <no...@ya...> - 2001-10-04 17:13:25
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Jason
> Hardman
> Sent: Wednesday, October 03, 2001 6:04 PM
> To: Dev-C++
> Subject: Re: [Dev-C++] Help with functions
>=20
>=20
> I went ahead and created a faster algorithm anyway, it's the
> sortv2() function a the bottom. Note that the e-mail line length is
> cropped, so some of the comments will need to be straightened out=20
> before it will compile again.
>=20
> Jason.
>=20
>=20
> #include <cstdio> // all around useful header
> #include <cstdlib> // the rand() fuction is in this header
> #include <ctime> // for time()
> #include <iostream> // for cout and cin
> #include <cstring> // for strcpy()
>=20
> void sortv2(char str[]);
>=20
> int main(int argc, char *argv[])
> {
> using namespace std; // allow implicit use of std namespace
>=20
> // Random number generation
> int num;
> srand((unsigned) time(NULL)); // give the rand() function a seed
> // I use the time because it's not likely to be the same each=20
> time
> // the program is run. There may be a better way of doing=20
> 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.
>=20
> for(int i =3D 0; i < 10; ++i)
> {
> num =3D (rand() % 99) + 1; // A random number between 1 and =
100.
> cout << "A random number " << i + 1 << ": " << num << endl;
> }
>=20
> cout << endl;
>=20
> // Getting a character out of a string
> char a_string[] =3D "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=20
> 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] <<=20
> endl;
> // You don't need a function to get a single character out of=20
> a string
> // just use array access. This applies to std::string too.
> // don't forget... arrays are indexed from 0
>=20
> cout << endl;
>=20
> // Sorting a string in sequence
C++ provides enough sorting algorithms, try using one of them (probably=20
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
>=20
> // Begin sort (brute force)
> int i =3D 0, j =3D 0, k =3D 0; // index's
> char sorted[80], c;
> while(str[i])
> {
> while(str[i] =3D=3D ' ') ++i; // skip spaces
> if(!str[i]) break; // avoid end of line
> j =3D i;
> c =3D str[i];
> while(str[j])
> {
> if(str[j] =3D=3D c)
> {
> sorted[k] =3D c;
> str[j] =3D ' '; // don't get the same char twice
> ++k; // increment sorted index
> }
> ++j; // increment search index
> }
> ++i; // increment input index
> }
> sorted[k] =3D 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=20
> you
> // don't really need anything faster for just 80 chars.
> // I leave it to you to perform size and type checking=20
> anywhere
>=20
> sortv2(str2); // I made a faster one anyway :)
> cout << "Sorted by V2 is : " << str2 << endl;
>=20
> system("PAUSE");
> return 0;
> }
>=20
>=20
> // 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,=20
> rather than the multiple in the other version // one thing you could=20
> note is that if you give this function // an already sorted string it=20
> will take longer than the v1 sort // method. void sortv2(char str[])=20
> // pass it the string to be sorted {
> char ts[80], *pstr, *pts;
> int count[128], *pc;
> pc =3D count;
> for(int i =3D 0; i < 128; ++i) *pc++ =3D 0; // clear all of
> count to zero
> pstr =3D str;
> while(*pstr) ++count[*pstr++]; // count instances of each=20
> character
> pstr =3D str;
> pts =3D ts;
> while(*pstr)
> {
> while(*pstr =3D=3D ' ') ++pstr; // ignore spaces
> if(!*pstr) break; // stop at EOL
> for(; count[*pstr]; --count[*pstr])
> *pts++ =3D *pstr;
> ++pstr;
> }
> *pts =3D 0; // NULL terminate
> strcpy(str, ts); // copy it over original string
>=20
> }
>=20
>=20
> _______________________________________________
> Dev-cpp-users mailing list Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ya...> - 2001-10-04 17:45:49
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Jason
> Hardman
> Sent: Wednesday, October 03, 2001 6:04 PM
> To: Dev-C++
> Subject: Re: [Dev-C++] Help with functions
>=20
>=20
> I went ahead and created a faster algorithm anyway, it's the
> sortv2() function a the bottom. Note that the e-mail line length is
> cropped, so some of the comments will need to be straightened out=20
> before it will compile again.
>=20
> Jason.
>=20
>=20
> #include <cstdio> // all around useful header
> #include <cstdlib> // the rand() fuction is in this header
> #include <ctime> // for time()
> #include <iostream> // for cout and cin
> #include <cstring> // for strcpy()
>=20
> void sortv2(char str[]);
>=20
> int main(int argc, char *argv[])
> {
> using namespace std; // allow implicit use of std namespace
>=20
> // Random number generation
> int num;
> srand((unsigned) time(NULL)); // give the rand() function a seed
> // I use the time because it's not likely to be the same each=20
> time
> // the program is run. There may be a better way of doing=20
> 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.
>=20
> for(int i =3D 0; i < 10; ++i)
> {
> num =3D (rand() % 99) + 1; // A random number between 1 and =
100.
> cout << "A random number " << i + 1 << ": " << num << endl;
> }
>=20
> cout << endl;
>=20
> // Getting a character out of a string
> char a_string[] =3D "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=20
> 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] <<=20
> endl;
> // You don't need a function to get a single character out of=20
> a string
> // just use array access. This applies to std::string too.
> // don't forget... arrays are indexed from 0
>=20
> cout << endl;
>=20
> // Sorting a string in sequence
C++ provides enough sorting algorithms, try using one of them (probably=20
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
>=20
> // Begin sort (brute force)
> int i =3D 0, j =3D 0, k =3D 0; // index's
> char sorted[80], c;
> while(str[i])
> {
> while(str[i] =3D=3D ' ') ++i; // skip spaces
> if(!str[i]) break; // avoid end of line
> j =3D i;
> c =3D str[i];
> while(str[j])
> {
> if(str[j] =3D=3D c)
> {
> sorted[k] =3D c;
> str[j] =3D ' '; // don't get the same char twice
> ++k; // increment sorted index
> }
> ++j; // increment search index
> }
> ++i; // increment input index
> }
> sorted[k] =3D 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=20
> you
> // don't really need anything faster for just 80 chars.
> // I leave it to you to perform size and type checking=20
> anywhere
>=20
> sortv2(str2); // I made a faster one anyway :)
> cout << "Sorted by V2 is : " << str2 << endl;
>=20
> system("PAUSE");
> return 0;
> }
>=20
>=20
> // 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,=20
> rather than the multiple in the other version // one thing you could=20
> note is that if you give this function // an already sorted string it=20
> will take longer than the v1 sort // method. void sortv2(char str[])=20
> // pass it the string to be sorted {
> char ts[80], *pstr, *pts;
> int count[128], *pc;
> pc =3D count;
> for(int i =3D 0; i < 128; ++i) *pc++ =3D 0; // clear all of
> count to zero
> pstr =3D str;
> while(*pstr) ++count[*pstr++]; // count instances of each=20
> character
> pstr =3D str;
> pts =3D ts;
> while(*pstr)
> {
> while(*pstr =3D=3D ' ') ++pstr; // ignore spaces
> if(!*pstr) break; // stop at EOL
> for(; count[*pstr]; --count[*pstr])
> *pts++ =3D *pstr;
> ++pstr;
> }
> *pts =3D 0; // NULL terminate
> strcpy(str, ts); // copy it over original string
>=20
> }
>=20
>=20
> _______________________________________________
> Dev-cpp-users mailing list Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|
|
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
|
|
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 |
|
From: Ioannis V. <no...@ya...> - 2001-10-05 09:42:01
|
I forgot to mention that i have started myself with C too and that except from the previous benefits of the templates (inline expansion=3D0 runtime overhead, instances only of the member functions which are used=3D0 space overhead) the fact that not using the string type and its member functions and using C strings and passing them as arguments among functions "twiddling their bits" has almost always as a result the programmer to write a lot more code than that getting compiled when you use the string type. Also the cost of calling a stand alone function is usually bigger than the cost of calling a member function. Add to this that the templates are usually expanded inline and the fact that a good compiler always creates code only for those template functions used, and you will understand why C++ programs performed better than their equivallent C programs in http://www.research.att.com/~bs/esc99.html Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys > -----Original Message----- > From: dev...@li... > [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 |