count_list
Description
Count a string list ,then construct a string vector and an appearance frequency vector for all duplicate occurences
Syntax
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 )
Parameters
- vsList
- [input] string list to be counted
- vsUnique
- [output] unique strings in the given list
- vnCounts
- [output] frequency count of each unique string
- bCaseSensitive
- [input] true if string comparison is case sensitive
- vsList
- [input] string list to be counted
- vnIndices
- [output] corresponding array to hold indices of the counting
- bCaseSensitive
- [input] true if string comparison is case sensitive
Return
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
Examples
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
}
}
}
Remark
See Also
header to Include
origin.h
Reference
|