我试图将std :: upper_bound与本征库定义的向量一起使用。我在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);
}
我希望程序返回的第一个元素的位置大于输入的位置(在本例中为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 Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\algorithm 2347
Error C2938 '_Iter_diff_t<double>' : Failed to specialize alias template errorExample C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\algorithm 2347
Error C2672 'std::distance': no matching overloaded function found errorExample C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.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 Studio\2017\Community\VC\Tools\MSVC\14.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 Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\algorithm 2351
Error C2938 '_Iter_diff_t<double>' : Failed to specialize alias template errorExample C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\algorithm 2351
Error C2672 'std::advance': no matching overloaded function found errorExample C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\algorithm 2353
Error C2893 Failed to specialize function template 'void std::advance(_InIt &,_Diff)' errorExample C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\algorithm 2353
Error C2100 illegal indirection errorExample C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\algorithm 2355
答案 0 :(得分:2)
在版本3.4之前,Eigen::VectorXd
与STL算法不兼容,因为它不提供迭代器来访问其元素。
从Eigen
's dense matrices and arrays provide STL compatible iterators版本3.4开始,您可以使用begin()
/ end()
方法来获取迭代器:
auto firstGreaterValueIter = std::upper_bound(myVector.begin(), myVector.end(), trashold);
也不要忘记std::upper_bound
返回一个迭代器,因此必须取消引用才能获取该值:
if (firstGreaterValueIter != myVector.end()) {
double firstGreaterValue = *firstGreaterValueIter;
// ...
}