当前位置:网站首页>valarray数值库学习
valarray数值库学习
2022-07-28 16:14:00 【班公湖里洗过脚】
c++标准库表数组形式的类有三个:array:固定长度,目的为代替内置数组如: int a[5] = {1,3,5,7,9};vector:支持变长度的类, valarray:是表示并操作值数组的类,面向数值计算设计,支持对元素进行数学运算,并且支持多种形式的广义下标运算符、切片及间接访问。
下面我们来看valarray的使用示例:
成员函数
| 构造新的数值数组 (公开成员函数) | |
| 析构数值数组 (公开成员函数) | |
| 为内容赋值 (公开成员函数) | |
| 获取/设置 valarray 数组元素、切片或掩码 (公开成员函数) | |
| 对 valarray 的每个元素运用一元算术运算符 (公开成员函数) | |
| 对 valarray 的每个元素应用复合赋值运算符 (公开成员函数) | |
| 与另一 valarray 交换 (公开成员函数) | |
| 返回valarray的大小 (公开成员函数) | |
| 更改 valarray 的大小 (公开成员函数) | |
| 计算所有元素的和 (公开成员函数) | |
| 返回最小元素 (公开成员函数) | |
| 返回最大的元素 (公开成员函数) | |
| 以填入零的方式移动 valarray 的元素 (公开成员函数) | |
| 循环移动 valarray 的元素 (公开成员函数) | |
| 对 valarray 的每个元素应用函数 (公开成员函数) |
1.valarray初始化
#include <iostream>
#include <valarray>
using namespace std;
//输出valarray
template <typename T>
void outValarray(const valarray<T>& val)
{
for(auto v: val)
cout << v << "\t";
cout << endl;
}
int main()
{
//1.创建一个大小为5的valarray
valarray<int> val(5);
outValarray(val);
//2.使用列表{1,3,5,7,9}初始化一个valarray
valarray<int> val2 = {1,3,5,7,9};
outValarray(val2);
//3.创建一个大小为5,值全为7的valarray
valarray<int> val3(7, 5);
outValarray(val3);
//4.使用数组初始化valarray
int ia[] = {10,20,30,40,50};
valarray<int> val4(ia, sizeof(ia)/sizeof(ia[0]));
outValarray(val4);
//5.使用数组创建大小为3的valarray
valarray<int> val5(ia + 2, 3);
outValarray(val5);
//6.使用valarray进行数学运算
val = (val2 + val4)*val4;
outValarray(val);
cout << "Hello World!" << endl;
return 0;
}运行结果:

2.valarray一元算术运算:
#include <iostream>
#include <valarray>
using namespace std;
//输出valarray
template <typename T>
void outValarray(const valarray<T>& val)
{
for(auto v: val)
cout << v << "\t";
cout << endl;
}
int main()
{
//1.operator+运算,给每个元素+11
valarray<int> val = {1,3,5,7,9};
outValarray(val);
val = val + 11;
outValarray(val);
//2.operator-运算,给每个元素-5
val = val - 5;
outValarray(val);
//3.operator~运算,给每个元素~
val = ~val;
outValarray(val);
//4.operator !运算,给每个元素!
outValarray(!val);
cout << "Hello World!" << endl;
return 0;
}运行结果:

3.对valarray的每个元素应用复合赋值运算符:
#include <iostream>
#include <valarray>
using namespace std;
//输出valarray
template <typename T>
void outValarray(const valarray<T>& val)
{
for(auto v: val)
cout << v << "\t";
cout << endl;
}
int main()
{
//1. operator+=运算
valarray<int> val = {1,3,5,7,9};
outValarray(val);
val += 11;
outValarray(val);
//2. operator-=运算
val -= 2;
outValarray(val);
//3. operator*=运算
val *= 3;
outValarray(val);
//4. operator/=运算
val /= 2;
outValarray(val);
//5. operator%=运算
val %= 5;
outValarray(val);
//6. operator&=运算
val &= 5;
outValarray(val);
//7. operator|=运算
val |= 1;
outValarray(val);
//8. operator^=运算
val ^= 2;
outValarray(val);
//9. operator<<=运算
val <<= 2;
outValarray(val);
//10. operator>>=运算
val >>= 1;
outValarray(val);
cout << "Hello World!" << endl;
return 0;
}
运算结果:

