Given a set of strings, say "String1", "String2",..., "StringN"
, what is the most efficient way in C++ to determine (return true
or false
) whether given string s
matches any of the strings in the above set?
Can Boost.Regex be used for this task?
std::unordered_set
would provide the most efficient look-up (amortized constant time).
#include <unordered_set>
#include <string>
#include <cassert>
int main() {
std::unordered_set<std::string> s = {"Hello", "Goodbye", "Good morning"};
assert(s.find("Goodbye") != s.end());
assert(s.find("Good afternoon") == s.end());
return 0;
}
You can put all your strings in a std::set and then check if that string is in the set.
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