![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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
|
![]() |
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.
|