Cześć, niby rozumiem jak działa inner_product ale nie udaje mi sie wykonać tego tak jak bym oczekiwał. O to mój kod, licze w nim wystąpienia literek od 0 do 9 i potem za pomocą inner product chce obliczyć "statystyke" wg podanego wzoru w funkcji, lecz niestety cały czas wynik to 0. Nie wiem dlaczego ....
#include<iostream>
#include<random>
#include<time.h>
#include<set>
#include<algorithm>
#include<numeric>
#include<cmath>
int dod(int x, int y){return x + y;}
int funkcja(int x, int y){return (x-y)^2/(x+y);}
int main()
{
using namespace std;
srand(time(NULL));
multiset<int> r; // bufor do zapisywania obserwacji
multiset<int> s;
multiset<int> R;
multiset<int> S;
for (int i = 0; i < 100; i++)
{
r.insert(rand() % 10); // dodaj wynik nowej obserwacji
s.insert(rand() % 10);
}
//multiset<int>::iterator i = r.begin();
//while (i != r.end())
//{
// cout << "Liczba: " << *i << endl;
// i++;
//}
//cout << "-----------------------------------" << endl;
//multiset<int>::iterator l = s.begin();
//while (l != s.end())
//{
// cout << "Liczba: " << *l << endl;
// l++;
//}
typedef multiset<int>::iterator init;
//init it = r.begin();
//while (it != r.end())
//{
// pair<init, init> p = r.equal_range(*it);
// int count = distance(p.first, p.second);
// R.insert(count);
// cout << *it << ": " << count << endl;
// it = p.second;
//}
//cout << endl;
//init itt = s.begin();
//while (itt != s.end())
//{
// pair<init, init> o = s.equal_range(*itt);
// int count = distance(o.first, o.second);
// S.insert(count);
// cout << *itt << ": " << count << endl;
// itt = o.second;
//}
/////////////////////////////////////////
init it = r.begin();
init itt = s.begin();
while (it != r.end() && itt != s.end())
{
pair<init, init> p = r.equal_range(*it);
pair<init, init> o = s.equal_range(*itt);
int count = distance(p.first, p.second);
R.insert(count);
cout << *it << "R: " << count << endl;
int countt = distance(o.first, o.second);
S.insert(countt);
cout << *itt << "S: " << countt << endl;
cout << endl;
it = p.second;
itt = o.second;
}
init iR = R.begin();
init iRR = R.end();
init iS = S.begin();
int p = inner_product(iR,iRR,iS,0,dod,funkcja);
cout << endl<<"------------ :"<< p<<endl;
return 0;
}