如何比较同一对象的两个向量并获得其他元素
How to compare two vectors of a same object and get other elements
我想比较两个对象向量元素,并相应地获得同一对象的其他向量元素。例如,对象有一个向量;foo1.a=[4 2 1 3] foo2.a=[2 1 4]
根据从foo.a
得到的结果,我想找到相同的元素,然后得到相应的其他向量包含,如foo1.b=[8 8 2 10]
和foo2.b=[8 2 8]
。我试图比较循环中的两个向量,然后得到相同的向量,但我失败了。
给定两个向量:
std::vector<int> v1; // {4, 2, 1, 3};
std::vector<int> v2; // {2, 1, 4};
首先,sort
两个矢量,这样就很容易找到共同的元素:
std::sort(v1); // {1, 2, 3, 4}
std::sort(v2); // {1, 2, 4}
使用set_intersection
查找常见元素:
std::vector<int> vi;
std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vi.begin()); // {1, 2, 4}
使用set_difference
查找唯一元素:
std::vector<int> vd;
std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vd.begin()); // {3}
相关文章:
- 在C++中查找两个向量之间最相似的值
- 如何在C++中从两个向量生成所有可能的对?
- 返回两个向量 – 使用引用还是元组?
- 检查两个向量是否并行的最有效方法
- 如何获得比较两个向量对的子集
- 如何在 c++ 中对两个向量进行线性搜索?
- 如何在 c++ 中从两个向量创建 JSON 对象?
- C++如何同时删除位于两个向量中的 2 个指针?
- 如何通过 stl 容器和算法库计算两个向量的内积?
- 删除两个向量中的重复项
- 两个向量在犰狳中相等?
- 为什么我无法比较自定义类类型的两个向量?
- 通过块比较两个向量时,如何避免重复
- Boost.Test - 如何在交叉点上检查两个向量
- 通过移动从两个向量创建元组向量
- 如何在C 中合并两个向量
- R- armadillo c :用其他两个向量对向量进行排序
- 使用嵌套循环搜索两个向量并查看其属性
- 如何以精度换取速度来评估C++中两个向量的点积符号?(不特定于硬件)
- 我该如何XOR这两个向量的内容