swap

How to swap keys with values in array?

牧云@^-^@ 提交于 2019-11-28 03:11:03
问题 I have array like: array( 0 => 'a', 1 => 'b', 2 => 'c' ); I need to convert it to: array( 'a', 'b', 'c' ); What's the fastest way to swap keys with values? 回答1: PHP has the array_flip function which exchanges all keys with their corresponding values, but you do not need it in your case because the arrays are the same. array( 'a', 'b', 'c' ); This array has the keys 0, 1, and 2. 回答2: Use array_flip() . That will do to swap keys with values. However, your array is OK the way it is. That is, you

sql swap primary key values

余生颓废 提交于 2019-11-28 00:46:06
is it possible to swap primary key values between two datasets? If so, how would one do that? Unreason 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 Note: Updating primary keys has other side effects and maybe the preferred approach would be to leave the primary keys as they are and swap the values of all the other columns. Caveat: The reason why the t.id = t2.id, t2.id = t.id works

Move semantics == custom swap function obsolete?

家住魔仙堡 提交于 2019-11-27 20:25:54
Recently, many questions pop up on how to provide your own swap function. With C++11, std::swap will use std::move and move semantics to swap the given values as fast as possible. This, of course, only works if you provide a move constructor and a move assignment operator (or one that uses pass-by-value). Now, with that given, is it actually necessary to write your own swap functions in C++11? I could only think of non-movable types, but then again, the custom swap s usually work through some kind of "pointer exchange" (aka moving). Maybe with certain reference variables? Hm... Howard Hinnant

Finding the minimum number of swaps to convert one string to another, where the strings may have repeated characters

烈酒焚心 提交于 2019-11-27 19:06:51
I was looking through a programming question, when the following question suddenly seemed related. How do you convert a string to another string using as few swaps as follows. The strings are guaranteed to be interconvertible (they have the same set of characters, this is given), but the characters can be repeated . I saw web results on the same question, without the characters being repeated though. Any two characters in the string can be swapped. For instance : "aabbccdd" can be converted to "ddbbccaa" in two swaps, and "abcc" can be converted to "accb" in one swap. Thanks! David Eisenstat

Swapping elements in a Common Lisp list

北慕城南 提交于 2019-11-27 17:44:42
问题 Is there a Common Lisp function that will swap two elements in a list given their indices and return the modified list? 回答1: You can use rotatef: (rotatef (nth i lst) (nth j lst)) Of course, list indexing can be expensive (costing O( size of list )), so if you do this with any regularity, you'd rather want to use an array: (rotatef (aref arr i) (aref arr j)) 回答2: I would avoid indexing into the list twice by using nthcdr to get the cdr of the cons cell containing the first element that you

How do you swap two integer variables without using any if conditions, casting, or additional variables? [closed]

蹲街弑〆低调 提交于 2019-11-27 15:40:14
There are two integer variables. Can you swap those integer variables without using any if conditions, without casting, and without using additional variables? For example: int a = 10; int b = 5; a > b always. The answer should be a == 5 and b == 10 If you think you are being clever by not using 3rd variable then do some performance tests and you see that the much faster way is to use 3rd int to store the variable temporarily. Anyways, i solved the problem with XOR bitwise operator: a ^= b; b ^= a; a ^= b; a=a+b; b=a-b; a=a-b; It's a little trick. int a = 5; int b= 10; a = a+b; b = a-b; /*

Swapping Nodes on a single linked list

老子叫甜甜 提交于 2019-11-27 14:50:55
I am trying to make a swapNode function that can take any two nodes and swap them. I've made an algorithm that works if they're at least 2 nodes away, but I can't seem to come up with an algorithm that will work if they are closer to each other. Here's what I wrote so far: void swapNode(call * &head, call * &first, call * &second){ call * firstPrev = NULL; call * secPrev = NULL; call * current = head; //set previous for first while((current->next != first) ){ current = current->next; } firstPrev = current; current = head; //set previous for second while((current->next != second) ){ current =

Potential Problem in “Swapping values of two variables without using a third variable”

China☆狼群 提交于 2019-11-27 14:38:21
I recently came along this method for swapping the values of two variables without using a third variable. a^=b^=a^=b But when I tried the above code on different compilers, I got different results, some gave correct results, some didn't. Is anything terribly wrong with the code? Prasoon Saurav Is anything terribly wrong with the code? Yes! a^=b^=a^=b in fact invokes Undefined Behaviour in C and in C++ because you are trying to change the value of a more than once between two sequence points. Try writing (although not foolproof ) a ^= b; b ^= a; a ^= b; instead of a^=b^=a^=b . P.S : Never try

Can I swap colors in image using GD library in PHP?

我的梦境 提交于 2019-11-27 14:04:18
I got the image like this (it's a graph): (source: kitconet.com ) I want to change the colours, so the white is black, the graph line is light blue, etc.. is it possible to achieve with GD and PHP? This will replace the white color with Gray $imgname = "test.gif"; $im = imagecreatefromgif ($imgname); $index = imagecolorclosest ( $im, 255,255,255 ); // get White COlor imagecolorset($im,$index,92,92,92); // SET NEW COLOR $imgname = "result.gif"; imagegif($im, $imgname ); // save image as gif imagedestroy($im); David Crowell I had trouble making this solution work. The image cannot be a true

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

此生再无相见时 提交于 2019-11-27 13:03:27
MSDN says : swap should be used in preference to iter_swap , which was included in the C++ Standard for backward compatibility. But comp.std.c++ says : Most STL algorithms operate on iterator ranges. It therefore makes sense to use iter_swap when swapping elements within those ranges, since that is its intended purpose --- swapping the elements pointed to by two iterators. This allows optimizations for node-based sequences such as std::list , whereby the nodes are just relinked, rather than the data actually being swapped. So which one is correct? Should I use iter_swap , or should I use swap