在这里,我试图用 C++ 中的xtensor库做一个非常基本的操作。我有 xarray a
,并且使用与索引相关的函数 xt::where
,我想获取条件为 True 的索引数组(注意,还有另一个 xt::where 函数,但它是一个运算符函数,我不想要它)。当我尝试用这一行编译它时,我得到了很多错误:
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;
}