Help on some functions

Started by
3 comments, last by Zakwayda 17 years, 4 months ago
what functions in standard c++ go for: 1.)int strcmp(char*,char*) 2.)void clrscr(); //clear screen 3.)void Sleep(int milliseconds); //from dev c++, for delaying for some time
Advertisement
Quote:Original post by Felon_pro
what functions in standard c++ go for:
1.)int strcmp(char*,char*)
2.)void clrscr(); //clear screen
3.)void Sleep(int milliseconds); //from dev c++, for delaying for some time
Not sure about the last two, but C++ doesn't have a 'strcmp' function per se. Instead, it has the std::string class, for which both the == and < operators are overloaded. With these operators you can perform string comparisons as you would with strcmp().
Quote:
what functions in standard c++ go for:
2.)void clrscr(); //clear screen

I believe this is a system call, provided by dos. Its not part of the native C/C++ language.
It resides in the conio.h header.
Quote:
3.)void Sleep(int milliseconds); //from dev c++, for delaying for some time

This is also a system call, provided by Windows API. Not part of the native language either.
It resides in the windows.h header.
You will typically need to create a win32 project to include and use this header.
Quote:Original post by jyk
Quote:Original post by Felon_pro
what functions in standard c++ go for:
1.)int strcmp(char*,char*)
2.)void clrscr(); //clear screen
3.)void Sleep(int milliseconds); //from dev c++, for delaying for some time
Not sure about the last two, but C++ doesn't have a 'strcmp' function per se. Instead, it has the std::string class, for which both the == and < operators are overloaded. With these operators you can perform string comparisons as you would with strcmp().


It depends what you're using it for. If your char*s are "strings" (and I use the term very loosely), then you should be using real (std::)strings instead; and in that case you can use those operators.

But there certainly is also a function in the 'modern' C++ library that provides the same *algorithm* as strcmp() - i.e., checking elements of a sequence (although for strcmp(), "sequence" is restricted to "chars that are sequential in memory") one at a time, stopping when either of them runs out (which strcmp() defines as "finding a char with a zero value") or there is a mismatch, and then returning either a negative, zero or positive value according to which sequence is "first in alphabetical order". (I only wrote that all out because I would bet that most people who use strcmp() don't fully understand it, or even realize that the return value encodes three possibilities rather than just two - equal or not-equal. Heck, a lot of them probably think '!' is part of the function name :S)

It is called std::lexicographical_compare, and it lives in <algorithm>. It's generic, meaning it works on iterators (pointers are a kind of iterator, but not the only kind) over any type (not just char) - but it does have to be a specific, static type, that is common to all the elements of both sequences. (Actually, that's an oversimplification; you can have elements of type A in one container, and of type B in the other, and compare the contents with std::lexicographical_compare, IF there is a defined way to compare As to Bs.) So for example, with the appropriate headers included, you could do a lexicographical_compare of the contents of two std::list<CHAR_INFO> objects (using .begin() and .end() to get the necessary iterators).

OK, so it has a sort of long name (you can alias that with a typedef, or wrap it in your own function, though), but there's only one real reason you wouldn't use it instead of strcmp(), in modern C++: which is because you don't need it any more due to std::string usage ;) To be serious, though, it's more flexible, and will still compile down to the same code as strcmp() in the few cases where it's possible.

If you tried to compare those lists of CHAR_INFO manually, you'd probably either waste time figuring out how to do it manually with a for-loop and getting code that would be *at best* as fast as the result using std::lexicographical_compare (because you would, in effect, implement it), or maybe extracting the info into char*'s somehow in order to strcmp() the results - which would be much worse all around, but sometimes people think this way :s
Quote:Original post by Zahlman
Quote:Original post by jyk
Quote:Original post by Felon_pro
what functions in standard c++ go for:
1.)int strcmp(char*,char*)
2.)void clrscr(); //clear screen
3.)void Sleep(int milliseconds); //from dev c++, for delaying for some time
Not sure about the last two, but C++ doesn't have a 'strcmp' function per se. Instead, it has the std::string class, for which both the == and < operators are overloaded. With these operators you can perform string comparisons as you would with strcmp().


It depends what you're using it for. If your char*s are "strings" (and I use the term very loosely), then you should be using real (std::)strings instead; and in that case you can use those operators.

But there certainly is also a function in the 'modern' C++ library that provides the same *algorithm* as strcmp() - i.e., checking elements of a sequence (although for strcmp(), "sequence" is restricted to "chars that are sequential in memory") one at a time, stopping when either of them runs out (which strcmp() defines as "finding a char with a zero value") or there is a mismatch, and then returning either a negative, zero or positive value according to which sequence is "first in alphabetical order". (I only wrote that all out because I would bet that most people who use strcmp() don't fully understand it, or even realize that the return value encodes three possibilities rather than just two - equal or not-equal. Heck, a lot of them probably think '!' is part of the function name :S)

It is called std::lexicographical_compare, and it lives in <algorithm>. It's generic, meaning it works on iterators (pointers are a kind of iterator, but not the only kind) over any type (not just char) - but it does have to be a specific, static type, that is common to all the elements of both sequences. (Actually, that's an oversimplification; you can have elements of type A in one container, and of type B in the other, and compare the contents with std::lexicographical_compare, IF there is a defined way to compare As to Bs.) So for example, with the appropriate headers included, you could do a lexicographical_compare of the contents of two std::list<CHAR_INFO> objects (using .begin() and .end() to get the necessary iterators).

OK, so it has a sort of long name (you can alias that with a typedef, or wrap it in your own function, though), but there's only one real reason you wouldn't use it instead of strcmp(), in modern C++: which is because you don't need it any more due to std::string usage ;) To be serious, though, it's more flexible, and will still compile down to the same code as strcmp() in the few cases where it's possible.

If you tried to compare those lists of CHAR_INFO manually, you'd probably either waste time figuring out how to do it manually with a for-loop and getting code that would be *at best* as fast as the result using std::lexicographical_compare (because you would, in effect, implement it), or maybe extracting the info into char*'s somehow in order to strcmp() the results - which would be much worse all around, but sometimes people think this way :s
It was actually my intention to mention lexicographical_compare() in my post, but apparently I got distracted and forgot to do so. Color me chagrined :-|

This topic is closed to new replies.

Advertisement