Write a function reverseDoubles which takes a pointer to a list of doubles and the length of the list and reverses it. So (1, 2, 3, 4) should be changed to (4, 3, 2, 1), for example. Write a test for this function。
这个是要求反转链表中的double类型的元素,实际上采用reverse更方便,也存在其他更高级的一些写法。下面代码仅供参考:
list<double> reverseDoubles(list<double>& l, int length) {
/* Write a function reverseDoubles which takes a pointer to a list of
doubles and the length of the list and reverses it. So (1, 2, 3, 4) should be
changed to (4, 3, 2, 1), for example. Write a test for this function.
其实list存在函数reverse可以直接实现这个需求,不需要重复写函数
*/
list<double> l2;
for (auto v : l) {
l2.push_front(v);
}
return l2;
}
void testReverseDoubles() {
list<double> l = { 1.0,2.0,3.0,4.0 };
list<double> l2 = reverseDoubles(l, 4);
list<double> l3 = { 4.0,3.0,2.0,1.0 };
ASSERT(l2 == l3);
}
系统当前共有 481 篇文章