swap

Why can't I swap two items in a list in one line?

元气小坏坏 提交于 2019-12-03 19:11:12
问题 Why does this not work (values are not swapped): lol = ["test","test2"] lol[lol.index("test")], lol[lol.index("test2")] = lol[lol.index("test2")], lol[lol.index("test")] But this works (values are swapped): i1 = lol.index("test") i2 = lol.index("test2") lol[i1], lol[i2] = lol[i2], lol[i1] 回答1: The reason why the first example is not working is because you are calling .index() multiple times, and after each time, the values in the list are changing, so the indices found in the code are not

using directive vs using declaration swap in C++

岁酱吖の 提交于 2019-12-03 09:04:46
问题 Please refer to the code below: #include <algorithm> namespace N { template <typename T> class C { public: void SwapWith(C & c) { using namespace std; // (1) //using std::swap; // (2) swap(a, c.a); } private: int a; }; template <typename T> void swap(C<T> & c1, C<T> & c2) { c1.SwapWith(c2); } } namespace std { template<typename T> void swap(N::C<T> & c1, N::C<T> & c2) { c1.SwapWith(c2); } } As written above, the code doesn't compile on Visual Studio 2008/2010. The error is: 'void N::swap(N::C

Permutation of a vector

久未见 提交于 2019-12-03 08:40:06
问题 suppose I have a vector: 0 1 2 3 4 5 [45,89,22,31,23,76] And a permutation of its indices: [5,3,2,1,0,4] Is there an efficient way to resort it according to the permutation thus obtaining: [76,31,22,89,45,23] Using at most O(1) additional space? 回答1: Yes. Starting from the leftmost position, we put the element there in its correct position i by swapping it with the (other) misplaced element at that position i. This is where we need the O(1) additional space. We keep swapping pairs of elements

Why is swapping multidimensional arrays not noexcept?

久未见 提交于 2019-12-03 08:31:57
问题 I have the following snippet: #include <algorithm> #include <iostream> int main(int argc, char** argv) { int x[2][3]; int y[2][3]; using std::swap; std::cout << noexcept(swap(x, y)) << "\n"; return 0; } Using GCC 4.9.0, this prints 0 . I don't understand why. According to the standard there's two overloads for std::swap : namespace std { template<class T> void swap(T& a, T& b) noexcept( is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value ); template<class T, size_t

How to swap two string variables in Java without using a third variable

孤者浪人 提交于 2019-12-03 06:59:22
问题 How do I swap two string variables in Java without using a third variable, i.e. the temp variable? String a = "one" String b = "two" String temp = null; temp = a; a = b; b = temp; But here there is a third variable. We need to eliminate the use of the third variable. 回答1: Do it like this without using a third variable: String a = "one"; String b = "two"; a = a + b; b = a.substring(0, (a.length() - b.length())); a = a.substring(b.length()); System.out.println("a = " + a); System.out.println("b

Swapping ms-sql tables

℡╲_俬逩灬. 提交于 2019-12-03 01:44:10
I want to swap to tables in the best possible manner. I have an IpToCountry table, and I create a new one on a weekly basis according to an external CSV file which I import. The fastest way I've found to make the switch was doing the following: sp_rename IpToCountry IpToCountryOld go sp_rename IpToCountryNew IpToCountry go The problem with this is that the table might still be accessed in between. How do I approach this problem in SQL? In considered using sp_getapplock and sp_releaseapplock, but I want to keep the read from the table function as quick as possible. Assuming that you're unable

Why is swapping multidimensional arrays not noexcept?

对着背影说爱祢 提交于 2019-12-03 00:00:19
I have the following snippet: #include <algorithm> #include <iostream> int main(int argc, char** argv) { int x[2][3]; int y[2][3]; using std::swap; std::cout << noexcept(swap(x, y)) << "\n"; return 0; } Using GCC 4.9.0, this prints 0 . I don't understand why. According to the standard there's two overloads for std::swap : namespace std { template<class T> void swap(T& a, T& b) noexcept( is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value ); template<class T, size_t N> void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b))); } In my understanding the

How to swap two string variables in Java without using a third variable

你。 提交于 2019-12-02 21:36:10
How do I swap two string variables in Java without using a third variable, i.e. the temp variable? String a = "one" String b = "two" String temp = null; temp = a; a = b; b = temp; But here there is a third variable. We need to eliminate the use of the third variable. Ankur Lathi Do it like this without using a third variable: String a = "one"; String b = "two"; a = a + b; b = a.substring(0, (a.length() - b.length())); a = a.substring(b.length()); System.out.println("a = " + a); System.out.println("b = " + b); marcus // taken from this answer: https://stackoverflow.com/a/16826296/427413 String

Permutation of a vector

匆匆过客 提交于 2019-12-02 21:09:18
suppose I have a vector: 0 1 2 3 4 5 [45,89,22,31,23,76] And a permutation of its indices: [5,3,2,1,0,4] Is there an efficient way to resort it according to the permutation thus obtaining: [76,31,22,89,45,23] Using at most O(1) additional space? Yes. Starting from the leftmost position, we put the element there in its correct position i by swapping it with the (other) misplaced element at that position i. This is where we need the O(1) additional space. We keep swapping pairs of elements around until the element in this position is correct. Only then do we proceed to the next position and do

Linker performance related to swap space?

冷暖自知 提交于 2019-12-02 20:02:09
Sometimes it's handy to mock up something with a little C program that uses a big chunk of static memory. I noticed after changing to Fedora 15 the program took a long time to compile. We're talking 30s vs. 0.1s. Even more weird was that ld (the linker) was maxing out the CPU and slowly started eating all available memory. After some fiddling I managed to find a correlation between this new problem and the size of my swap file. Here's an example program for the purposes of this discussion: #include <string.h> #include <stdlib.h> #include <stdio.h> #define M 1000000 #define GIANT_SIZE (200*M)