📜  std :: C++中的valarray类

📅  最后修改于: 2021-05-30 02:56:43             🧑  作者: Mango

C++ 98引入了一个名为valarray的特殊容器,可以有效地保存并提供对数组的数学运算。

  • 它支持按元素进行数学运算以及各种形式的广义下标运算符,切片和间接访问。
  • 与向量相比,valarray在某些数学运算中比向量更有效。

valarray类中的公共成员函数:

1. apply() :-该函数其参数中给出的操作应用于所有valarray元素,并返回具有操作值的新valarray

2. sum() :-此函数返回valarrays的所有元素的总和。

// C++ code to demonstrate the working of 
// apply() and sum()
#include
#include // for valarray functions
using namespace std;
int main()
{
    // Initializing valarray
    valarray varr = { 10, 2, 20, 1, 30 };
      
    // Declaring new valarray
    valarray varr1 ;
      
    // Using apply() to increment all elements by 5
    varr1 = varr.apply([](int x){return x=x+5;});
      
    // Displaying new elements value
    cout << "The new valarray with manipulated values is : ";
    for (int &x: varr1) cout << x << " ";
    cout << endl;
      
    // Displaying sum of both old and new valarray
    cout << "The sum of old valarray is : ";
    cout << varr.sum() << endl;
    cout << "The sum of new valarray is : ";
    cout << varr1.sum() << endl;
  
    return 0;
      
}

输出:

The new valarray with manipulated values is : 15 7 25 6 35 
The sum of old valarray is : 63
The sum of new valarray is : 88

3. min() :-此函数返回valarray的最小元素。

4. max() :-此函数返回valarray的最大元素。

// C++ code to demonstrate the working of 
// max() and min()
#include
#include // for valarray functions
using namespace std;
int main()
{
    // Initializing valarray
    valarray varr = { 10, 2, 20, 1, 30 };
      
    // Displaying largest element of valarray
    cout << "The largest element of valarray is : ";
    cout << varr.max() << endl;
      
    // Displaying smallest element of valarray
    cout << "The smallest element of valarray is : ";
    cout << varr.min() << endl;
  
    return 0;
      
}

输出:

The largest element of valarray is : 30
The smallest element of valarray is : 1

5.移(): -该函数返回在其参数提到的数目的切换元件后的新的valarray。如果数字为正,则应用左移;如果数字为负,则应用右移

6.环状移位(): -后循环移位(旋转)该函数返回新的valarray通过在其参数提到的数量的元件。如果数字为正,则应用左圆周移位;如果数字为负,则应用右圆周移位

// C++ code to demonstrate the working of 
// shift() and cshift()
#include
#include // for valarray functions
using namespace std;
int main()
{
    // Initializing valarray
    valarray varr = { 10, 2, 20, 1, 30 };
      
    // Declaring new valarray
    valarray varr1;
      
    // using shift() to shift elements to left
    // shifts valarray by 2 position
    varr1 = varr.shift(2);
      
    // Displaying elements of valarray after shifting
    cout << "The new valarray after shifting is : ";
    for ( int&x : varr1) cout << x << " ";
    cout << endl;
      
    // using cshift() to circulary shift elements to right
    // rotates valarray by 3 position
    varr1 = varr.cshift(-3);
      
    // Displaying elements of valarray after circular shifting
    cout << "The new valarray after circular shifting is : ";
    for ( int&x : varr1) cout << x << " ";
    cout << endl;
  
    return 0;
      
}

输出:

The new valarray after shifting is : 20 1 30 0 0 
The new valarray after circular shifting is : 20 1 30 10 2 

7. swap() :-此函数一个valarray与另一个valarray交换

// C++ code to demonstrate the working of 
// swap()
#include
#include // for valarray functions
using namespace std;
int main()
{
   // Initializing 1st valarray
    valarray varr1 = {1, 2, 3, 4};
       
    // Initializing 2nd valarray
    valarray varr2 = {2, 4, 6, 8};
       
     // Displaying valarrays before swapping
     cout << "The contents of 1st valarray "
             "before swapping are : ";
     for (int &x : varr1)
         cout << x << " ";
     cout << endl;
     cout << "The contents of 2nd valarray "
             "before swapping are : ";
     for (int &x : varr2)
         cout << x << " ";
     cout << endl;
    
     // Use of swap() to swap the valarrays
     varr1.swap(varr2);
    
     // Displaying valarrays after swapping
     cout << "The contents of 1st valarray "
             "after swapping are : ";
     for (int &x : varr1)
         cout << x << " ";
     cout << endl;
    
     cout << "The contents of 2nd valarray "
             "after swapping are : ";
     for (int &x : varr2)
         cout << x << " ";
     cout << endl;
  
    return 0;
      
}

输出:

The contents of 1st valarray before swapping are : 1 2 3 4 
The contents of 2nd valarray before swapping are : 2 4 6 8 
The contents of 1st valarray after swapping are : 2 4 6 8 
The contents of 2nd valarray after swapping are : 1 2 3 4 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”