swap

Why shared pointer assignment does 'swap'?

一世执手 提交于 2019-12-11 02:21:59
问题 As i understand when assigned shared ptr should behave like: a) if (--this->count == 0) { release this->count and this->obj } b) this->count = r->count, this->obj = r->obj; What boost does is just shared_ptr & operator=( shared_ptr const & r ) BOOST_NOEXCEPT { this_type(r).swap(*this); return *this; } So what is the magic behind swap and why this works? 回答1: a) if (--this->count == 0) { release this->count and this->obj } No, a shared_ptr keeps two counts, one for the object and one for the

Swap indexes using slices?

百般思念 提交于 2019-12-11 01:17:19
问题 I know that you can swap 2 single indexes in Python r = ['1', '2', '3', '4', '5', '6', '7', '8'] r[2], r[4] = r[4], r[2] output: ['1', '2', '5', '4', '3', '6', '7', '8'] But why can't you swap 2 slices of indexes in python? r = ['1', '2', '3', '4', '5', '6', '7', '8'] I want to swap the numbers 3 + 4 with 5 + 6 + 7 in r: r[2:4], r[4:7] = r[4:7], r[2:4] output: ['1', '2', '5', '6', '3', '4', '7', '8'] expected output: ['1', '2', '5', '6', '7', '3', '4', '8'] What did I wrong? output: 回答1: The

Swapping Objects in an Array - C#

对着背影说爱祢 提交于 2019-12-10 19:33:43
问题 In C#, I have an Array of MenuItem. I'm trying to swap the two Objects in index 2 and index 3 of the array without success using the code below: MenuItem Temp = Items[2]; Items[2] = Items[3]; Items[3] = Temp; There must be a reason why the second and third lines aren't working in C# which I may not understand yet. Is anyone able to clarify it a bit more? Do I have to go deeper and swap each property in the objects individually? Edited - Sorry. Looks like I made a mess of the code when trying

Swapping Values with XOR [duplicate]

∥☆過路亽.° 提交于 2019-12-10 17:42:16
问题 This question already has answers here : Sequence Point - Xor Swap on Array get wrong result (1 answer) How does XOR variable swapping work? (9 answers) Closed 5 years ago . What is the difference between these two macros? #define swap(a, b) (((a) ^ (b)) && ((a) ^= (b) ^= (a) ^= (b))) Or #define swap(a, b) (((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b))) I saw the second macro here but couldn't understand why it wasn't written like the first one? Is there a special reason that I missed? 回答1:

Swapping values between two columns using data.table

天涯浪子 提交于 2019-12-10 12:32:48
问题 I have been breaking my head over translating this question to a data.table solution. (to keep it simple I'll use the same data set) When V2 == "b I want to swap the columns between V1 <-> V3 . dt <- data.table(V1=c(1,2,4), V2=c("a","a","b"), V3=c(2,3,1)) #V1 V2 V3 #1: 1 a 2 #2: 2 a 3 #3: 4 b 1 The code below would be the working solution for data.frame , however because of the amount of frustration this has given me because I was using a data.table without realising I'm now determined to

How do I swap 2 elements in a list [duplicate]

主宰稳场 提交于 2019-12-10 11:27:57
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Swap two items in List<T> Edit: Maybe this will work for getting the 'b' value? for (int i = 0; i < inventory.Count; i++) { if (inventory[a].ItemRectangle.Intersects(inventory[i].ItemRectangle)) { itemB = inventory[i]; } } Edit: Here's my progress. Item itemA; Item itemB; int a = -1; int b = -1; if (a != -1 && b != -1) { itemA = inventory[a]; itemB = inventory[b]; Swap(ref itemA, ref itemB); inventory[a] = itemA

how do you swap two rows in a matrix (in C)?

喜欢而已 提交于 2019-12-10 04:29:19
问题 for example, given a matrix: 1 2 3 4 5 6 7 8 9 if you are goint to swap row[0] and row[1], resulting matrix would be: 4 5 6 1 2 3 7 8 9 can you guys help me get a code in C for this? 回答1: The answer depends entirely on how your "matrix" is implemented, because the c language has no notion of such a thing. Are you using two dimensional arrays? double m[3][3]; Or something else? Two dimensional arrays You will have to move individual elements by hand. for (i=0; i<ROWLENGTH; ++i){ double temp;

jQuery snippet to swap two sets of elements with animation

北城以北 提交于 2019-12-09 19:40:06
问题 is there some jQuery code to swap 2 sets of elements with animation? i only found Move list item to top of unordered list using jQuery but its too limited (only slide element to top and not accounting for different margins). 回答1: see this fiddle. arbitrary borders, margins, paddings, sizes etc. supported and still no jumps at animation end. function slideSwap($set1, $set2) { //$elem.append(infoString($elem)); //$after.append(infoString($after)); $set1.css("color", "red"); $set2.css("color",

C - fastest method to swap two memory blocks of equal size?

浪尽此生 提交于 2019-12-09 14:58:06
问题 What is the fastest way to swap two non-overlapping memory areas of equal size? Say, I need to swap (t_Some *a) with (t_Some *b) . Considering space-time trade-off, will increased temporary space improve the speed? For example, (char *tmp) vs (int *tmp) ? I am looking for a portable solution. Prototype: void swap_elements_of_array(void* base, size_t size_of_element, int a, int b); 回答1: Your best bet is to maximize registers usage so that when you read a temporary you don't end up with extra

How to replace specific values in a vector in C++?

自闭症网瘾萝莉.ら 提交于 2019-12-09 01:16:45
问题 I have a vector with some values (3, 3, 6, 4, 9, 6, 1, 4, 6, 6, 7, 3), and I want to replace each 3 with a 54 or each 6 with a 1, for example and so on. So I need to go through the vector first, get the [i] value, search and replace each 3 with a 54, but still keep relevant positions.std::set is vector::swap a good way? I am not even sure how to begin this :( I can't use push_back as that would not keep the correct order of values as that is important. Please keep it simple; I am just a