swap

Swap arrays by using pointers in C++

落爺英雄遲暮 提交于 2019-12-18 19:07:48
问题 I have two arrays of pointers to doubles that I need to swap. Rather than just copy the data within the arrays, it would be more efficient just to swap the pointers to the arrays. I was always under the impression that array names were essentially just pointers, but the following code receives a compiler error: double left[] = {1,2,3}; double right[] = {9,8,7}; double * swap = left; left = right; // Error "ISO C++ forbids assignment of arrays" right = swap; // Error "incompatible types in

Interchanging values of two variables by xor operator using references or pointers

白昼怎懂夜的黑 提交于 2019-12-18 09:41:59
问题 I have two integer variables i and j and I want to make a function which takes these two variables as its argument and interchanges their contents using xor operator. Now if I make the function to take arguments by value i.e void swap (int x , int y); (with function body same as for the function swap below) then the values are being swapped nicely within the function. But as what I want is the swapping of the values of the variables in the calling function I used passing arguments by

Swap elements in LinkedList

痴心易碎 提交于 2019-12-18 07:43:52
问题 I want to maintain order of the elements being added in a list. So, I used a LinkedList in Java. Now I want to be able to swap two elements in the linked list. First of all, I cannot find an elementAt() for LinkedList . Also, there is no way to add element at a specified position. 回答1: There is a Collections.swap(List<?> list, int i, int j) that you can use to swap two elements of a List<?> . There's also LinkedList.get(int index) and LinkedList.add(int index, E element) (both are methods

Undefined behaviour of operators in XOR swap algorithm?

早过忘川 提交于 2019-12-18 06:13:44
问题 void swap(int* a, int* b) { if (a != b) *a ^= *b ^= *a ^= *b; } As the above *a ^= *b ^= *a ^= *b is just a shortcut for *a = *a ^ (*b = *b ^ (*a = *a ^ *b)) , could (e.g.) the 2nd *a be evaluated (for the XOR) just before the 3rd *a is modified (by the =)? Does it matter whether I write it in C99/C11/C++98/C++11? 回答1: The C++11 standard says: 5.17/1: The assignment operator (=) and the compound assignment operators all group right-to-left . (...) the assignment is sequenced after the value

Move or swap a stringstream

一个人想着一个人 提交于 2019-12-18 04:52:56
问题 I want to move a stringstream, in the real world application I have some stringstream class data member, which I want to reuse for different string's during operation. stringstream does not have a copy-assignment or copy constructor, which makes sense. However, according to cppreference.com and cplusplus.com std::stringstream should have a move assignment and swap operation defined. I tried both, and both fail. Move assignment #include <string> // std::string #include <iostream> // std::cout

Animated swap position of two buttons

和自甴很熟 提交于 2019-12-18 04:17:15
问题 I am trying to swap the position of two buttons. My swapping code looks is: private void exchangeButtons(Button btn1, Button btn2) { // Create the animation set AnimationSet exchangeAnimation = new AnimationSet(true); TranslateAnimation translate = new TranslateAnimation( Animation.RELATIVE_TO_SELF, btn2.getLeft(), Animation.RELATIVE_TO_SELF, btn1.getLeft(), Animation.RELATIVE_TO_SELF, btn2.getRight(), Animation.RELATIVE_TO_SELF, btn1.getRight()); translate.setDuration(500); exchangeAnimation

Making swap faster, easier to use and exception-safe

独自空忆成欢 提交于 2019-12-18 03:30:18
问题 I could not sleep last night and started thinking about std::swap . Here is the familiar C++98 version: template <typename T> void swap(T& a, T& b) { T c(a); a = b; b = c; } If a user-defined class Foo uses external ressources, this is inefficient. The common idiom is to provide a method void Foo::swap(Foo& other) and a specialization of std::swap<Foo> . Note that this does not work with class templates since you cannot partially specialize a function template, and overloading names in the

Making swap faster, easier to use and exception-safe

半世苍凉 提交于 2019-12-18 03:29:58
问题 I could not sleep last night and started thinking about std::swap . Here is the familiar C++98 version: template <typename T> void swap(T& a, T& b) { T c(a); a = b; b = c; } If a user-defined class Foo uses external ressources, this is inefficient. The common idiom is to provide a method void Foo::swap(Foo& other) and a specialization of std::swap<Foo> . Note that this does not work with class templates since you cannot partially specialize a function template, and overloading names in the

Swap items in doubly-linked list by their indices in the backing array

三世轮回 提交于 2019-12-17 21:12:37
问题 I have an array of objects of the following type: struct Node { Node *_pPrev, *_pNext; double *_pData; }; Some of the nodes participate in a doubly-linked list, with _pData!=nullptr for such nodes. There is also a dummy head node with _pNext pointing to the beginning of the list, and _pPrev pointing to the end. The list starts with containing only this head node, and it should be never removed from the list. The doubly-linked list is backed by an array, with initial size equal to the maximum

How do I swap tensor's axes in TensorFlow?

荒凉一梦 提交于 2019-12-17 18:17:23
问题 I have a tensor of shape (30, 116, 10) , and I want to swap the first two dimensions, so that I have a tensor of shape (116, 30, 10) I saw that numpy as such a function implemented ( np.swapaxes ) and I searched for something similar in tensorflow but I found nothing. Do you have any idea? 回答1: tf.transpose provides the same functionality as np.swapaxes , although in a more generalized form. In your case, you can do tf.transpose(orig_tensor, [1, 0, 2]) which would be equivalent to np.swapaxes