STL vector, search for a sequence
-
Hi guys, I have two vectors of string and I'm looking for an easy way to find the first vector sequence or sub-sequence into the first one for example: Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "C++", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 3 and match length 2 Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "JAVA", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 0 and match length 1 thanks for any idea, suggestion
-
Hi guys, I have two vectors of string and I'm looking for an easy way to find the first vector sequence or sub-sequence into the first one for example: Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "C++", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 3 and match length 2 Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "JAVA", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 0 and match length 1 thanks for any idea, suggestion
The simplest algorithm is probably this:
std::pair<size_t, size_t> find_first_subsequence(std::vectorstd::string const& v1,
std::vectorstd::string const& v2)
{
std::vectorstd::string::const_iterator pos = std::find(v1.begin(), v1.end(), v2[0]);
const size_t firstPos = pos-v1.begin();
if (pos == v1.end()) return std::make_pair(-1, -1);
size_t numEqual = 0;
while(numEqual < v2.size() && pos != v1.end() && *pos == v2[numEqual])
{
++numEqual;
++pos;
}
return std::make_pair(firstPos, numEqual);
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Hi guys, I have two vectors of string and I'm looking for an easy way to find the first vector sequence or sub-sequence into the first one for example: Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "C++", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 3 and match length 2 Vect_1 < "JAVA", "ADA", "C", "C++", "C#"> Vect_2 < "JAVA", "C#"> search vect_2 sequence in vect_1 will return vect_1 position 0 and match length 1 thanks for any idea, suggestion
Why not use std::search[^]?
Steve
-
Why not use std::search[^]?
Steve
Because
std::search
only returns a match if it finds the whole of a sequence you're looking for, not a partial match, which is what the OP was looking for.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Because
std::search
only returns a match if it finds the whole of a sequence you're looking for, not a partial match, which is what the OP was looking for.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Fair point. I didn't read the OP's post carefully enough.
Steve