C++ trying to swap values in a vector

I think what you are looking for is iter_swap which you can find also in <algorithm>. all you need to do is just pass two iterators each pointing at one of the elements you want to exchange. since you have the position of the two elements, you can do something like this: // assuming your … Read more

sql swap primary key values

Let’s for the sake of simplicity assume you have two records id name ——— 1 john id name ——— 2 jim both from table t (but they can come from different tables) You could do UPDATE t, t as t2 SET t.id = t2.id, t2.id = t.id WHERE t.id = 1 AND t2.id = 2 … Read more

Python Array Rotation

You can rotate a list in place in Python by using a deque: >>> from collections import deque >>> d=deque([1,2,3,4,5]) >>> d deque([1, 2, 3, 4, 5]) >>> d.rotate(2) >>> d deque([4, 5, 1, 2, 3]) >>> d.rotate(-2) >>> d deque([1, 2, 3, 4, 5]) Or with list slices: >>> li=[1,2,3,4,5] >>> li[2:]+li[:2] [3, 4, … Read more

Does C++11 change the behavior of explicitly calling std::swap to ensure ADL-located swap’s are found, like boost::swap?

I would have had to vote against your proof-of-concept implementation had it been proposed. I fear it would break the following code, which I’m pretty sure I’ve seen in the wild at least once or twice over the past dozen years. namespace oops { struct foo { foo() : i(0) {} int i; void swap(foo& … Read more

iter_swap() versus swap() — what’s the difference?

The standard itself has very few mentions of iter_swap: It should have the effect of swap(*a, *b), although there is no stipulation that it must be implemented that way. The dereferenced values *a and *b must be “swappable”, which implies that swap(*a, *b) must be valid, and thus the dereferenced types must be identical, although … Read more