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.

2196 questions
372
votes
7 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
  • 36,283
  • 61
  • 170
  • 271
255
votes
18 answers

How to find out which processes are using swap space in Linux?

Under Linux, how do I find out which process is using the swap space more?
Shameem
  • 13,659
  • 13
  • 38
  • 42
185
votes
20 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
  • 6,464
  • 16
  • 61
  • 114
182
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
  • 74,179
  • 49
  • 153
  • 188
145
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
  • 237,063
  • 85
  • 359
  • 638
96
votes
13 answers

convert big endian to little endian in C [without using provided func]

I need to write a function to convert big endian to little endian in C. I can not use any library function.
Alex Xander
  • 3,530
  • 12
  • 34
  • 42
92
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
  • 12,658
  • 17
  • 74
  • 108
84
votes
5 answers

Swap two items in List

Is there a LINQ way to swap the position of two items inside a list?
Tony The Lion
  • 57,181
  • 57
  • 223
  • 390
81
votes
19 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,540
  • 4
  • 15
  • 17
76
votes
29 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
  • 26,251
  • 31
  • 104
  • 163
76
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
  • 14,220
  • 23
  • 77
  • 113
73
votes
2 answers

Is id = 1 - id atomic?

From page 291 of OCP Java SE 6 Programmer Practice Exams, question 25: public class Stone implements Runnable { static int id = 1; public void run() { id = 1 - id; if (id == 0) pick(); else …
Adam Stelmaszczyk
  • 18,835
  • 4
  • 63
  • 104
73
votes
6 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
  • 12,011
  • 51
  • 154
  • 289
71
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,096
  • 1
  • 9
  • 12
61
votes
12 answers

Effective 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
  • 2,996
  • 9
  • 33
  • 65
1
2 3
99 100