开发者

C++ check if a list contains a sublist

开发者 https://www.devze.com 2023-01-19 20:35 出处:网络
I need a contai开发者_JAVA技巧ner in which I can check if a sequence of elements are present or not. Same thing as substring matching, just for generic collections. I know it\'s not hard to write, but

I need a contai开发者_JAVA技巧ner in which I can check if a sequence of elements are present or not. Same thing as substring matching, just for generic collections. I know it's not hard to write, but if it's implemented in some lib already, I wouldn't bother (maybe Boost has something like this?)


Any sequence container will do. You just need to use std::search algorithm to do the search of the sublist:

vector<int> sequence = ...;
vecter<int> sublist = ...;

vector<int>::iterator pos = std::search(
    sequence.begin(), sequence.end(), 
    sublist.begin(), sublist.end());

if(pos == sequence.end()) 
    // not fount
else 
    // found at pos


Do you want std::search?


The mismatch algorithm from STL is pretty much strcmp for generic containers.

http://www.cplusplus.com/reference/algorithm/mismatch/

0

精彩评论

暂无评论...
验证码 换一张
取 消