How to check whether a vector is a subset of another in c++

后端 未结 4 1715

I am trying to find a simple way to check whether a vector is a subset of another without sorting the order of elements in the vector. Both the vectors contain random number

4条回答
  •  滥情空心
    2021-01-01 23:20

    Copy the vectors. Sort the copies. Then use std::includes on the copies.

    template 
    bool IsSubset(std::vector A, std::vector B)
    {
        std::sort(A.begin(), A.end());
        std::sort(B.begin(), B.end());
        return std::includes(A.begin(), A.end(), B.begin(), B.end());
    }
    

提交回复
热议问题