Reputation: 3437
I'm trying to use adjacent_difference with two different iterator types. The functor I have created takes the type used by the InputIterator as parameters and returns the type used by the OutputIterator. I don't understand why the code I included doesn't compile:
In file included from /usr/include/c++/4.9/numeric:62:0, from 4: /usr/include/c++/4.9/bits/stl_numeric.h: In instantiation of '_OutputIterator std::adjacent_difference(_InputIterator, _InputIterator, _OutputIterator, _BinaryOperation) [with _InputIterator = __gnu_cxx::__normal_iterator >; _OutputIterator = __gnu_cxx::__normal_iterator >; _BinaryOperation = {anonymous}::CheckOp]': 48:85: required from here /usr/include/c++/4.9/bits/stl_numeric.h:374:17: error: cannot convert '_ValueType {aka Test}' to 'float' in assignment *__result = __value;
// Example program
#include <iostream>
#include <string>
#include <numeric>
#include <vector>
struct Test
{
float first;
float second;
};
namespace{
class CheckOp {
public:
float operator()(Test x, Test y) const
{
float a = x.first - y.first;
float b = x.second - y.second;
return a + b;
}
};
}
int main()
{
std::vector<Test> testVec;
Test test1;
test1.first = 5.5F;
test1.second = 6.5F;
testVec.push_back(test1);
Test test2;
test2.first = 2.5F;
test2.second = 8.5F;
testVec.push_back(test2);
Test test3;
test3.first = 9.4F;
test3.second = 7.8F;
testVec.push_back(test3);
CheckOp checkOP;
std::vector<float> resultVec(testVec.size());
std::adjacent_difference(testVec.begin(), testVec.end(), resultVec.begin(), checkOP);
}
Upvotes: 1
Views: 435
Reputation: 72054
Note the description of adjacent_difference
(from cppreference):
First, creates an accumulator
acc
whose type isInputIt
's value type, initializes it with*first
, and assigns the result to*d_first
.
This implies that the input and output sequences must have the same, or at least a compatible, type.
Upvotes: 3