gpt4 book ai didi

c++ - 在 Eigen 库 vector 上使用 std::upper_bound 时出错

转载 作者:行者123 更新时间:2023-11-28 01:24:17 26 4
gpt4 key购买 nike

我正在尝试将 std::upper_bound 与 Eigen 库定义的 vector 一起使用。我在 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

最佳答案

直到版本 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 返回一个迭代器,所以你必须取消引用才能得到值:

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

关于c++ - 在 Eigen 库 vector 上使用 std::upper_bound 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54682860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com