在C++中访问boost多数组的维度

问题描述 投票:0回答:1

当我使用警告标志运行以下命令时,我收到类型转换警告。

#include <boost/multi_array.hpp>

void function (boost::multi_array<unsigned char, 2> matrix) {
  int nrows = matrix.shape()[0];
  int ncols = matrix.shape()[1];
}

请参阅下面的警告消息。这是否意味着我隐式地将“long unsigned int”转换为常规“int”?

如果是这样,我想这就是我想要的(之后需要用nrows、ncols进行计算),那么我该如何使转换显式化呢?

image.cpp:93:32: warning: conversion to ‘int’ from ‘boost::const_multi_array_ref<float, 2ul, float*>::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
     int nrows = matrix.shape()[0];
c++ boost boost-multi-array
1个回答
2
投票

这是否意味着我隐式地将“long unsigned int”转换为常规“int”?

是的,就是这个意思。

如果您不想要警告,则不要将

nrows
ncols
设为
int
类型。最简单的事情就是让编译器推断出类型,即

auto nrows = matrix.shape()[0];
auto ncols = matrix.shape()[1];

或者您可以将它们设为

size_t
类型,这是标准库用于容器大小的类型,并且不会发出警告。

© www.soinside.com 2019 - 2024. All rights reserved.