vectorbase::Reorder

Description

Reorder elements of the vector

Syntax

BOOL Reorder( const vector<uint> & vnIndices )

Parameters

vnIndices
[input] A 0-offset indices vector to be used for reordering the current vector. Indices should be the position of the elements in original vector. The indices must be all unique and should include all of the indices in the current vector.

Return

Returns TRUE on success or FALSE on error.

Examples

EX1

void vectorbase_Reorder_ex1()
{
    // Declare a string vector and fill with some data
    vector<string> vecStr1 = {"A", "B", "C"};
    
    // Declare vector with indices for reordering
    vector<uint> vecIndex1 = {0, 2, 1};
    
    // Reorder vecStr1 using vecIndex
    vecStr1.Reorder(vecIndex1);
    printf("vecStr1 = ");
    for (int ii = 0; ii < vecStr1.GetSize(); ii++)
        printf("%s ",vecStr1[ii]);
 
    // Result:
    //         vecStr1 = "A", "C", "B"

}

EX2

void vectorbase_Reorder_ex2()
{
    // The following code shows how to sort one vector
    // based on contents of another
    vector<string> vecStr2 = {"A", "B", "C"};
    vector vecWeight = {0.4, 0.1, 0.2};
    
    // First get an index vector based on sorting vecWeight
    vector<uint> vecIndex2;
    vecWeight.Sort(SORT_ASCENDING, TRUE, vecIndex2);
    
    // Now reorder vecStr2 using this index vector
    vecStr2.Reorder(vecIndex2);
    printf("\nvecStr2 = ");
    for (int ii = 0; ii < vecStr2.GetSize(); ii++)
        printf("%s ",vecStr2[ii]);
    
    // Result:
    //        vecStr2 = "B", "C", "A"
}

EX3

void vectorbase_Reorder_ex3()
{
    vector vTemp = {6,4,3,7,8,0,1,2,5}; 
    vector<uint> vN = {6,4,3,7,8,0,1,2,5}; 
 
    vector<uint> vnIndex;
    vN.Sort(SORT_ASCENDING, TRUE, vnIndex);
 
    bool bRet = vTemp.Reorder(vnIndex);     
    for (int ii = 0; ii < vTemp.GetSize(); ii++)
        printf("%g ",vTemp[ii]);
    
    //Result:
    //      vTemp ={0,1,2,3,4,5,6,7,8};
}

Remark

Reorder the vector according to the specified index vector

See Also

vectorbase::Sort

header to Include

origin.h