I have a A=set of strings and a B=seperate string. I want to count the number of occurences in from B in A.
Example :
A:
10001
10011
11000
10010
10101
B:
10001
result would be 3.(10001 is a subset of 10001,10011,10101)
So i need a function that takes a set and string and returns an int.
int myfunc(set<string> , string){
int result;
// My Brain is melting
return result ;
}
edit : 00000 shouldn't be a subset of anything !
If you have control over the input, and these strings are really supposed to represent bitmasks, then you probably want to keep them as integers of some sort and use bitmasks as suggested by others. Otherwise, if your stuck with dealing with them as strings, and you're going to use the same set of strings to search through multiple times, you're still better off converting them to integral bitmasks.
If, however, the set of strings is only being processed once, you're better off just going through the set and manually checking each once. Offhand, something like this:
int myfunc(set<string> in, string search){
assert(search.length() <= 32);
int result = 0;
for(set<string>::iterator iIn = in.begin(); iIn != in.end(); ++iIn)
{
bool isSubset = true;
if (iIn->length() != search.length()) // Is this guaranteed?
isSubset = false;
for (int iSearch = 0; isSubset && iSearch < search.length; ++iSearch)
if (search[iSearch] == '1' && (*iIn)[iSearch] == '0')
isSubset = false;
if (isSubset)
++result;
}
return result;
}
Or else the convert to long first version:
int myfunc(set<string> in, string search){
int result = 0;
long searchInteger = strtol(search.c_str(), NULL, 2);
for(set<string>::iterator iIn = in.begin(); iIn != in.end(); ++iIn)
if ((strtol(iIn->c_str(), NULL, 2) & searchInteger) == searchInteger)
++result;
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With