swap

Move or swap a stringstream

老子叫甜甜 提交于 2019-11-29 07:02:49
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 #include <sstream> // std::stringstream int main () { std::stringstream stream("1234"); //stream = std:

Animated swap position of two buttons

倖福魔咒の 提交于 2019-11-29 04:47:48
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.addAnimation(translate); //int fromX = btn1.getLeft(); //int fromY = btn1.getRight(); //int toX = btn2

Swapping elements in a Common Lisp list

谁说我不能喝 提交于 2019-11-29 03:47:36
Is there a Common Lisp function that will swap two elements in a list given their indices and return the modified list? 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)) aaronasterling 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 want to swap and then use elt to get the remaining element out of the sublist. This means that you

Python Simple Swap Function

▼魔方 西西 提交于 2019-11-29 01:56:56
I came across this problem when attempting to learn python. Consider the following function: def swap0(s1, s2): assert type(s1) == list and type(s2) == list tmp = s1[:] s1 = s2[:] s2 = tmp return s1 = [1] s2 = [2] swap0(s1, s2) print s1, s2 What will s1 and s2 print? After running the problem, I found that the print statement will print 1 2. It seems that the value of s1 and s2 did not change from the swap0 function. The only explanation that I could think of was because of the line. tmp = s1[:] Since s1[:] is a copy, this makes sense that the value of s1 will not change in the function call.

Making swap faster, easier to use and exception-safe

对着背影说爱祢 提交于 2019-11-29 01:32:31
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 std namespace is illegal. The solution is to write a template function in one's own namespace and rely on

Is the copy and swap idiom still useful in C++11

馋奶兔 提交于 2019-11-28 22:50:51
问题 I refer to this question: What is the copy-and-swap idiom? Effectively, the above answer leads to the following implementation: class MyClass { public: friend void swap(MyClass & lhs, MyClass & rhs) noexcept; MyClass() { /* to implement */ }; virtual ~MyClass() { /* to implement */ }; MyClass(const MyClass & rhs) { /* to implement */ } MyClass(MyClass && rhs) : MyClass() { swap(*this, rhs); } MyClass & operator=(MyClass rhs) { swap(*this, rhs); return *this; } }; void swap( MyClass & lhs,

Difference Swapping and Paging

只谈情不闲聊 提交于 2019-11-28 17:10:28
问题 What are the differences between Swapping and Paging with reference to Process Memory Management ? Also guide me to the tutorials if any where I could get more information. 回答1: Swapping refers to copying the entire process address space, or at any rate, the non-shareable-text data segment, out to the swap device, or back, in one go (typically disk). Whereas paging refers to copying in/out one or more pages of the address space. In particular, this is at a much finer grain. For example, there

std::swap vs std::exchange vs swap operator

巧了我就是萌 提交于 2019-11-28 17:00:02
问题 An implementation of std::swap might look like this: template <class T> void swap (T& a, T& b) { T c(std::move(a)); a=std::move(b); b=std::move(c); } template <class T, size_t N> void swap (T (&a)[N], T (&b)[N]) { for (size_t i = 0; i<N; ++i) swap (a[i],b[i]); } An implementation std::exchange n3668 might look like this: template< typename T, typename U = T > T exchange( T & obj, U && new_val ) { T old_val = std::move(obj); obj = std::forward<U>(new_val); return old_val; } It says: For

How to swap two numbers without using temp variables or arithmetic operations?

独自空忆成欢 提交于 2019-11-28 16:51:10
This equation swaps two numbers without a temporary variable, but uses arithmetic operations: a = (a+b) - (b=a); How can I do it without arithmetic operations? I was thinking about XOR. a=a+b; b=a-b; a=a-b; This is simple yet effective.... BiGYaN In C this should work: a = a^b; b = a^b; a = a^b; OR a cooler/geekier looking: a^=b; b^=a; a^=b; For more details look into this . XOR is a very powerful operation that has many interesting usages cropping up here and there. Why not use the std libs? std::swap(a,b); The best way to swap two numbers without using any temporary storage or arithmetic

Why is a = (a+b) - (b=a) a bad choice for swapping two integers?

依然范特西╮ 提交于 2019-11-28 16:48:20
I stumbled into this code for swapping two integers without using a temporary variable or the use of bitwise operators. int main(){ int a=2,b=3; printf("a=%d,b=%d",a,b); a=(a+b)-(b=a); printf("\na=%d,b=%d",a,b); return 0; } But I think this code has undefined behavior in the swap statement a = (a+b) - (b=a); as it does not contain any sequence points to determine the order of evaluation. My question is: Is this an acceptable solution to swap two integers? haccks No. This is not acceptable. This code invokes Undefined behavior . This is because of the operation on b is not defined. In the