如何判断一个字符串是否包含另一个字符串
How to decide if one string contains another string
我有一组字符串,然后是第二个字符串。我想遍历这个集合并确定当前字符串是否包含在第二个字符串中。STL
或这些包含的工具中有什么工具可以帮助我解决这个问题吗?
#include <cctype>
#include <iostream>
#include <iomanip>
#include <set>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <string>
Usecase:
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
与boost::lambda
传递给std::for_each
,将结果与std::string::npos
进行比较并计数
您可以在这里找到相关示例
另一种更直接的方法是创建自己的函函数,它将在构造函数中接受string,在operator()(const std::string&)
中搜索子字符串并计数npos'
可以使用find_if.
希望对大家有所帮助
相关文章:
- C++-字符串是否包含一个带有简单循环的单词
- 将常量字符串添加到非常量字符串是否会给出常量字符串
- 检查字符串是否"null" C++
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 检查字符串是否为整数
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 如何检查字符串是否正确以将其转换为 int?
- 程序检查字符串是否只包含 1 和 0?
- 检查字符串是否是拉平德罗姆
- 尝试使用 indexOf 创建一个 if 语句来检查字符串是否包含字符.有一点麻烦
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- gtest:检查字符串是否等于两个字符串之一
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- 如何检查字符串是否以C++中的"ed"结尾;
- 字符串是否在其构造函数中复制数据
- 检查字符串是否是数字,然后将该数字转换为 int?
- 如何检查一个字符串是否包含多个其他字符串?
- 检查字符串是否仅包含字母
- 如何根据一个特定字母的出现来确定两个字符串是否相等?
- 查找字符串是否包含字符串向量的任何一个元素的最佳方法