I have the following function which determines whether there is a word in a set that begins with a certain string:
bool Dictionary::prefixExists(string prefix) const
{
bool result = false;
if(_words.lower_bound(prefix) != _words.end())
{
string lboundResult = *_words.lower_bound(prefix);
if(lboundResult.size() >= prefix.size() && lboundResult[prefix.size() - 1] == prefix[prefix.size() - 1])
result = true;
}
return result;
}
|
In order to avoid preforming the lower_bound() method twice, I would like to combine the functionality of the line:
if(_words.lower_bound(prefix) != _words.end())
|
with the line:
string lboundResult = *_words.lower_bound(prefix);
|
Does anyone know how this might be done?
I tried:
...
string lboundResult;
if((lboundResult = *_words.lower_bound(prefix)) != _words.end())
...
|
but that didn't work.
Last edited on
Thanks Stewbond! That's brilliantly simple. :)