使用accumulate方法但不能第一个元素总是显示为0
Using accumulate method but can't the first element is always shown as 0
我正在尝试编写一个程序,其中 returns 一个列表,其第 n 个元素是传输列表的前 n 个值的总和。
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);
}
输出为:0,1,3,6,10
我认为问题是,第一个循环是 accumulate(liste.begin(), liste.begin(), 0);它应该始终为 0。但是我不知道如何获取第一个元素(尽管它只是一个元素,所以不需要累积)。
我想要以下输出:1,3,6,10,15.
您使用了错误的算法。对于此任务,已经存在在 header <numeric>
.
中声明的适当算法 std::partial_sum
这是一个演示程序。
#include <iostream>
#include <list>
#include <iterator>
#include <numeric>
std::list<int> summe( const std::list<int> &liste )
{
std::list<int> neueListe;
std::partial_sum( std::begin( liste ), std::end( liste ),
std::back_inserter( neueListe ) );
return neueListe;
}
int main()
{
std::list<int> l = { 1, 2, 3, 4, 5 };
auto l2 = summe( l );
for ( const auto &item : l2 )
{
std::cout << item << ' ';
}
std::cout << '\n';
return 0;
}
程序输出为
1 3 6 10 15
或者您可以将算法的实现转移到您的函数中。为此,有一个循环就足够了。
我对 summe()
函数的实现在循环中使用 integer 值来迭代 liste,我将其添加到它的length 1
单元和内部 accumulate
方法的值,我使用 begin(liste)
迭代器作为参数,下一次迭代 next(begin(liste), i)
.
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 << "{ " ;
for (int i=1; i < liste.size()+1; i++) {
sum = accumulate(begin(liste), next(begin(liste), i), 0);
neueListe.push_back(sum);
cout << sum << " ";
n++;
}
cout << " }";
return neueListe;
}
输出:
1 3 6 10 15
添加二元运算符
如果您想将值相乘,您需要创建一个函数作为附加参数来调用 accumulate()
:
int mult_op(int x, int y) {return x*y;}
// use 1 otherwise return a list filled with 0's
accumulate(begin(liste), next(begin(liste), i), 1, mult_op);
1 2 6 24 120
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.
您已经看到此函数已存在于 <numeric>
中,因此您不必编写它。但无论如何我都会回答这个具体问题,这样你就可以从中学习,更一般地说。
原始片段
for (itr = liste.begin(); itr != liste.end(); itr++) {
sum = accumulate(liste.begin(), itr, 0);
neueListe.push_back(sum);
cout << sum << endl;
n++;
}
重写
int sum {0};
for (const auto x : liste) { // use ranged for loop !!!!!
sum += x;
neueListe.push_back(sum);
}
各种 cout
语句用于帮助您了解正在发生的事情,不属于最终代码。相反,您可以单独打印列表(或使用它做任何您需要的事情)。
值 n
似乎没有任何意义,因为它增加了但从未在任何地方引用。
我正在尝试编写一个程序,其中 returns 一个列表,其第 n 个元素是传输列表的前 n 个值的总和。
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);
}
输出为:0,1,3,6,10 我认为问题是,第一个循环是 accumulate(liste.begin(), liste.begin(), 0);它应该始终为 0。但是我不知道如何获取第一个元素(尽管它只是一个元素,所以不需要累积)。 我想要以下输出:1,3,6,10,15.
您使用了错误的算法。对于此任务,已经存在在 header <numeric>
.
std::partial_sum
这是一个演示程序。
#include <iostream>
#include <list>
#include <iterator>
#include <numeric>
std::list<int> summe( const std::list<int> &liste )
{
std::list<int> neueListe;
std::partial_sum( std::begin( liste ), std::end( liste ),
std::back_inserter( neueListe ) );
return neueListe;
}
int main()
{
std::list<int> l = { 1, 2, 3, 4, 5 };
auto l2 = summe( l );
for ( const auto &item : l2 )
{
std::cout << item << ' ';
}
std::cout << '\n';
return 0;
}
程序输出为
1 3 6 10 15
或者您可以将算法的实现转移到您的函数中。为此,有一个循环就足够了。
我对 summe()
函数的实现在循环中使用 integer 值来迭代 liste,我将其添加到它的length 1
单元和内部 accumulate
方法的值,我使用 begin(liste)
迭代器作为参数,下一次迭代 next(begin(liste), i)
.
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 << "{ " ;
for (int i=1; i < liste.size()+1; i++) {
sum = accumulate(begin(liste), next(begin(liste), i), 0);
neueListe.push_back(sum);
cout << sum << " ";
n++;
}
cout << " }";
return neueListe;
}
输出:
1 3 6 10 15
添加二元运算符
如果您想将值相乘,您需要创建一个函数作为附加参数来调用 accumulate()
:
int mult_op(int x, int y) {return x*y;}
// use 1 otherwise return a list filled with 0's
accumulate(begin(liste), next(begin(liste), i), 1, mult_op);
1 2 6 24 120
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.
您已经看到此函数已存在于 <numeric>
中,因此您不必编写它。但无论如何我都会回答这个具体问题,这样你就可以从中学习,更一般地说。
原始片段
for (itr = liste.begin(); itr != liste.end(); itr++) {
sum = accumulate(liste.begin(), itr, 0);
neueListe.push_back(sum);
cout << sum << endl;
n++;
}
重写
int sum {0};
for (const auto x : liste) { // use ranged for loop !!!!!
sum += x;
neueListe.push_back(sum);
}
各种 cout
语句用于帮助您了解正在发生的事情,不属于最终代码。相反,您可以单独打印列表(或使用它做任何您需要的事情)。
值 n
似乎没有任何意义,因为它增加了但从未在任何地方引用。