![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
How It Works Lets look at MAIN.CPP. This program sorts an array of char *. The array contains four elementsthe names of C and C++ creators. No changes are added to the flow of the program. The only changes are those deriving from the different type of the array elements and the size of the array. Lets look at COMPARE.CPP to see the changes. The first source lines includes the header file <string.h>, which contains the declaration of strcmp. #include <string.h> The prototype of compare, as expected, is the same. However, the function body has changed. strcmp is used to compare the arguments, which you know are C strings. Luckily, strcmp does not return a Boolean value indicating whether the strings are identical; it would be useless for our purpose. Rather, it performs a lexicographical comparison. A lexicographical comparison compares two strings in a method similar to the way two words are compared to decide which comes first in a dictionary. A lexicographic comparison treats every character of the compared strings as an integer. The value of the character of the second string is subtracted from the corresponding value of the first string. The result can be 0, which means that the two strings are identical. A positive value indicates the first string ranks lexicographically higher than the second one. A negative value indicates the first string ranks lexicographically lower than the second one. This is exactly what qsort expects to get from its callback function, so compare has only to invoke strcmp and return the result. In order to call strcmp, you have to cast the void pointers to their real types. int __cdecl compare (const void * pfirst, const void * psecond) { return strcmp(*(const char**) pfirst, *(const char **) psecond); } Comments qsort, as you have seen, is a powerful algorithm. It is efficient, generic, and standardized. However, you have to bear in mind that it does not support C++ objects. Unlike STL containers, qsort will not reconstruct a relocated object, nor will it invoke the destructor of that object in its previous location. In other words, qsort and STLs sort are not interchangeable, but are complementary. You should use qsort with arrays (and arrays only) of plain data types such as char, int, pointers of all kind, and structs. 8.2 Find an element in an array?Problem I know how to sort an array but I need to find an element in it. How do I do it in C? Technique Standard C has a generic function for searching an element in an arraybsearch.bsearch performs a binary search on a sorted array of elements. If your array is not sorted, you should sort it before using bsearch. Later you will see how it is possible to locate an element in a nonsorted array. bsearch tries to locate an element in an array. If the element exists, bsearch returns a pointer to it. Otherwise, it returns null.bsearch and qsort are similar both in the way they are used and in the principles of their implementation. Therefore, it is advisable for you to read this How-To after reading How-To 8.1. Remember also that this How-To uses two source files from the Comments section of How-To 8.1COMPARE.H and COMPARE.CPP. The text states whether a source file is identical to any of the source files used in previous examples. Steps
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|