当前位置:网站首页>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;
}运行结果:

参考:
边栏推荐
- Ugui learning notes (II) Scrollview related
- Realize the reset function of steering wheel UI with touch rotation and finger departure in unity
- PostgreSQL每周新闻—2022年7月20日
- 如何使用Fail2Ban保护WordPress登录页面
- SUSE CEPH rapid deployment – storage6
- [JS] eight practical new functions of 1394-es2022
- Leetcode9. Palindromes
- Re10: are we really making much progress? Revisiting, benchmarking, and refining heterogeneous gr
- 大学生参加六星教育PHP培训,找到了薪水远超同龄人的工作
- RE14: reading paper illsi interpretable low resource legal decision making
猜你喜欢

【深度学习】:《PyTorch入门到项目实战》第二天:从零实现线性回归(含详细代码)

概率论与数理统计第一章
![[deep learning]: day 1 of pytorch introduction to project practice: data operation and automatic derivation](/img/4e/a41eee56fc0e8d3089f105bcb63155.png)
[deep learning]: day 1 of pytorch introduction to project practice: data operation and automatic derivation

【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)

College students participated in six Star Education PHP training and found jobs with salaries far higher than those of their peers

Brother Ali teaches you how to correctly understand the problem of standard IO buffer

Add differential pairs and connections in Ad

Binary representation of negative integers and floating point numbers

Games101-assignment05 ray tracing - rays intersect triangles

【深度学习】:《PyTorch入门到项目实战》第四天:从0到1实现logistic回归(附源码)
随机推荐
Leetcode70 suppose you are climbing stairs. You need n steps to reach the roof. You can climb one or two steps at a time. How many different ways can you climb to the roof?
SUSE Ceph 快速部署 – Storage6
MySQL installation tutorial
Deep understanding of deepsea and salt deployment tools – storage6
Ugui learning notes (VI) get the information of the clicked UI
Read excel xlsx format file in unity
Realize the reset function of steering wheel UI with touch rotation and finger departure in unity
Re13:读论文 Gender and Racial Stereotype Detection in Legal Opinion Word Embeddings
传英伟达已与软银展开会谈,将出价超过320亿美元收购Arm
在AD中添加差分对及连线
【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)
Brother Ali teaches you how to correctly understand the problem of standard IO buffer
13 differences between MySQL and Oracle
[deep learning]: day 6 of pytorch introduction to project practice: multi-layer perceptron (including code)
Summary of kubenertes 1.16 cluster deployment problems
综合设计一个OPPE主页--页面服务部分
Comprehensively design an oppe homepage -- after sales service of the page
After paying $1.8 billion in royalties to Qualcomm, Huawei reportedly ordered 120million chips from MediaTek! Official response
Do you really understand CMS garbage collector?
Question making note 3 (two point search)