Data structure for matching sets

前端 未结 13 1130
有刺的猬
有刺的猬 2021-02-02 00:14

I have an application where I have a number of sets. A set might be
{4, 7, 12, 18}
unique numbers and all less than 50.

I then have several data items:
1 {1,

13条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 00:47

    I'm surprised no one has mentioned that the STL contains an algorithm to handle this sort of thing for you. Hence, you should use includes. As it describes it performs at most 2*(N+M)-1 comparisons for a worst case performance of O(M+N).

    Hence:

    bool isContained = includes( myVector.begin(), myVector.end(), another.begin(), another.end() );
    

    if you're needing O( log N ) time, I'll have to yield to the other responders.

提交回复
热议问题