Questions tagged [swap]

Changing position of two items.

Act of rearranging the locations of two items such that the first item now occupies the place of the second and the second item now occupies the place of the first.

2503 questions
436
votes
8 answers

Is there a standardized method to swap two variables in Python?

In Python, I've seen two variable values swapped using this syntax: left, right = right, left Is this considered the standard way to swap two variable values or is there some other means by which two variables are by convention most usually…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
224
votes
22 answers

How to swap two variables in JavaScript

I have this two variables: var a = 1, b = 2; My question is how to swap them? Only this variables, not any objects.
Lukas
  • 7,384
  • 20
  • 72
  • 127
187
votes
22 answers

Is there a native jQuery function to switch elements?

Can I easily swap two elements with jQuery? I'm looking to do this with one line if possible. I have a select element and I have two buttons to move up or down the options, and I already have the selected and the destination selectors in place, I do…
juan
  • 80,295
  • 52
  • 162
  • 195
170
votes
8 answers

Understand Python swapping: why is a, b = b, a not always equivalent to b, a = a, b?

As we all know, the pythonic way to swap the values of two items a and b is a, b = b, a and it should be equivalent to b, a = a, b However, today when I was working on some code, I accidentally found that the following two swaps give different…
Shaun Han
  • 2,676
  • 2
  • 9
  • 29
164
votes
2 answers

What can I do with a moved-from object?

Does the standard define precisely what I can do with an object once it has been moved from? I used to think that all you can do with a moved-from object is do destruct it, but that would not be sufficient. For example, take the function template…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
116
votes
30 answers

Swap two variables without using a temporary variable

I'd like to be able to swap two variables without the use of a temporary variable in C#. Can this be done? decimal startAngle = Convert.ToDecimal(159.9); decimal stopAngle = Convert.ToDecimal(355.87); // Swap each: // startAngle becomes:…
Sreedhar
  • 29,307
  • 34
  • 118
  • 188
114
votes
13 answers

How to convert big endian to little endian in C without using library functions?

I need to write a function to convert a big endian integer to a little endian integer in C. I cannot use any library function. How would I do this?
Alex Xander
  • 3,903
  • 14
  • 36
  • 43
100
votes
6 answers

Swap two items in List

Is there a LINQ way to swap the position of two items inside a List?
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
98
votes
3 answers

how to provide a swap function for my class?

What is the proper way to enable my swap in STL algorithms? 1) Member swap. Does std::swap use SFINAE trick to use the member swap. 2) Free standing swap in the same namespace. 3) Partial specialization of std::swap. 4) All of the above. Thank…
pic11
  • 14,267
  • 21
  • 83
  • 119
91
votes
20 answers

Is there a PHP function for swapping the values of two variables?

Say for instance I have ... $var1 = "ABC" $var2 = 123 and under certain conditions I want to swap the two around like so... $var1 = 123 $var2 = "ABC" Is there a PHP function for doing this rather than having to create a 3rd variable to hold one of…
Taylor
  • 1,700
  • 4
  • 17
  • 18
90
votes
8 answers

How to move specific item in array list to the first item

For example : A list A B C D E Given C , Switch to C A B D E Notice that the array size will change, some items may removed in run times Collections.swap(url, url.indexOf(itemToMove), 0); This statement is not working because it output C B A D E…
user782104
  • 13,233
  • 55
  • 172
  • 312
81
votes
2 answers

How does the standard library implement std::swap?

How is the swap function implemented in the STL? Is it as simple as this: template void swap(T& t1, T& t2) { T tmp(t1); t1=t2; t2=tmp; } In other posts, they talk about specializing this function for your own class. Why…
Maximilian Mordig
  • 1,333
  • 1
  • 12
  • 16
79
votes
5 answers

C++ trying to swap values in a vector

This is my swap function: template void swap (t& x, t& y) { t temp = x; x = y; y = temp; return; } And this is my function (on a side note v stores strings) call to swap values but whenever I try to call using values in…
user782311
  • 911
  • 2
  • 9
  • 9
79
votes
13 answers

Efficient swapping of elements of an array in Java

I am wondering if there is a more efficient way of swapping two elements in an Array, than doing something like this: String temp = arr[1]; arr[1] = arr[2]; arr[2] = temp; Well, this is obviously not bad or even wrong, but I need to swap very often…
Robin
  • 3,512
  • 10
  • 39
  • 73
77
votes
4 answers

Why does swapping values with XOR fail when using this compound form?

I found this code to swap two numbers without using a third variable, using the XOR ^ operator. Code: int i = 25; int j = 36; j ^= i; i ^= j; j ^= i; Console.WriteLine("i:" + i + " j:" + j); //numbers Swapped correctly //Output: i:36…
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
1
2 3
99 100