如何检查向量是否是c ++中另一个的子集 [英] How to check whether a vector is a subset of another in c++

查看:134
本文介绍了如何检查向量是否是c ++中另一个的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个简单的方法来检查一个向量是否是另一个的子集,而不排序向量中元素的顺序。这两个向量包含随机数元素。

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 elements in them.

std :: includes 似乎只适用于排序范围。如何实现这一点?

std::includes seems to work only for sorted ranges. How can I accomplish this?

推荐答案

复制向量。排序副本。然后在副本上使用 std :: includes

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

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

这篇关于如何检查向量是否是c ++中另一个的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