简体   繁体   English

反向算法和list :: reverse之间的区别

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM