[英]Find difference between two lists using Eigen
I would like to find the difference between two lists. 我想找到两个列表之间的区别。 For example: 例如:
// two lists:
A = [ 0, 1, 2, 3, 4, 5, 6 ];
B = [ 1, 4, 5 ];
// difference between the lists:
C = [ 0, 2, 3, 6 ];
I have done this using the STL-library of C++ as follows: 我使用C ++的STL库完成了此操作,如下所示:
#include <iostream>
#include <vector>
int main()
{
std::vector<size_t> A = {0, 1, 2, 3, 4, 5, 6};
std::vector<size_t> B = {1, 4, 5};
std::vector<size_t> C;
std::set_difference(A.begin(),A.end(), B.begin(),B.end(), std::inserter(C,C.begin()));
return 0;
}
However, because my application uses mostly Eigen, I now would like to do also this using Eigen. 但是,由于我的应用程序主要使用Eigen,因此我现在也想使用Eigen进行此操作。 I couldn't find what I was looking for in the documentation nor online. 我在文档或在线中找不到我想要的东西。
Note that I specifically want to avoid writing my own function. 请注意,我特别想避免编写自己的函数。
Here you go: 干得好:
#include <iostream>
#include <Eigen/Dense>
int main()
{
using namespace Eigen;
VectorXd a(3), b(1);
VectorXd c(a.size());
a << 1,2,3;
b << 1;
auto it = std::set_difference(a.data(), a.data() + a.size(),
b.data(), b.data() + b.size(),
c.data());
c.conservativeResize(std::distance(c.data(), it)); // resize the result
std::cout << c;
}
The key here is to use Eigen::VectorXd::data()
member function, which returns a pointer to the underlying storage, which is itself an iterator that can be passed around to C++ standard library functions. 此处的关键是使用Eigen::VectorXd::data()
成员函数,该成员函数返回指向基础存储的指针,该指针本身是可以传递给C ++标准库函数的迭代器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.