2013-04-16 179 views
0

我有一组字符串,然后是一些第二个字符串。我想遍历该集合并确定当前字符串是否包含在第二个字符串中。有没有从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 

当然,我可以通过字符字符串的字符串,性格比较它还是创造了一些自动的,但为什么做事情更难,比他们实际上可以是:)

+1

['的std :: string :: find()方法'](http://en.cppreference.com/w/cpp/string/basic_string/find)? – hmjd

回答

2

您可以使用std::string::find进行搜索,并使用boost::bind将其传递到std::for_eachboost::lambda以将结果与std::string::npos进行比较并将其计数为
您可以找到相关的例子Here
另一种更简单的方法是创建自己的仿函数,将采取字符串的构造,在operator()(const std::string&)搜索字符串和计数npos'es

+0

你不介意写一个简短的例子,boost :: bind'是如何工作的? – Dworza

+2

或std :: bind。 。 。甚至是一个lambda – learnvst

+0

是的,我仍然有过时的03脑:) – alexrider

1

可以使用find_if

我希望这有助于

相关问题