2.2.3.19.39 vectorbase::Sum

Description

Compute the sum of all elements of the vector.

Syntax

int Sum( int & nSum )


int Sum( double & dSum )


int Sum( complex & cxSum )

Parameters

nSum
[output]sum of all elements of the vector for int type


dSum
[output]sum of all elements of the vector for double type


cxSum
[output]sum of all the elements of the vector for complex type

Return

Returns 0 on success or a non-zero error code on failure.

Examples

EX1

void vectorbase_Sum_ex1()
{
    // Sum elements of a vector of type short
    vector<short> vecS = {1, 2, 3, 4, 5};
    int iSum;
    vecS.Sum(iSum);
    out_int("iSum = ",iSum);
    // Result:
    //        iSum = 15
}


EX2

void vectorbase_Sum_ex2()
{
    // Sum elements of a vector of type double
    vector vecD = {1.1, 2.1, 3.1, 4.1, 5.1}
    double dSum;
    vecD.Sum(dSum);
    out_double("dSum = ",dSum);
    // Result:
    //        dSum = 15.5
}


EX3

void vectorbase_Sum_ex3()
{
    // Sum elements of a vector of type complex
    vector<complex> vecC = {1+2i, 3+4i, 5-6i};
    complex cSum;
    vecC.Sum(cSum);
    out_complex("cSum = ",cSum);
    // Result:
    //        cSum = 9 + 0 i
}

Remark

Compute the sum of all elements of the vectorbase object. The argument type must be greater than or equal to the base type of the vectorbase object.

See Also

Data_sum

Header to Include

origin.h