我有一组字符串,然后是第二组字符串。我想遍历该集合并确定当前字符串是否包含在第二个字符串中。 STL
或其中的任何工具是否有任何工具可以让我轻松工作?
#include <cctype>
#include <iostream>
#include <iomanip>
#include <set>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <string>
用例:
test1 . Add ( 0, "hello" );
test1 . Add ( 1, "world" );
test1 . Add ( 2, "rld" );
test1 . Add ( 3, "ell" );
test1 . Add ( 4, "hell" );
printSet ( test1 . Search ( "hello world!" ) );
// 0, 1, 2, 3, 4
printSet ( test1 . Search ( "hEllo world!" ) );
// 1, 2
当然,我可以逐个字符串、逐个字符地比较它,或者创建一些自动机,但为什么要让事情变得比实际更难 :)
最佳答案
您可以使用 std::string::find
用于搜索,boost::bind
将其传递到 std::for_each
并使用 boost::lambda
将结果与 std 进行比较::string::npos
并计算它们
您可以找到相关示例 Here
另一种更直接的方法是创建自己的仿函数,它将在构造函数中接受字符串,在 operator()(const std::string&)
中搜索子字符串并计算 npos'es
关于c++ - 如何判断一个字符串是否包含另一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16038020/