Click Here!
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 accumulated value has two components. The first is the initial value kept in the ResultSum variable. Set this value to 0.0. Under different circumstances, the value can be other than zero (usually it keeps the balance of the previous financial period). The second component of the accumulated value is the sum of the vector’s values. The accumulate algorithm takes the first iterator in the range, which is Transaction.begin() in this example, and takes all the values up to the last iterator, which is Transaction.end(). If there is no data in the container, the algorithm will return the initial value. Therefore, the existence of the initial value is quite important for the algorithm’s reliability.
Finally, when the program is running it shows the string
The balance of the transations is 7144.15

The result represents the balance of the transaction set.
2.  Accumulating data using subtraction
For the same accounting application, change the task slightly. Now, calculate expenses by subtracting them from the initial value. To do that, you must make a few changes to the program.
// accum.cpp

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <functional>
using namespace std;

int main()
{
    vector <float> Transaction;
    float InitValue= 10000.0;
    float ResultSum;

    Transaction.push_back(18.56);
    Transaction.push_back(100.00);
    Transaction.push_back(123.01);
    Transaction.push_back(7610.23);
    Transaction.push_back(507.65);

    ResultSum = accumulate (Transaction.begin(),
    ⇒Transaction.end(), InitValue, minus <float>());

    cout << “The balance of the transations is “
         << ResultSum << endl;
    return 0;
}

First of all, based on the task, change the initial value and make all transaction amounts positive. To calculate the result, use the accumulate algorithm with the fourth argument that specifies the calculations. The fourth argument can be any function object. Note that the algorithm consequently takes the values and applies the function to the result. In this case the steps in the processing of the algorithms are
1.  Result1 = InitValue–Value1
2.  Result2 = Result1–Value2
3.  Result3 = Result2–Value3
4.  Result4 = Result3–Value4
5.  Result5 = Result4–Value5

where Result1 to Result5 are the internal intermediate values, and Value1 to Value5 are the values of the Transaction vector elements.
To calculate the result, use the minus function from the STL. The function is defined in the <functional> header file; therefore, you have to include it into your code.
If you run the program it will show
The balance of the transations is 1640.55

The result represents the rest of the funds after the expenses are calculated.
3.  Creating accumulated sums
Now create another container that will keep the accumulated sums. In other words, keep the values Value1, Value1+Value2, Value1+Value2+Value3, and so on.
// accum.cpp

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
using namespace std;

int main()
{
    vector <float> Transaction;
    vector <float> Sums(5);
    vector <float>:: iterator i;

    Transaction.push_back(18.56);
    Transaction.push_back(-100.00);
    Transaction.push_back(123.01);
    Transaction.push_back(7610.23);
    Transaction.push_back(-507.65);

    partial_sum (Transaction.begin(), Transaction.end(),
    ⇒Sums.begin());

    for (i = Sums.begin(); i!= Sums.end(); i++)
        cout << *i << endl;

    return 0;
}

The program works with the two vector containers. The first one is the Transaction vector from the first example in this section. The vector is initialized by the push_back vector function. The second container, Sums, is intended to keep the accumulated sums. It is created in such a way that the constructor makes space for the first five elements in it:
vector <float> Sums(5);

This is a very important point. If you did not create the elements in the vector, your program would crash because the algorithm that you use does not create new elements; it just fills in the existing ones.
To create the sums, use the partial_sum algorithm. The algorithm has three arguments. The first two arguments specify the range of the container data to process, the third argument specifies the beginning of the output container.
The output of the program is
18.56
-81.44
41.57
7651.8
7144.15

No wonder the last line results in the same value as the first example in this How-To.
4.  Creating accumulated products
Similar to the previous example, this example will accumulate the values. However, now you create products in the output vector. The program will calculate the values 1, 1*1/2, 1*1/2*1/3, and so on, and move them in the Products vector.
// accum.cpp

#include <iostream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
using namespace std;

int main()
{
    vector <float> SourceValues;
    vector <float> Products(5);
    vector <float>:: iterator i;

    SourceValues.push_back(1.);
    SourceValues.push_back(1./2.);
    SourceValues.push_back(1./3.);
    SourceValues.push_back(1./4.);
    SourceValues.push_back(1./5.);

    partial_sum (SourceValues.begin(), SourceValues.end(),
                     Products.begin(), multiplies <float>());
    for (i = Products.begin(); i!= Products.end(); i++)
        cout << *i << endl;

    return 0;
}

This example works with the Microsoft Visual C++ version 5 compiler. If you use the Borland C++ 5 compiler, you have to change the multiplies function object to the times function object. Currently, the STL considers times obsolete.


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.