我将如何解决这个问题?我想在字符串的所有位置搜索一个大字符串。
答案 0 :(得分:13)
使用std::string::find
。你可以这样做:
std::string::size_type start_pos = 0;
while( std::string::npos !=
( start_pos = mystring.find( my_sub_string, start_pos ) ) )
{
// do something with start_pos or store it in a container
++start_pos;
}
编辑:Doh!谢谢你的评论,Nawaz!更好?
答案 1 :(得分:13)
另外两个答案是正确的,但它们非常慢并且具有O(N ^ 2)复杂度。但是有Knuth-Morris-Pratt算法可以找到O(N)复杂度的所有子串。
修改强>
还有另一个算法,所谓的“Z函数”具有O(N)复杂度,但我找不到这个算法的英文源(也许是因为还有另一个更有名的东西同名 - Z- Riman的功能),所以只需将其代码放在这里并解释它的作用。
void calc_z (string &s, vector<int> & z)
{
int len = s.size();
z.resize (len);
int l = 0, r = 0;
for (int i=1; i<len; ++i)
if (z[i-l]+i <= r)
z[i] = z[i-l];
else
{
l = i;
if (i > r) r = i;
for (z[i] = r-i; r<len; ++r, ++z[i])
if (s[r] != s[z[i]])
break;
--r;
}
}
int main()
{
string main_string = "some string where we want to find substring or sub of string or just sub";
string substring = "sub";
string working_string = substring + main_string;
vector<int> z;
calc_z(working_string, z);
//after this z[i] is maximal length of prefix of working_string
//which is equal to string which starting from i-th position of
//working_string. So the positions where z[i] >= substring.size()
//are positions of substrings.
for(int i = substring.size(); i < working_string.size(); ++i)
if(z[i] >=substring.size())
cout << i - substring.size() << endl; //to get position in main_string
}
答案 2 :(得分:3)
我将添加完整性,std::search
可以使用另一种方法,类似于std::string::find
,不同之处在于您使用迭代器,例如:
std::string::iterator it(str.begin()), end(str.end());
std::string::iterator s_it(search_str.begin()), s_end(search_str.end());
it = std::search(it, end, s_it, s_end);
while(it != end)
{
// do something with this position..
// a tiny optimisation could be to buffer the result of the std::distance - heyho..
it = std::search(std::advance(it, std::distance(s_it, s_end)), end, s_it, s_end);
}
我发现此有时优于std::string::find
,尤其是如果您将字符串表示为vector<char>
。
答案 3 :(得分:2)
简单地use std::string::find()
返回找到子字符串的位置,或std::string::npos
(如果没有找到)。
Here是文档。
以下是本文档中的示例:
// string::find
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("There are two needles in this haystack with needles.");
string str2 ("needle");
size_t found;
// different member versions of find in the same order as above:
found=str.find(str2);
if (found!=string::npos)
cout << "first 'needle' found at: " << int(found) << endl;
found=str.find("needles are small",found+1,6);
if (found!=string::npos)
cout << "second 'needle' found at: " << int(found) << endl;
found=str.find("haystack");
if (found!=string::npos)
cout << "'haystack' also found at: " << int(found) << endl;
found=str.find('.');
if (found!=string::npos)
cout << "Period found at: " << int(found) << endl;
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
cout << str << endl;
return 0;
}