This code will return false
if the long double rand
begins with any string in the vector prefix
.
bool prefix_vect(long double rand, std::vector<std::string> prefix) {
std::string str_rand = std::to_string(rand);
for(auto i : prefix) {
if(boost::starts_with(str_rand, i))
return false;
}
return true;
}
This code uses #include <boost/algorithm/string/predicate.hpp>
.
How can I improve this code? Is there a way to make it fit into one line, such that it could be used without a function at all, and instead in an if-statement?
I am not experienced with C++ lambda expressions, is this something that could use them?
As a note: I don't care if it returns true or false, the Boolean can be switched if it makes this easier to optimize.
Edit: This isn't my main account, just a throwaway (unable to merge accounts, sadly).
--prefix 0.1 0.3
, and in the end it would print only the numbers beginning with 0.1 and 0.3 -- doing so by verifying it through this function. The numbers 0.1 and 0.3 are stored in a vector of strings, and the randomly generated numbers are long doubles, thus they need to be converted to a string to be compared easily. \$\endgroup\$