有效的方式来检查给定的字符串是否等同于给定的字符串集合中的至少一个字符串 [英] Efficient way to check if a given string is equivalent to at least one string in the given set of strings
本文介绍了有效的方式来检查给定的字符串是否等同于给定的字符串集合中的至少一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一组字符串,例如 true
或 false
)是否给予 string s
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?
推荐答案
可以使用Boost.Regex来执行此任务吗?方案
解决方案
std :: unordered_set
将提供最有效的查找(摊销的常量时间)。
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;
}
这篇关于有效的方式来检查给定的字符串是否等同于给定的字符串集合中的至少一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文