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

标签 c++ indexing conditional-statements where-clause xtensor

在这里,我试图对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/

相关文章:

c++ - 预期为 'double *',但参数的类型为 'double',并且函数的参数 2/3/4/5 的类型不兼容

c++ - 单列表插入到列表末尾

python - 从另一个数据框和列表创建数据框

arrays - 如何在 R 中提取 1x1 数组切片作为矩阵?

c++ - 使用 rundll32 运行 C++ DLL - 缺少条目

python - 使用列表列表进行 Numpy 索引

javascript - 从输入框获取值,根据一定条件改变url

javascript - Angular - 条件名称取决于变量值

php - 空($_POST) === FALSE 不能正常工作

c++ - 在 C++ 中输入大货币值