当前位置:网站首页>Valarray Library Learning
Valarray Library Learning
2022-07-28 17:16:00 【I washed my feet in Bangong Lake】
c++ There are three classes in the form of standard library table array :array: Fixed length , The purpose is to replace the built-in array, such as : int a[5] = {1,3,5,7,9};vector: Support variable length classes , valarray: Is a class that represents and operates on an array of values , Design for numerical computation , Support mathematical operations on elements , And support various forms of generalized subscript operators 、 Slicing and indirect access .
So let's see valarray Use example of :
Member functions
| Construct a new numeric array ( Expose member functions ) | |
| Destruct an array of values ( Expose member functions ) | |
| Assign value to content ( Expose member functions ) | |
| obtain / Set up valarray Array elements 、 Slice or mask ( Expose member functions ) | |
| Yes valarray Use unary arithmetic operators for each element of ( Expose member functions ) | |
| Yes valarray Apply the compound assignment operator to each element of ( Expose member functions ) | |
| With another valarray In exchange for ( Expose member functions ) | |
| return valarray Size ( Expose member functions ) | |
| change valarray Size ( Expose member functions ) | |
| Calculate the sum of all elements ( Expose member functions ) | |
| Returns the smallest element ( Expose member functions ) | |
| Returns the largest element ( Expose member functions ) | |
| Move by filling in zero valarray The elements of ( Expose member functions ) | |
| Cyclic movement valarray The elements of ( Expose member functions ) | |
| Yes valarray Each element of the application function ( Expose member functions ) |
1.valarray initialization
#include <iostream>
#include <valarray>
using namespace std;
// Output valarray
template <typename T>
void outValarray(const valarray<T>& val)
{
for(auto v: val)
cout << v << "\t";
cout << endl;
}
int main()
{
//1. Create a size of 5 Of valarray
valarray<int> val(5);
outValarray(val);
//2. Use list {1,3,5,7,9} Initialize a valarray
valarray<int> val2 = {1,3,5,7,9};
outValarray(val2);
//3. Create a size of 5, It's worth it all 7 Of valarray
valarray<int> val3(7, 5);
outValarray(val3);
//4. Initialize with an array valarray
int ia[] = {10,20,30,40,50};
valarray<int> val4(ia, sizeof(ia)/sizeof(ia[0]));
outValarray(val4);
//5. Use an array to create a size of 3 Of valarray
valarray<int> val5(ia + 2, 3);
outValarray(val5);
//6. Use valarray Do math
val = (val2 + val4)*val4;
outValarray(val);
cout << "Hello World!" << endl;
return 0;
}Running results :

2.valarray Unary arithmetic operation :
#include <iostream>
#include <valarray>
using namespace std;
// Output valarray
template <typename T>
void outValarray(const valarray<T>& val)
{
for(auto v: val)
cout << v << "\t";
cout << endl;
}
int main()
{
//1.operator+ operation , Give each element +11
valarray<int> val = {1,3,5,7,9};
outValarray(val);
val = val + 11;
outValarray(val);
//2.operator- operation , Give each element -5
val = val - 5;
outValarray(val);
//3.operator~ operation , Give each element ~
val = ~val;
outValarray(val);
//4.operator ! operation , Give each element !
outValarray(!val);
cout << "Hello World!" << endl;
return 0;
}Running results :

3. Yes valarray Apply the compound assignment operator to each element of :
#include <iostream>
#include <valarray>
using namespace std;
// Output valarray
template <typename T>
void outValarray(const valarray<T>& val)
{
for(auto v: val)
cout << v << "\t";
cout << endl;
}
int main()
{
//1. operator+= operation
valarray<int> val = {1,3,5,7,9};
outValarray(val);
val += 11;
outValarray(val);
//2. operator-= operation
val -= 2;
outValarray(val);
//3. operator*= operation
val *= 3;
outValarray(val);
//4. operator/= operation
val /= 2;
outValarray(val);
//5. operator%= operation
val %= 5;
outValarray(val);
//6. operator&= operation
val &= 5;
outValarray(val);
//7. operator|= operation
val |= 1;
outValarray(val);
//8. operator^= operation
val ^= 2;
outValarray(val);
//9. operator<<= operation
val <<= 2;
outValarray(val);
//10. operator>>= operation
val >>= 1;
outValarray(val);
cout << "Hello World!" << endl;
return 0;
}
Calculation results :

