C++ STL valarray assignment

#include <iostream>
#include <valarray>
using namespace std;

template <typename T> void printValarray(const valarray<T>& va){
    for(int i=0;i<va.size();++i){
        cout<<va[i]<<" ";
    }
    cout<<endl;
}
intmain()
{
    // Contains the value of 4 elements, the value of the element is not specified
    valarray<int> va1(4);
    printValarray(va1);
    // Contains 4 elements, all of which have a value of 3
    valarray<int> va2(3,4);
    printValarray(va2);
    //Define an integer array
    int ia [] = {1,2,3,4,5,6};
    //The size of the dynamic array is the same as the number of elements of the array ia
    valarray<int> va3(ia,sizeof(ia)/sizeof(ia[0]));
    printValarray(va3);
    //4 elements, the value is the first 4 elements of ia plus 1
    valarray<int> va4(ia+1,4);
    printValarray(va4);
    //Assign value to va1
    va1=(va2+va4)*va4;
    printValarray(va1);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325555072&siteId=291194637