在这里,我试图对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;
}发布于 2020-10-19 16:16:19
问题是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;
}在这种情况下,如果您想要比auto更详细,也可以将idx类型化为xt::xarray<size_t>或xt::xtensor<size_t, 2>。
https://stackoverflow.com/questions/64334276
复制相似问题