4. Use examples of member functions :
#include <iostream>
#include <valarray>
using namespace std;
// Output valarray
template <typename T>
void outValarray(const valarray<T>& val)
{
for(auto v: val)
cout << v << "\t";
cout << endl;
}
int main()
{
//1.swap With another valarray In exchange for
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 return valarray Size
valarray<int> val2 = {1,3,5,7,9,11,13,15};
cout << "val2.size===== " << val2.size() << endl;
cout << endl;
//3.resize change valarray Size
valarray<int> val3 = {1,3,5};
outValarray(val3);
val3.resize(5);
outValarray(val3);
val3.resize(6, 10);
outValarray(val3);
cout << endl;
//4.sum Calculate the sum of all elements
valarray<int> val = {13,33,59,72,95,11,13,15};
outValarray(val);
cout << "val.sum===== " << val.sum() << endl;
cout << endl;
//5.min Returns the smallest element
cout << "val.min===== " << val.min() << endl;
cout << endl;
//6.max Returns the largest element
cout << "val.max===== " << val.max() << endl;
cout << endl;
//7.shift Move by filling in zero valarray The elements of
valarray<int> val4 = {13,33,59,72,95,11,15};
outValarray(val4);
valarray<int> val5 = val4.shift(3);
outValarray(val5);
cout << endl;
//8.cshift Cyclic movement valarray The elements of
valarray<int> val6 = {13,33,59,72,95,11,15};
outValarray(val6);
valarray<int> val7 = val6.cshift(3);
outValarray(val7);
cout << endl;
//9.apply Yes valarray Each element of the application function
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;
}Running results :

Reference resources :
边栏推荐
- 浏览器解码过程分析
- [deep learning]: day 8 of pytorch introduction to project practice: weight decline (including source code)
- 解决SQL Server数据库独占的问题
- Jupyter notebook win installation record
- Goweb开发之Beego框架实战:第四节 数据库配置及连接
- UNIQUE VISION Programming Contest 2022(AtCoder Beginner Contest 248)G. GCD cost on the tree
- Re10: are we really making much progress? Revisiting, benchmarking, and refining heterogeneous gr
- Alibaba cloud MSE supports go language traffic protection
- Unity shader cartoon style rendering
- Deep understanding of deepsea and salt deployment tools – storage6
猜你喜欢

总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对

Reasoning Over Semantic-Level Graph for Fact Checking

Goweb开发之Beego框架实战:第一节 Beego框架介绍

Unity3d shader achieves ablation effect

Goweb开发之Beego框架实战:第三节 程序执行流程分析

Unity shader screen post-processing

net框架

DGL Chapter 1 (official tutorial) personal notes

What does the service grid that has been popular for two years bring to microservices? (Reprinted)
Read excel xlsx format file in unity
随机推荐
valarray数值库学习
go语言慢速入门——流程控制语句
Kubernetes service and ingress you need to master
微服务架构-服务注册中心和服务网关(6.8) (转载)
Steps to configure V530 switch
2019年全球移动通信基站市场:爱立信、华为、诺基亚分列前三
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"
Ugui learning notes (IV) ugui event system overview and Usage Summary
After paying $1.8 billion in royalties to Qualcomm, Huawei reportedly ordered 120million chips from MediaTek! Official response
Unity3d shader achieves ablation effect
Global mobile communication base station market in 2019: Ericsson, Huawei and Nokia ranked in the top three
Codeforces round 770 (Div. 2) F. Fibonacci additions (construction + difference)
: No such file or directory
向高通支付18亿美元专利费之后,传华为向联发科订购了1.2亿颗芯片!官方回应
Easypoi --- excel file export
Function接口之andThen
Codeforces round 770 (Div. 2) e. fair share
Android Development - set cache
批量下载文件
Some attention code explanations