在 Eigen 库向量上使用 std::upper_bound 时出错

Get an error using std::upper_bound on an Eigen library vector

我正在尝试将 std::upper_bound 与 Eigen 库定义的向量一起使用。我在 visual studio 2017

收到一些错误
#include <stdafx.h>
#include <stdio.h>
#include <Eigen/Dense>
#include <algorithm>  

using namespace Eigen;
using namespace std;

int main(int argc, char* argv[])
{
    VectorXd myVector= VectorXd::LinSpaced(20, -1.5, 6.4);

    double trashold= strtod(argv[1], NULL);

    double firstGreaterValue= std::upper_bound(myVector(0), 
    myVector(myVector.size()-1),trashold);
}

我希望程序 return 第一个元素的位置大于我的输入(在本例中为 1.1)。

我在编译时遇到这些错误

Severity    Code    Description Project File    Line    Suppression State
Error   C2794   'difference_type': is not a member of any direct or indirect base class of 'std::iterator_traits<_FwdIt>'   errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2347    
Error   C2938   '_Iter_diff_t<double>' : Failed to specialize alias template    errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2347    
Error   C2672   'std::distance': no matching overloaded function found  errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2347    
Error   C2893   Failed to specialize function template 'iterator_traits<_Iter>::difference_type std::distance(_InIt,_InIt)' errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2347    
Error   C2794   'difference_type': is not a member of any direct or indirect base class of 'std::iterator_traits<_FwdIt>'   errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2351    
Error   C2938   '_Iter_diff_t<double>' : Failed to specialize alias template    errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2351    
Error   C2672   'std::advance': no matching overloaded function found   errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2353    
Error   C2893   Failed to specialize function template 'void std::advance(_InIt &,_Diff)'   errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2353    
Error   C2100   illegal indirection errorExample    C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.10.25017\include\algorithm   2355    

在版本 3.4 之前,Eigen::VectorXd 与 STL 算法不兼容,因为它不提供访问其元素的迭代器。

从3.4版本开始,Eigen's dense matrices and arrays provide STL compatible iterators,可以使用begin()/end()方法获取迭代器:

auto firstGreaterValueIter = std::upper_bound(myVector.begin(), myVector.end(), trashold);

另外不要忘记 std::upper_bound returns 一个迭代器,所以你必须取消引用才能得到值:

if (firstGreaterValueIter != myVector.end()) {
    double firstGreaterValue = *firstGreaterValueIter;
    // ...
}