简体   繁体   中英

C++ check if a list contains a sublist

I need a container 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

你想要std::search吗?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM