swap

swap temporary tuples of references

醉酒当歌 提交于 2019-12-04 06:56:23
I'm writing a custom iterator that, when dereferenced returns a tuple of references. Since the tuple itself is ephemeral, I don't think I can return a reference from operator*(). I think my iterator makes sense semantically, since it has reference semantics, even though operator* returns a value. The issue is, when I try to call std::swap (or rather, when std::sort does), like below, I get errors because the swap expects l-values. Is there an easy fix to this problem? #include <vector> class test { public: test() :v1(10), v2(10) {} class iterator { public: iterator(std::vector<int>& _v1, std:

Assembly - Swap function - Why it will not work?

主宰稳场 提交于 2019-12-04 05:33:53
问题 I need to create a function that swaps the value of &x with the value of &y (meaning swap *(&y) and *(&x). Swap: push EBP mov EBP,ESP mov EBX, [EBP+12] ; ebx = *x mov EAX, DWORD [EBX] ;eax = ebx = *x mov DWORD [EBP-4], EAX ; [ebp-4] = eax =*x mov EDX, [EBP+8] ; edx = *y mov EAX, DWORD [EDX] ; eax = *edx = *y mov DWORD [EBX], EAX ; ebx = eax = *y mov EAX, DWORD [EBP-4] ; eax = *x mov DWORD [EDX], EAX ; edx = *x pop EBP ; ebx = *y and edx = *x ret I call it like this: // call Swap push x push y

Why swap with xor works fine in c++ but in java doesn't ? some puzzle [duplicate]

狂风中的少年 提交于 2019-12-04 04:16:41
Possible Duplicate: Why is this statement not working in java x ^= y ^= x ^= y; Sample code int a=3; int b=4; a^=(b^=(a^=b)); In c++ it swaps variables, but in java we get a=0, b=4 why? By writing your swap all in one statement, you are relying on side effects of the inner a^=b expression relative to the outer a^=(...) expression. Your Java and C++ compilers are doing things differently. In order to do the xor swap properly, you have to use at least two statements: a ^= b; a ^= (b ^= a); However, the best way to swap variables is to do it the mundane way with a temporary variable, and let the

Swapping div contents with jQuery

吃可爱长大的小学妹 提交于 2019-12-04 03:16:19
Here's my HTML: <div class="large"> <img src="/images/photos/Interior.jpg" alt="The interior" style="[...]" /> <div class="caption">The interior</div> </div> <div class="small"> <img src="/images/photos/Bedroom.jpg" alt="Bedroom" style="[A different ...]" /> <div class="caption">A bedroom</div> </div> Upon clicking a div.small , I'd like both the images and captions to swap container divs. The catch is I can't just swap the src, as there are a bunch of inline styles set which need to be preserved. Finally, once the images have been swapped, I want to apply my custom function .fitToParent() to

Command-line to reverse byte order/change endianess

ぐ巨炮叔叔 提交于 2019-12-04 00:29:34
I'm hacking around in some scripts trying to parse some data written by Javas DataOutputStream#writeLong(...) . Since java always seems to write big endian, I have a problem feeding the bytes to od . This is due to the fact that od always assumes that the endianess matches the endianess of the arch that you are currently on, and I'm on a little endian machine. I'm looking for an easy one-liner to reverse the byte order. Let's say that you know that the last 8 bytes of a file is a long written by the aforementioned writeLong(...) method. My current best attempt to print this long is tail -c 8

Is there an elegant way to swap references in C++?

大城市里の小女人 提交于 2019-12-04 00:27:51
问题 Sometimes classes are referencing other classes. Implementing std::swap() for such classes cannot be straightforward, because it would lead to swapping of original instances instead of references. The code below illustrates this behavior: #include <iostream> class A { int& r_; public: A(int& v) : r_(v) {} void swap(A& a) { std::swap(r_, a.r_); } }; void test() { int x = 10; int y = 20; A a(x), b(y); a.swap(b); std::cout << "x=" << x << "\n" << "y=" << y << "\n"; } int main() { test(); return

Why does `basic_ios::swap` only do a partial swap?

*爱你&永不变心* 提交于 2019-12-03 22:40:15
C++11 §27.5.4.2/21: void swap(basic_ios& rhs); Effects: The states of *this and rhs shall be exchanged, except that rdbuf() shall return the same value as it returned before the function call, and rhs.rdbuf() shall return the same value as it returned before the function call. What is this partial swapping useful for? Can it cause trouble? You can blame me for this one. The committee has tried to change (twice I think), but each time the solution ended up breaking things. Swap and move semantics was retrofitted onto our I/O system a decade after it was designed. And it wasn't a perfectly clean

Swapping two structures in c

六月ゝ 毕业季﹏ 提交于 2019-12-03 21:59:30
Hi i'm trying to create a swap function that swaps the first two elements of the structure. Can someone please show me how to make this work. void swap(struct StudentRecord *A, struct StudentRecord *B){ struct StudentRecord *temp = *A; *A = *B; *B = *temp; } struct StudentRecord *pSRecord[numrecords]; for(int i = 0; i < numrecords; i++) { pSRecord[i] = &SRecords[i]; } printf("%p \n", pSRecord[0]); printf("%p \n", pSRecord[1]); swap(&pSRecord[0], &pSRecord[1]); printf("%p \n", pSRecord[0]); printf("%p \n", pSRecord[1]); The expression *A has type struct StudentRecord while the name temp is

How to swap the bitmap images on View in android?

99封情书 提交于 2019-12-03 20:31:05
I am working with small application for display bubbles images on android screen.I have displayed all bubbles images from resource directory.I have implemented code as follows in view class. onDraw method: @Override protected void onDraw(Canvas canvas) { super.dispatchDraw(canvas); drawImages(canvas); } I have implemented drawImages() method as follows: BitmapFactory.Options opts = new BitmapFactory.Options(); private void drawImages(Canvas canvas) { for(int i = 0; i<MAX_ROWS; i++){ for(int j=0; j<MAX_COLS; j++) { bmp = BitmapFactory.decodeResource(mContext.getResources(), items[i][j],opts);

Name hiding by using declaration

天大地大妈咪最大 提交于 2019-12-03 20:22:25
#include <iostream> struct H { void swap(H &rhs); }; void swap(H &, H &) { std::cout << "swap(H &t1, H &t2)" << std::endl; } void H::swap(H &rhs) { using std::swap; swap(*this, rhs); } int main(void) { H a; H b; a.swap(b); } And this is the result: swap(H &t1, H &t2) In the code above, I try to define a swap function of H . In the function void H::swap(H &rhs) , I use an using declaration to make the name std::swap visible. If there isn't an using declaration, the code cannot be compiled because there is no usable swap function with two parameters in class H . I have a question here. In my