Find indices of specified values in vector.
It finds all the values that match the supplied condition and returns their indices. If the type of the vectorbase is double and dTestVal is missing value(NANUM), then only equality test(MATREPL_TEST_EQUAL) should be allowed.
int Find( vector<uint> & vecIndex, double dLower, double dUpper = _ONAN, int nPrecision = 8, int iBegin = 0, int iEnd = -1, DWORD dwCntrl = 0 )
int Find(uint wBitwiseOption, double dTestVal, vector<uint>& vn, int iBegin = 0, int iEnd = -1, double dTol = 0, DWORD dwCntrl = 0 )
enum { VECTORBASEFIND_IF_ONE_MATCH_THEN_RETURN = 0x00000001, };
returns -1 if invalid case, such as vecIndex is null; return 0 if not found, otherwise return "n" where n is the number of values found.
The total number of matched values (the size of vn), or a negative number for error.
EX1
void vectorbase_Find_ex1() { // Fill a vector with some values and output the index and values vector vecData = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}; int ii; printf("\nElements of the vector:\n"); for( ii = 0; ii < vecData.GetSize(); ii++ ) printf(" Index= %d, Value= %5.2f\n", ii, vecData[ii] ); vector<uint> vecIndex; int nFound; // Search for value = 0.6 printf("\nSearching for value = 0.6:\n"); nFound = vecData.Find(vecIndex, 0.6); if( -1 != nFound ) { printf("Found %d element(s)\n", nFound); for(ii = 0; ii < nFound; ii++) printf("Index= %d, Value= %5.2f\n", vecIndex[ii], vecData[vecIndex[ii]]); } else printf("Found no element(s)\n"); // Search for values between 0.25 and 0.6 printf("\nSearching for values between 0.25 and 0.6:\n"); nFound = vecData.Find(vecIndex, 0.25, 0.6); if( -1 != nFound ) { printf("Found %d element(s)\n", nFound); for(ii = 0; ii < nFound; ii++) printf("Index= %d, Value= %5.2f\n", vecIndex[ii], vecData[vecIndex[ii]]); } else printf("Found no element(s)\n"); // Search for value = 0.65 with precision set low printf("\nSearching for value = 0.65 with precision set to 1:\n"); nFound = vecData.Find(vecIndex, 0.65, _ONAN, 1); if( -1 != nFound ) { printf("Found %d element(s)\n", nFound); for(ii = 0; ii < nFound; ii++) printf("Index= %d, Value= %5.2f\n", vecIndex[ii], vecData[vecIndex[ii]]); } else printf("Found no element(s)\n"); // Search for value = 0.65 with precision set higher printf("\nSearching for value = 0.65 with precision set to 2:\n"); nFound = vecData.Find(vecIndex, 0.65, _ONAN, 2); if( -1 != nFound ) { printf("Found %d element(s)\n", nFound); for(ii = 0; ii < nFound; ii++) printf("Index= %d, Value= %5.2f\n", vecIndex[ii], vecData[vecIndex[ii]]); } else printf("Found no element(s)\n"); }
EX2
void vectorbase_Find_ex2() { vector vn = {1,2,3,4,5,6,7,8,9,10,0,5}; vector<uint> vIndex; int wBitwiseOption = MATREPL_TEST_EQUAL; int nRet = vn.Find(wBitwiseOption, 5.0, vIndex, 0, -1); printf("Found %d matches.",nRet); // nRet should be 2 here }
EX3
void vectorbase_Find_ex3() { vector vn = {2.5, 0, 6, 8.9, 4.56, 0, 1.49}; double dValToFind = 0; vector<uint> vIndex; DWORD dwCtrl = VECTORBASEFIND_IF_ONE_MATCH_THEN_RETURN; // directly return if once find one match. bool bIsExistedZero = ( 1 == vn.Find(MATREPL_TEST_EQUAL, dValToFind, vIndex, 0, -1, 0, dwCtrl) ); if( bIsExistedZero ) out_str("At least existed one zero."); }
Find indices of specified values in a vectorbase derived object.
origin.h