Count a string list ,then construct a string vector and an appearance frequency vector for all duplicate occurences
int count_list( const vector<string> & vsList, vector<string> & vsUnique, vector<uint> & vnCounts, bool bCaseSensitive = false )
bool count_list( const vector<string> & vsList, vector<int> & vnIndices, bool bCaseSensitive = false )
total number of multiple occurences. 0 if input strings are already all unique
true if there is at least one duplicates in the given list. false if input strings are all unique
EX1
void count_list_ex1() { vector<string> vsList = {"Boston", "New York", "DC", "New York", "Tempa", "boston","New York"}; vector<string> vsUnique; vector<uint> vnCounts; int nCount = count_list(vsList, vsUnique, vnCounts, true); //case sensitive out_int("nCount = ",nCount); //nCount equals to 2 if(nCount > 0) { printf("Duplicate string found\n"); for(int ii = 0; ii < vsUnique.GetSize(); ii++) { if(vnCounts[ii] > 1) printf("%s: %d\n", vsUnique[ii], vnCounts[ii]); //New York: 3 } } }
EX2
void count_list_ex2() { vector<string> vsList = {"Boston", "New York", "Boston", "DC", "New York", "Tempa","New York"}; vector<int> vnIndices; if(count_list(vsList, vnIndices)) { printf("Duplicate string found\n"); for(int ii = 0; ii < vnIndices.GetSize(); ii++) { printf("%s: %d\n", vsList[ii], vnIndices[ii]); //Output indices for all duplicate occurences } } }
origin.h