c++ - 如何比较两个动态数组并检查哪些元素匹配?

标签 c++ eigen

我想按元素比较两个矩阵并返回一个包含 1 的矩阵如果该位置的元素匹配,或者 0否则。我创建了一个简单的测试函数来执行此操作:

template <class A, class B>
void func(const A& a, const B& b)
{
    auto c = (b.array() == a.array()).cast<int>();
    std::cout << c;
}

所以我写了一个main像这样的功能来测试它:

int main()
{
    Eigen::Array<int,Eigen::Dynamic,Eigen::Dynamic> b;

    b.resize(2,2);
    b.fill(2);

    auto a = b;
    a(0,0) = 1;
    a(0,1) = 2;
    a(1,0) = 3;
    a(1,1) = 4;

    func(a,b);
    return 0;
}

但我一直收到这个错误:
eigenOperations.cpp: In function ‘void func(const A&, const B&)’: eigenOperations.cpp:8:24: error: expected primary-expression before ‘int’ auto c = temp.cast<int>(); eigenOperations.cpp:8:24: error: expected ‘,’ or ‘;’ before ‘int’ make: *** [eigenOperations] Error 1

我在这里做错了什么?

最佳答案

将其标记为重复的人是对的。问题确实出现了,因为我没有完全理解 C++ template 关键字。

Where and why do I have to put the "template" and "typename" keywords?

将函数替换为以下修复了问题:

template <class A, class B>
void func(const A& a, const B& b)
{
    auto c = (b.array() == a.array()).template cast<int>();
    std::cout << c;
}

关于c++ - 如何比较两个动态数组并检查哪些元素匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24624671/

相关文章:

c++ - 使用 Media Foundation 制作视频

c++ - 在 C++ 中实现这种工作流程的最佳方式?

c++ - 连接稀疏矩阵特征

c++ - 将 Matrix::row() 的返回分配给 Matrix<X,1,Dynamic>

android - Android NDK 上的谷歌测试未定义对 'typeinfo for testing::Test' 的引用

c++ - 包括写在不同文件夹中的函数

c++ - 特征矩阵的下三角

c++ - Eigen 中填充稀疏矩阵的速度取决于节点数或边数?

c++ - Eigen 中的稀疏矩阵

c++ - Windows 服务程序中退出函数的作用