swap()函数在valarray头文件中定义。此函数用于将一个valarray的内容与另一个valarray交换。
用法:
void swap( valarray& valarray2 );
参数:此方法接受一个参数valarray2,该参数表示另一个valarray,我们必须使用该valarray交换旧的valarray。
返回值:此函数不返回任何内容。
以下示例程序旨在说明上述函数:
示例1:
// C++ program to demonstrate
// example of swap() function.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initializing 1st valarray
valarray<int> varr1 = { 12, 24, 36, 48 };
// Initializing 2nd valarray
valarray<int> varr2 = { 20, 40, 60, 80 };
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 after swapping are : 20 40 60 80 The contents of 2nd valarray after swapping are : 12 24 36 48
示例2:
// C++ program to demonstrate
// example of swap() function.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initializing 1st valarray
valarray<int> varr1 = { -12, -24, -36, -48 };
// Initializing 2nd valarray
valarray<int> varr2 = { 20, 40, 60, 80 };
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 after swapping are : 20 40 60 80 The contents of 2nd valarray after swapping are : -12 -24 -36 -48
相关用法
- C++ valarray abs()用法及代码示例
- C++ valarray exp()用法及代码示例
- C++ valarray sin()用法及代码示例
- C++ valarray pow()用法及代码示例
- C++ valarray end()用法及代码示例
- C++ valarray tan()用法及代码示例
- C++ valarray cos()用法及代码示例
- C++ valarray log()用法及代码示例
- C++ valarray tanh()用法及代码示例
- C++ valarray asin()用法及代码示例
- C++ valarray resize()用法及代码示例
- C++ valarray atan2()用法及代码示例
- C++ valarray begin()用法及代码示例
注:本文由纯净天空筛选整理自bansal_rtk_大神的英文原创作品 valarray swap() function in c++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。