STD::VC++中的ValSube类

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

null
  • 它支持元素级数学运算和各种形式的广义下标运算符、切片和间接访问。
  • 与向量相比,数组在某些数学运算中也比向量有效。

valarray类中的公共成员函数:

1.申请 :-这个功能 应用操纵 在其论点中给出 对所有人 valarray元素立即和 返回一个新数组 被操纵的价值观。

2.sum() :-这个功能 返回总和 所有元素的数组。

// C++ code to demonstrate the working of
// apply() and sum()
#include<iostream>
#include<valarray> // for valarray functions
using namespace std;
int main()
{
// Initializing valarray
valarray< int > varr = { 10, 2, 20, 1, 30 };
// Declaring new valarray
valarray< int > 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() :-此函数返回 最小的 数组的元素。

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

// C++ code to demonstrate the working of
// max() and min()
#include<iostream>
#include<valarray> // for valarray functions
using namespace std;
int main()
{
// Initializing valarray
valarray< int > 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.班次 :-此函数在 变换元素 通过 这个 数字 在其论点中提到。如果 这个数字是正数 , 左移 适用于,如果 数字是负数 , 右移 应用了。

6.cshift() :-此函数在 循环移位(旋转) 元素 通过 这个 数字 在其论点中提到。如果 数字为正,左循环 转移 适用于,如果 数字为负,向右循环移位 应用了。

// C++ code to demonstrate the working of
// shift() and cshift()
#include<iostream>
#include<valarray> // for valarray functions
using namespace std;
int main()
{
// Initializing valarray
valarray< int > varr = { 10, 2, 20, 1, 30 };
// Declaring new valarray
valarray< int > 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.互换 :-这个功能 交换 一个接一个。

// C++ code to demonstrate the working of
// swap()
#include<iostream>
#include<valarray> // for valarray functions
using namespace std;
int main()
{
// Initializing 1st valarray
valarray< int > varr1 = {1, 2, 3, 4};
// Initializing 2nd valarray
valarray< int > 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 

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享