swap

Javascript: How can I swap elements of an array of objects (by reference, not index)?

喜你入骨 提交于 2019-12-02 01:48:54
I have an array of objects a=[ {v:0}, {v:1}, {v:2}, {v:3} ] ; I do not have the index into the array, but I do have references to the 2 values I want to swap s1=a[2] ; s2 = a[3] ; How do I use these references to swap the elements of the actual array? [s1,s2] = [s2,s1] ; // only swaps s1 and s2, NOT elements of the array // a is unchanged Mosè Raguzzini If you have the reference you can safely retrieve the index by Array.indexOf() : a.indexOf(myReference) // returns the index of the reference or -1 Then, with retrieved index, you can proceed as usual. Like this: let a = [{ v: 0 }, { v: 1 }, {

swap in doubly linked list

回眸只為那壹抹淺笑 提交于 2019-12-02 01:19:32
I am trying to swap two nodes in a doubly linked list. Below is the part of program having swap function. int swap (int x, int y) { struct node *temp = NULL ; struct node *ptr1, *ptr2; temp = (struct node *)malloc(sizeof(struct node)); if (head == NULL ) { printf("Null Nodes"); } else { ptr1 = ptr2 = head; int count = 1; while (count != x) { ptr1 = ptr1->next; count++; } int count2 = 1; while (count2 != y) { ptr2 = ptr2->next; count2++; } ptr1->next->prev = ptr2; ptr1->prev->next = ptr2; ptr2->next->prev = ptr1; ptr2->prev->next = ptr1; temp->prev = ptr1->prev; ptr1->prev = ptr2->prev; ptr2-

Weird XOR swap behavior while zeroing out data

狂风中的少年 提交于 2019-12-02 00:34:00
问题 Thanks Doug. Here's the fix: void swap(int& a, int& b) { if (&a == &b) // added this check to ensure the same address is not passed in return; a ^= b; b ^= a; a ^= b; } I am implementing quicksort for fun in C++, and I am using integers for dummy data. I had been using the XOR swapping algorithm to swap two values in place, but I noticed my sort was screwing up. I changed my swapping algorithm and it worked. I added some debugging statements, and found that the XOR swap was doing something

How to swap clipboard contents for current selection in Visual Studio

醉酒当歌 提交于 2019-12-01 23:46:11
问题 Does anyone know of a simple way to swap whatever is currently in my clipboard with the current mouse selection? So lets say my clipboard has the text Foo in it. On a line of code public void DoBar() { ... I have Bar selected I'd like a simple key combination that changes DoBar() to DoFoo() and leaves me with the text Bar" in the clipboard (so that if I was to press CTRL-V it would paste Bar ). Note: I don't want to have to fiddle with anything graphical (i.e. clipboard switcher, paste ring).

Value type and reference type problem

浪尽此生 提交于 2019-12-01 22:17:49
问题 Hi I'm trying to do a simple swap of two objects.My code is void Main() { object First = 5; object Second = 10; Swap(First, Second); //If I display results it displays as //Value of First as 5 and Second as 10 } private static void Swap(object First, object Second) { object temp = First; First = Second; Second = temp; } Since objects are reference type, its reference should be passed to method and it should swap. why is it not happening? 回答1: There's various different things here: the objects

Value type and reference type problem

可紊 提交于 2019-12-01 21:34:51
Hi I'm trying to do a simple swap of two objects.My code is void Main() { object First = 5; object Second = 10; Swap(First, Second); //If I display results it displays as //Value of First as 5 and Second as 10 } private static void Swap(object First, object Second) { object temp = First; First = Second; Second = temp; } Since objects are reference type, its reference should be passed to method and it should swap. why is it not happening? There's various different things here: the objects live off in the heap. Forget about them for now the references to the objects are what get stored in First

Weird XOR swap behavior while zeroing out data

好久不见. 提交于 2019-12-01 20:15:34
Thanks Doug. Here's the fix: void swap(int& a, int& b) { if (&a == &b) // added this check to ensure the same address is not passed in return; a ^= b; b ^= a; a ^= b; } I am implementing quicksort for fun in C++, and I am using integers for dummy data. I had been using the XOR swapping algorithm to swap two values in place, but I noticed my sort was screwing up. I changed my swapping algorithm and it worked. I added some debugging statements, and found that the XOR swap was doing something weird. I printed the data before and after I swapped it, and this is what it printed: ... swapping -5, -3

When would you swap two numbers without using a third variable?

一个人想着一个人 提交于 2019-12-01 19:02:55
问题 I have read several sources that discuss how to swap two numbers without using a third variable. These are a few of the most relevant: How do you swap two integer variables without using any if conditions, casting, or additional variables? Potential Problem in "Swapping values of two variables without using a third variable" Swap two integers without using a third variable http://www.geeksforgeeks.org/swap-two-numbers-without-using-temporary-variable/ I understand why it doesn't make sense to

When would you swap two numbers without using a third variable?

喜你入骨 提交于 2019-12-01 18:34:39
I have read several sources that discuss how to swap two numbers without using a third variable. These are a few of the most relevant: How do you swap two integer variables without using any if conditions, casting, or additional variables? Potential Problem in "Swapping values of two variables without using a third variable" Swap two integers without using a third variable http://www.geeksforgeeks.org/swap-two-numbers-without-using-temporary-variable/ I understand why it doesn't make sense to use the described methods in most cases: the code becomes cluttered and difficult to read and will

Swapping first and last items in a list

元气小坏坏 提交于 2019-12-01 18:23:33
How can I go about swapping numbers in a given list? For example: list = [5,6,7,10,11,12] I would like to swap 12 with 5 . Is there an built-in Python function that can allow me to do that? >>> lis = [5,6,7,10,11,12] >>> lis[0], lis[-1] = lis[-1], lis[0] >>> lis [12, 6, 7, 10, 11, 5] Order of evaluation of the above expression: expr3, expr4 = expr1, expr2 First items on RHS are collected in a tuple, and then that tuple is unpacked and assigned to the items on the LHS. >>> lis = [5,6,7,10,11,12] >>> tup = lis[-1], lis[0] >>> tup (12, 5) >>> lis[0], lis[-1] = tup >>> lis [12, 6, 7, 10, 11, 5]