[英]C++ Boost valarray
I am looking for a convenient and optimized way to compare 2 valarrays for equality. 我正在寻找一种方便和优化的方法来比较2个valarray的相等性。 I've seen that Boost somewhat supports that:
我已经看到Boost有点支持:
In /boost/accumulators/numeric/functional/valarray.hpp -- 在/boost/accumulators/numeric/functional/valarray.hpp中 -
// for "promoting" a std::valarray<bool> to a bool, useful for
// comparing 2 valarrays for equality:
// if(numeric::promote<bool>(a == b))
template<typename From>
struct promote<bool, From, void, std_valarray_tag>
: std::unary_function<From, bool> {
bool operator ()(From &arr) const {
BOOST_MPL_ASSERT((is_same<bool, typename From::value_type>));
for(std::size_t i = 0, size = arr.size(); i != size; ++i) {
if(!arr[i]) {
return false;
}
}
return true;
}
};
The following trivial code runs with std::valarray
: 以下简单代码与
std::valarray
一起运行:
#include <valarray>
typedef std::valarray<int> valarray_t;
int main(void) {
int arr_length = 30; int num_of_idx = 5;
// initialize arr
int* arr = new int[arr_length];
for (int i=0; i<arr_length; i++)
arr[i] = i;
// Create a valarray of ints.
valarray_t vi(arr, arr_length);
std::valarray<bool> aaa = ( vi == vi );
}
How can I check whether aaa
is all true by using Boost? 如何使用Boost检查
aaa
是否全部正确?
Thanks! 谢谢!
The comment in your quoted code says exactly that: 引用代码中的注释恰恰说明了这一点:
bool allTrue = boost::numeric::promote<bool>(aaa);
Or just with C++11's standard library: 或者只是使用C ++ 11的标准库:
bool allTrue = std::all_of(begin(aaa), end(aaa), [](bool b){return b;});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.