4.成员函数使用示例:
#include <iostream>
#include <valarray>
using namespace std;
//输出valarray
template <typename T>
void outValarray(const valarray<T>& val)
{
for(auto v: val)
cout << v << "\t";
cout << endl;
}
int main()
{
//1.swap 与另一 valarray 交换
valarray<int> val0 = {1,3,5,7,9};
valarray<int> val1 = {11,31,51,71,91};
outValarray(val0);
outValarray(val1);
val0.swap(val1);
cout << endl;
outValarray(val0);
outValarray(val1);
cout << endl;
//2.size返回valarray的大小
valarray<int> val2 = {1,3,5,7,9,11,13,15};
cout << "val2.size===== " << val2.size() << endl;
cout << endl;
//3.resize 更改 valarray 的大小
valarray<int> val3 = {1,3,5};
outValarray(val3);
val3.resize(5);
outValarray(val3);
val3.resize(6, 10);
outValarray(val3);
cout << endl;
//4.sum 计算所有元素的和
valarray<int> val = {13,33,59,72,95,11,13,15};
outValarray(val);
cout << "val.sum===== " << val.sum() << endl;
cout << endl;
//5.min返回最小元素
cout << "val.min===== " << val.min() << endl;
cout << endl;
//6.max返回最大的元素
cout << "val.max===== " << val.max() << endl;
cout << endl;
//7.shift 以填入零的方式移动 valarray 的元素
valarray<int> val4 = {13,33,59,72,95,11,15};
outValarray(val4);
valarray<int> val5 = val4.shift(3);
outValarray(val5);
cout << endl;
//8.cshift 循环移动 valarray 的元素
valarray<int> val6 = {13,33,59,72,95,11,15};
outValarray(val6);
valarray<int> val7 = val6.cshift(3);
outValarray(val7);
cout << endl;
//9.apply对 valarray 的每个元素应用函数
valarray<int> val8 = {3,32,59,72,95,11,15};
outValarray(val8);
val8 = val8.apply([](int n)->int{return n*2;});
outValarray(val8);
cout << "Hello World!" << endl;
return 0;
}运行结果:

参考:
边栏推荐
- Microsoft: edge browser has built-in disk cache compression technology, which can save space and not reduce system performance
- 结构化设计的概要与原理--模块化
- Ruoyi's solution to error reporting after integrating flyway
- 阿里大哥教你如何正确认识关于标准IO缓冲区的问题
- MySQL安装教程
- 如何使用Fail2Ban保护WordPress登录页面
- Epoll horizontal departure, which edge triggers
- [deep learning]: day 1 of pytorch introduction to project practice: data operation and automatic derivation
- After paying $1.8 billion in royalties to Qualcomm, Huawei reportedly ordered 120million chips from MediaTek! Official response
- Understanding of asmlinkage
猜你喜欢

HTAP comes at a price

【深度学习】:《PyTorch入门到项目实战》第一天:数据操作和自动求导

MySQL安装教程

【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)

Unity editor learning (I) using features to change the display of fields in components

【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)

Easypoi --- excel file export
![[deep learning]: day 8 of pytorch introduction to project practice: weight decline (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: day 8 of pytorch introduction to project practice: weight decline (including source code)

Applet: scroll view slides to the bottom by default

ERROR: transport library not found: dt_ socket
随机推荐
Some opinions on bug handling
Unity shader uses rendered texture to achieve glass effect
Android Development - set cache
Time complexity
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
综合设计一个OPPE主页--页面服务部分
[deep learning]: day 6 of pytorch introduction to project practice: multi-layer perceptron (including code)
RE14: reading paper illsi interpretable low resource legal decision making
在AD中添加差分对及连线
Create a self-organizing / safe / controllable Lora network! Semtech responded for the first time to the impact of the "new regulations of the Ministry of industry and information technology"
SUSE Ceph 快速部署 – Storage6
Games101 section 13 ray tracing notes
Unity shader procedural texture
mysql 最大建议行数2000w,靠谱吗?
Efficiency comparison of three methods for obtaining timestamp
Games101 assignment04 job 04
2020Q2全球平板市场出货大涨26.1%:华为排名第三,联想增幅最大!
The longest substring of sword finger offer without repeated characters
Binary representation of negative integers and floating point numbers
阿里大哥教你如何正确认识关于标准IO缓冲区的问题