|
-
January 7th, 2004, 11:57 AM
#20
As Paul mentioned, maybe put the output into a vector,
sort the vector, and then remove duplicates using unique:
Code:
int main()
{
std::string sVal = "ABBCCCDD";
std::vector<int> V;
GenerateUnique(V, sVal.size());
CombinationGenerator<int> CG(V, 3);
std::vector<int> Elements;
vector<string> vResults;
while ( CG.NextCombination(Elements) )
{
std::string s;
for ( int i = 0; i < 3; ++i )
{
s += sVal[Elements[i]];
}
vResults.push_back(s);
}
sort(vResults.begin(),vResults.end());
vResults.erase(unique(vResults.begin(),vResults.end()),vResults.end());
copy(vResults.begin(),vResults.end() , ostream_iterator<string>(cout,"\n"));
return 0;
}
This might not be practical for a very large number of
combinations. In the case where the number of unique
elements is very much smaller than the number of
total elements, it might be more efficient to do a
custom method: maybe an insertion-sort method, where
you can use a binary search to see if the element is
already in the results vector, and add it if not. This will
require extra copying, but if the number of unique elements
is relatively small, it might be faster than adding all elements,
then sorting, then removing duplicates.
Last edited by Philip Nicoletti; January 7th, 2004 at 12:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|