gpt4 book ai didi

c++ - xtensor xt::where与索引相关的功能出现问题

转载 作者:行者123 更新时间:2023-12-02 10:26:22 25 4
gpt4 key购买 nike

在这里,我试图对C++中的xtensor库进行非常基本的操作。我有xarray a,并且有了index related function xt::where,我想获取条件为True的索引数组(请注意,还有另一个xt::where函数,但这是operator function,我不想要它) 。
当我尝试使用此行进行编译时,出现很多错误:

g++ -I/usr/include/xtensor -I/usr/local/include/xtl getindx.cpp -o getindx
奇怪的是,当我尝试使用另一个xt::where函数(运算符函数)时,它可以工作,编译并运行。我显然缺少了一些东西;我正在搜索,但无法通过,请帮助我!谢谢。
这是代码:
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"
#include "xtensor/xoperation.hpp"
#include "xtensor/xtensor.hpp"


using namespace std;

int main(int argc, char** argv){

xt::xarray<double> arr {5.0, 6.0, 7.0};
auto idx = xt::where(arr >= 6);

std::cout << idx << std::endl;

return 0;

}
编辑:错误。
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<std::vector<long unsigned int>, std::allocator<std::vector<long unsigned int> > >’)
std::cout << idx << std::endl;
EDIT2:没有xtensor解决。也许会慢一些。


int main(int argc, char** argv){


std::vector<double> arr{5.0,6.0,7.0};
std::vector<unsigned int> indices;

auto ptr = &bits[0];
for (int i = 0; i<arr.size(); i++, ptr++)
{
if (*ptr>=6) indices.push_back (i);
}

for (int i=0; i<indices.size(); i++){
cout << "indices= "indices[i] << endl;
} //output: indices=1, indices=2.
return 0;
}

最佳答案

问题是xt::where(或语法xt::argwhere可能在这里更具描述性)返回的std::vector或数组索引没有operator<<重载,例如用于打印。
为了处理这个xt::from_indices被创建。从the relevant docs page:

int main()
{
xt::xarray<size_t> a = xt::arange<size_t>(3 * 4);

a.reshape({3,4});

auto idx = xt::from_indices(xt::argwhere(a >= 6));

std::cout << idx << std::endl;
}
在这种情况下,如果您想使用 idx更详细些,也可以将 xt::xarray<size_t>键入 xt::xtensor<size_t, 2>auto

关于c++ - xtensor xt::where与索引相关的功能出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64334276/

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