Microsoft KB Archive/156811

= How to use the abs STL function in Visual C++ =

Article ID: 156811

Article Last Modified on 8/11/2005

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q156811

<div class="summary_section">

SUMMARY
The sample code below illustrates how to use the abs STL function in Visual C++.

<div class="moreinformation_section">

Prototype
template<class T>  valarray<T> abs(const valarray<T>& x); NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description
The sample declares a valarray of integers and uses STL abs function to get the absolute value for each array element.

Sample Code
NOTE: The first line in the sample code section says:

// Compile options needed: /GX

In VC++ .NET, /EHsc is set by default and is equivalent to /GX. ////////////////////////////////////////////////////////////////////// // // Compile options needed: None // // : main.cpp // // Functions: // //   abs // // Written by Yeong-Kah Tam // of Microsoft Product Support Services, // Copyright (c) 1996 Microsoft Corporation. All rights reserved. //////////////////////////////////////////////////////////////////////

// disable warning C4267: possible loss of data, // okay to ignore


 * 1) pragma warning(disable: 4267)


 * 1) include                // for i/o functions
 * 2) include                // for valarray

using namespace std; // std c++ libs implemented in std #endif
 * 1) if _MSC_VER > 1020  // if VC++ version is > 4.2


 * 1) define ARRAY_SIZE 10              // array size

typedef valarray INTVALARRAY; // type for valarray of ints

void main

{

// Initialize val_array to 0, -1, -2, etc.   INTVALARRAY val_array(ARRAY_SIZE); for (int i = 0; i < ARRAY_SIZE; i++) val_array[i] = -i;

// Display the size of val_array. cout << "Size of val_array = " << val_array.size << "\n\n";

// Display the values of val_array before calling abs. cout << "The values of val_array before calling abs:\n"; for (i = 0; i < ARRAY_SIZE; i++) cout << val_array[i] << "   "; cout << "\n\n";

// Display the result of val_array after calling abs. INTVALARRAY abs_array = abs(val_array); cout << "The result of val_array after calling abs:\n"; for (i = 0; i < ARRAY_SIZE; i++) cout << abs_array[i] << "    "; cout << "\n\n";

} Output: <pre class="fixed_text">Size of val_array = 10

The values of val_array before calling abs: 0   -1    -2    -3    -4    -5    -6    -7    -8    -9

The result of val_array after calling abs: 0    1     2     3     4     5     6     7     8     9

<div class="references_section">