Microsoft KB Archive/158233

= STL Sample for Basic Math Functions =

Article ID: 158233

Article Last Modified on 11/18/2003

-

APPLIES TO


 * The Standard C++ Library
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition

-



This article was previously published under Q158233



SUMMARY
The sample code below illustrates how to use basic math STL functions plus, minus, divides, times, and modulus in Visual C++.



Prototype
template struct plus : binary_function<_TYPE, _TYPE, _TYPE> {    _TYPE operator(const _TYPE& _X, const _TYPE& _Y) const {return (_X + _Y); } };  template struct minus : binary_function<_TYPE, _TYPE, _TYPE> {    _TYPE operator(const _TYPE& _X, const _TYPE& _Y) const {return (_X - _Y); } };  template struct times : binary_function<_TYPE, _TYPE, _TYPE> {    _TYPE operator(const _TYPE& _X, const _TYPE& _Y) const {return (_X * _Y); } };  template struct divides : binary_function<_TYPE, _TYPE, _TYPE> {    _TYPE operator(const _TYPE& _X, const _TYPE& _Y) const {return (_X / _Y); } };  template struct modulus : binary_function<_TYPE, _TYPE, _TYPE> {    _TYPE operator(const _TYPE& _X, const _TYPE& _Y) const {return (_X % _Y); } }; NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability. Consult your product documentation for information on the binary_function structure.

Description
This sample uses a class derived from all 5 basic math structures: plus, minus, times, divides, and modulus, using an integer as the templated operand.

Sample Code
//////////////////////////////////////////////////////////////// // // Compile options needed: none // // mathfunc.cpp - Illustrating the basic STL math //               functions. // // Structures: plus   - Adds data type A object to //                          a class object derived from plus. //            minus   - Subtracts data type A. //             times   - Multiplies object by data type A. //             divides - Divides object by data type A. //             modulus - Returns object modulo A. // // Written by Mark Hagen // of Microsoft Product Support Services, // Copyright (c) 1996 Microsoft Corporation. // All rights reserved. /////////////////////////////////////////////////////////////////

using namespace std;
 * 1) include

class MathOps : public plus, public minus , public times, public divides , public modulus { public: int value; MathOps{value=0;} MathOps(int x){value=x;} result_type operator+(second_argument_type add2) {return value + add2;} result_type operator-(second_argument_type sub2) {return value - sub2;} result_type operator*(second_argument_type mult2) {return value * mult2;} result_type operator/(second_argument_type div2) {return value / div2;} result_type operator%(second_argument_type mod2) {return value % mod2;} };

void main(void) { MathOps one,two,three,four,five,six;

cout << "Using MathOps class..." << endl ;

one = 18; cout << "one = " << one << endl ;

two = one + 1; cout << "two = one + 1 = " << two << endl ;

three = two - 2; cout << "three = two - 2 = " << three << endl ;

four = three * 3; cout << "four = three * 3 = " << four << endl ;

five = four / 4; cout << "five = four / 4 = " << five << endl ;

six = five % 5; cout << "six = five % 5 = " << six << endl ; } Program Output is: Using MathOps class... one = 18 two = one + 1 = 19 three = two - 2 = 17 four = three * 3 = 51 five = four / 4 = 12 six = five % 5 = 2

