[英]Difference between reverse algorithm and list::reverse
std::list<int> l{1,2,3};
std::reverse(l.begin(), l.end()); // 1
l.reverse(); // 2
I guess I should prefer list::reverse()
. 我想我应该更喜欢list::reverse()
。 Just wondering if there is any difference between 1 and 2 in above code? 只是想知道上面的代码中1和2之间是否有区别? eg performance? 例如性能?
The difference is that list::reverse
can rearrange the list by changing the internal pointers between the nodes. 区别在于list::reverse
可以通过更改节点之间的内部指针来重新排列列表。 std::reverse
from <algorithm>
will move the values (which works even if the sequence is not a standard container). <algorithm>
std::reverse
将移动值(即使该序列不是标准容器也可以使用)。
In your specific case, using an int
data type, it isn't that obvious if it is a win to move two pointers instead of swapping two int
s. 在您的特定情况下,使用int
数据类型,移动两个指针而不是交换两个int
是个胜利,但这并不是很明显。 You might want to run some benchmarks if it is important. 如果重要的话,您可能需要运行一些基准测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.