vector::InsertValue

Description

It inserts the same double type value at the multiple locations inside a vector. The vector itself is resized as needed.

Syntax

int InsertValue( vector<int> & vnIndices, double dValue )
int InsertValue( vector<int> & vnIndices, vectorbase& vVals )

Parameters

vnIndices
[input] the vector of indices where the inserted values should appear AFTER the insertion. For example:
If the vector before calling method has the following values (size of the vector is 5):
10 20 30 40 50
and vnIndices is:
0 2 6 8
and dValue is 100, then after the insertion the vector should be (the new size should be equal to the old size plus the size of vnIndices, which in this case means 5 + 4 = 9):
100 10 100 20 30 40 100 50 100
dValue
[input] the double type value to insert. It can be other numeric type, like int, float, here will auto case from the original type to double type.
vVals
[input] Values to insert. Its size must be equal to vnIndices.

Return

If OK, it returns the new size of the vector. If error, it returns a negative number.

Examples

EX1

//Insert double value into double vector
void vector_InsertValue_Ex1()
{
    vector<double> vd = {1.1, 2.2, 3.3, 4.4, 5.3};
    vector<int> vnIndices = {0, 4, 9};
 
    double dVal = 38.5;
    vd.InsertValue(vnIndices, dVal); //Insert double type
 
    for (int ii = 0; ii < vd.GetSize(); ii++)
    {
        out_double("", vd[ii]); 
    }
    // Output will be: 38.5, 1.1, 2.2, 3.3, 38.5, 4.4, 5.3, 0, 0, 38.5
}

EX2

//Insert int value into int vector
void vector_InsertValue_Ex2()
{
    vector<int> vnData = {1, 2, 3, 4, 5};
    vector<int> vnIndices = {0, 4, 9};

    int nVal = 100;
    vnData.InsertValue(vnIndices, nVal); //Change the type to double then change it back

    for (int ii = 0; ii < vnData.GetSize(); ii++)
    {
        out_int("", vnData[ii]); 
    }
        // Output will be: 100,1,2,3,100,4,5,0,0,100
}

EX3

//Insert values into vector
void vector_InsertValue_Ex3()
{
    vector<double> vd = {1.1, 2.2, 3.3, 4.4, 5.3};
    vector<int> vnIndices = {0, 4, 9};
    vector<double> vVals = {1000, 2000, 3000};
 
    double dVal = 38.5;
    vd.InsertValue(vnIndices, vVals);
 
    for (int ii = 0; ii < vd.GetSize(); ii++)
    {
        out_double("", vd[ii]); 
    }
    // Output will be: 1000, 1.1, 2.2, 3.3, 2000, 4.4, 5.3, 0, 0, 3000
}

Remark

See Also

vector::InsertAt

header to Include

origin.h