![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
This example was created using Microsoft Visual C++ version 5. If you use the Borland C++ 5 compiler, you have to change the line return count (PName.begin(), PName.end(), TestName); in the persname::CountNames function to int iCount; count (PName.begin(), PName.end(), TestName, iCount); return iCount; Lets discuss the implementation of the member functions. The void Add(string NewName) function takes a string argument and pushes it to the end of the PName list. This operation helps to create the lists rapidly without ordering the names. To implement it, use the push_back member function of the list container. The member function is common to all sequential containers and inserts a new element of an appropriate type to the end of the container. void AddBefore(string NewName, string BeforeName) inserts the NewName element before the first occurrence of the BeforeName element. If the BeforeName element does not exist, the function inserts the NewName element at the back of the list. The two Add functions are void in this implementation. However, in a more sophisticated class the functions could return a value. For instance, true would be returned if the operation were successful and false if the operation failed. void Sort(void) sorts all elements in the list. This brings up the first question: Should you use the sort member function of the list container to sort the names, or should you make the implementation with the sort algorithm of the Standard Template Library? No answer to this question is correct under all circumstances. In this case, the choice to use the sort member function of the list container is determined by the limitations of the container. The container uses input/output, forward, and bidirectional iterators. However, the sort algorithm accepts only random-access iterators as its arguments. Using the sort algorithm is appropriate when you want to make your solution more generic. For example, if you are not sure you selected the correct container and want to be able to change the container in the future, the sort algorithm will better solve this problem. You can apply the sort algorithm to different containers and changing the container would not change the persname::Sort member function. On the other hand, using the sort member function of the list class is more efficient. Even if you developed a sort algorithm that works with bidirectional iterators, it is a good idea to keep using the sort member functions of the list container. This Sort function sorts the data in ascending alphabetical order. The RemoveDuplicates function removes the consecutive duplicates from the list of names. The function uses the unique algorithm to remove the duplicates. The algorithm is used instead of the unique member function so you will be able to extend the class functionality quickly. The arguments of the unique algorithm specify the iterators to the first and last elements in the range that should be checked for duplicates. The algorithm returns an iterator that points to the end of the new list range. Note that the unique algorithm does not truncate the unused elements after it removes the duplicates. Therefore, you have to apply the erase function that erases all elements of the list from the new end to the old end. Pointing to the list elements is provided by the iterators. The iterator that points to the new end of the list is declared with the statement list <string>::iterator new_end; The iterators that point to the beginning and end of the list are the results of the list member functions begin() and end(). The functions exist for all sequential and associative containers. The NameExists function uses the find algorithm to find the first occurrence of the specified name in the data range: SResult = find(PName.begin(), PName.end(), TestName); This algorithm accepts the range limits as the first two arguments. This class specifies the entire container as the range. The third argument is the value of the element to be found. The find algorithm compares the elements in the range with the specified value using the == operator of the data in the list. The return value is an iterator that points to the first occurrence of the specified element in the container. If the element with the specified value is not found, the resulting iterator points to the end of the range. The CountNames function is the first iteration in creating a function that counts elements in a specified sub-sequence. The CountNames function returns the number of occurrences of a specified name in the list. The function uses the count algorithm on the entire sequence from PName.begin() to PName.end(). In order to test this class, create the DisplayAll function that displays all elements in the list. The function runs a loop from the first to the last element in the list. Note that the end() member function points after the end of the list (or another container). To use the algorithms, you must include the <algorithm> header file. For some compilers, the file can have <algorithm.h>, <algo.h>, or another name.
|
![]() |
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.
|