Stop the duel-boot
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next



The program uses the partial_sum algorithm with the four arguments. The first two arguments specify the range that is processed for the source data container. The third parameter specifies the first element of output container. Note that you have to make some space for the output data because the partial_sum function does not create the new output elements, it uses the existing ones instead.
The output of the program is
1
0.5
0.166667
0.04166667
0.0833333

The values may slightly differ for different compilers.

Comments

The accumulate and the partial_sum algorithms belong to the group of generalized numeric algorithms in the Standard Template Library. The accumulate algorithm calculates a sum of the values of the given container elements. A version of the algorithm can apply functions other than addition to the values in the container. The partial_sum algorithm creates the consecutive sums of the elements of the given container. Using functions other than addition can also extend the functionality of this algorithm.

9.7 Sort elements in a container using different sorting indexes?

Problem

I often create containers of different user-defined types. To maintain phone books, I use a special class that describes a phone book entry. For some applications, the entries can be very complicated and contain many fields. I want to perform many operations with the phone book; I want to sort the data using different fields. Can I specify different fields as sorting keys without changing my class?

Technique

To sort a data container, you have to define a few parameters: the container itself, the subset of the container that should be sorted, and the sorting order. To define the sorting order you need to define the greater-than (or less-than) relational operator on the container elements. In other words, you should always be able to say whether an element is greater than another element.

You are going to apply the sort algorithm to the class that specifies phone book entries and enables simple manipulations with the class data. To change the sorting keys, the program will change the less-than function from the < operator to another one.

Steps

1.  Declaring the data and relational operations
First of all, let’s write the phone book class declaration.
// PhBook.h - this is a header file with
// declaration of PhBook class

#include <string>
using namespace std;

class PhBook
{
public:
    PhBook();            // default constructor
    PhBook(string InLastName, string InFirstName,
           string City, string AreaCode, string Phone);
    ~PhBook();            // destructor

    //Often used comparison operators
    bool operator==(const PhBook) const;
    bool operator>(const PhBook) const;
    bool operator<(const PhBook) const;

    //Seldom used comparison functions
    bool Equal(const PhBook) const;
    bool GreaterThan(const PhBook) const;
    bool LessThan(const PhBook) const;

    //Display functions
    void DisplayPhone() const;
    void DisplayCity() const;
private:
    string LastName;
    string FirstName;
    string City;
    string AreaCode;
    string Phone;
};

The phone book entry described in the class is not very complex. It consists of last name, first name, city, area code, and phone fields. Real phone books might also have middle names or initials, addresses, phone extensions, fax numbers, sometimes email addresses, and many other fields. The code specified string as a data type for all fields. You could use a numeric data type for area codes or phone numbers, but it does not make sense because you are not doing arithmetic operations on them. The string data type needs the string include file that comes with the Microsoft Visual C++ compiler. Other compilers might use other include files such as string.h or cstring.h.
Besides two constructors and a destructor, the class declaration specifies overloaded equality and relational operators: ==, <, and >. A complete class declaration needs three more overloaded operators: !=, >=, and <=.
The overloaded operators are used to provide comparison algorithms for the most common operations, such as comparison of last names and first names only. To perform more precise routines, declare three member functions: Equal, GreaterThan, and LessThan. The functions are similar to the equality and relational operators, but they are used to compare all fields in two phone book entries.
Finally, you declared display functions. The first function, DisplayPhone, is intended to display the name and the phone number including the area code of phone book entries. The second function, DisplayCity, will show the name and the city of the entries.


Previous Table of Contents Next


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.