I am trying to write a programm which returns a list whose n-th element is the sum of the first n Values of the transferred list.
list<int> summe(list<int> liste) {
list<int> neueListe;
list<int>::iterator itr;
itr = liste.begin();
int sum = 0;
int n = 0;
cout << "Liste mit Summen: " << endl;
cout << "{ " << endl;
for (itr = liste.begin(); itr != liste.end(); itr++) {
sum = accumulate(liste.begin(), itr, 0);
neueListe.push_back(sum);
cout << sum << endl;
n++;
}
cout << " }";
return neueListe;
}
int main() {
//Aufgabe 2.2 Teil 2
list<int> l = { 1,2,3,4,5 };
Algo a;
a.summe(l);
}
The output is: 0,1,3,6,10 I think the problem is, that the first loop is accumulate(liste.begin(), liste.begin(), 0); which should be always 0. However I have no idea how to get the first element (although its just one element so theres no need to accumulate). I want the following output: 1,3,6,10,15.