The following piece of code does not compile:
#include <valarray>
int main()
{
std::valarray<std::valarray<int>> a;
std::valarray<std::valarray<int>> b;
//std::valarray<std::valarray<bool>> c;
std::valarray<bool> c;
c = (a == b);
return 0;
}
Neither of the declarations of c compile. Is it allowed to use valarray in the above demonstrated manner or are there restrictions on how it should be used.
I believe I have the solution , though I may be wrong.
Because begin
and end
are non-member overloads , you use begin(a)
instead of a.begin()
and so on.
My choice in using std::transform
is that you want to iterate over a
and b
, compare them, and store the results in c
, which is why the lambda returns a std::valarray<bool>
(instead of a bool
).
std::valarray<std::valarray<int>> a;
std::valarray<std::valarray<int>> b;
std::valarray<std::valarray<bool>> c;
std::transform(begin(a), end(a), begin(b), begin(c),
[&] (std::valarray<int> a_val, std::valarray<int> b_val)
-> std::valarray<bool> {
return std::valarray<bool>(a_val == b_val);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.