简体   繁体   English

C ++检查列表是否包含子列表

[英]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?) 我知道这不难写,但如果它已经在某些lib中实现了,我就不会打扰了(也许Boost有这样的东西?)

Any sequence container will do. 任何序列容器都可以。 You just need to use std::search algorithm to do the search of the sublist: 您只需要使用std :: search算法来搜索子列表:

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. STL的mismatch算法对于通用容器来说非常strcmp

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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