abs (STL Samples)

Viene illustrato come utilizzare la funzione della libreria di modelli (STL) standard di ABS in Visual C++.

template<class T>
   valarray<T> abs(
      const valarray<T>& x 
   );

Note

[!NOTA]

La classe/nomi di parametro nel prototipo non corrisponde alla versione nel file di intestazione.alcuni sono stati modificati per migliorare la leggibilità.

Nell'esempio viene dichiarato un valarray di Integer e utilizza la funzione di abs STL per ottenere il valore assoluto per ogni elemento della matrice.

Esempio

// abs_main.cpp
// compile with: /EHsc
#include <iostream>                 // for i/o functions
#include <valarray>                 // for valarray
using namespace std;

#define ARRAY_SIZE  10              // array size
typedef valarray<int> INTVALARRAY;  // type for valarray of ints

int main()
{
    int i;
    // Initialize val_array to 0, -1, -2, etc.
    INTVALARRAY val_array(ARRAY_SIZE);
    for (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 result of val_array before calling abs():\n";
    for (i = 0; i < ARRAY_SIZE; i++)
    {
        cout << val_array[i];
        if (i < ARRAY_SIZE - 1)
            cout << ", ";
    }
    cout << endl << endl;

    // 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];
        if (i < ARRAY_SIZE - 1)
            cout << ", ";
    }
    cout << endl;
}

Output

Size of val_array = 10

The result 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

Requisiti

intestazione: <valarray>

Vedere anche

Concetti

Esempi di una libreria di modelli standard