swap

Name hiding by using declaration

怎甘沉沦 提交于 2019-12-21 05:35:32
问题 #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

Swap characters in specific positions in strings of varying lengths

拟墨画扇 提交于 2019-12-21 05:30:15
问题 I've been trying to learn sed and the examples I've found here are for swapping dates from 05082012 to 20120805 and I'm having trouble adapting them to my current need. I need to convert an IP address 10.4.13.22 to a reverse lookup of 22.13.4.10 for a nsupdate script. My biggest problem is the fact that sometimes each octet can change lengths e.g. 10.4.13.2 and 10.19.8.126 Thanks for any help! echo 10.0.2.99 | sed 's/\(....\)\(....\)/\2\1/' this is currently what I've tried, just based off

Faster to swap or assign a vector of strings?

只谈情不闲聊 提交于 2019-12-20 04:11:58
问题 I have a class with a vector of strings and a function that assigns to that vector. I am changing my function to only assign to the vector if it's successful. To do that I use a temporary vector of strings in the function and then if the function is successful I assign to the vector of strings in the class. For example: class test { vector<string> v; void Function() { vector<string> temp; v = temp; // Is this better? v.swap( temp ); // Or instead is this better? } }; Thanks 回答1: In C++11,

What are the differences between swap in C++ and Python?

老子叫甜甜 提交于 2019-12-20 03:22:04
问题 About swap , in C++, we can swap two by std::swap(x, y); . x and y are passed into swap as reference . But in Python, can we also write a swap function which can do the swap the same way as in C++? The Python version of swap I implemented is kinda stupid: def swap_in_python(x, y): return y, x #so when swapping a and b, I usually do it like this a, b = swap_in_python(a, b) This is really cumbersome, but we can't pass reference or pointer as what we can do in C++, right? EDIT: Is there any

swap in doubly linked list

不打扰是莪最后的温柔 提交于 2019-12-20 03:07:19
问题 I am trying to swap two nodes in a doubly linked list. Below is the part of program having swap function. int swap (int x, int y) { struct node *temp = NULL ; struct node *ptr1, *ptr2; temp = (struct node *)malloc(sizeof(struct node)); if (head == NULL ) { printf("Null Nodes"); } else { ptr1 = ptr2 = head; int count = 1; while (count != x) { ptr1 = ptr1->next; count++; } int count2 = 1; while (count2 != y) { ptr2 = ptr2->next; count2++; } ptr1->next->prev = ptr2; ptr1->prev->next = ptr2; ptr2

swap() function for variables's values [duplicate]

北城余情 提交于 2019-12-19 12:23:08
问题 This question already has answers here : Is JavaScript a pass-by-reference or pass-by-value language? (30 answers) Closed 3 years ago . I am not able to achieve the desired result for this swap function below, where I want the values printed as 3,2 function swap(x,y){ var t = x; x = y; y = t; } console.log(swap(2,3)); Any clue will be appreciated ! 回答1: Your function is swapping the values internally, but the function does not return a value. The following method returns an array of values in

swap classes on click

江枫思渺然 提交于 2019-12-19 11:27:56
问题 I have a list of 6 items that are in a global div (navigationagence) Right now I am able to add a class on click but right now they adds up which means that once my six items are clicked they all end up with the currentagence class. I want to be able to remove the add a class to the clicked item but remove it to the other, How can I achieve that? Here is the jquery code I am using right now. $("#navigationagence ul li").click(function () { $(this).toggleClass("currentagence"); }); 回答1: You

Is it possible to swap the addresses of two variables?

有些话、适合烂在心里 提交于 2019-12-19 10:36:09
问题 I know that it is possible to swap the value of two variables like so #include <stdio.h> void swap(int *x, int *y); int main(){ int x=5,y=10; swap(&x, &y); printf("x: %d, y: %d\n", x, y); return 0; } void swap(int *x, int *y){ int temp; temp=*x; *x=*y; *y=temp; } But is it possible to swap the addresses of those two variables? And I don't mean just making a pointer to them and then swapping the values held by the pointers, I mean actually swapping the two, so that after the swap function the

Swapping elements in lists in python

孤人 提交于 2019-12-19 09:16:13
问题 I have a list and I need to swap the 1st element in the list with the maximum element in the list. But why does code 1 work while code 2 doesn't: code 1: a = list.index(max(list)) list[0], list[a] = list[a], list[0] code 2: list[0], list[list.index(max(list))] = list[list.index(max(list))], list[0] I thought Python would first evaluate the right-hand side before assigning it to the names on the left? 回答1: Python stores results in multiple targets from left to right , executing the assignment

How fast is std::swap for integer types?

我只是一个虾纸丫 提交于 2019-12-19 05:06:37
问题 STL implements a generic std::swap function to swap 2 values. It can be presented in the following way: template <class T> void swap (T& a, T& b) { T c(std::move(a)); a=std::move(b); b=std::move(c); } However, there is a XOR swap algorithm to swap 2 integers (http://en.wikipedia.org/wiki/XOR_swap_algorithm): void swap_u( size_t& x, size_t& y ) { x = x^y; y = x^y; x = x^y; } My questions: Is it an optimization nowadays (on x86 or arm )? Does C++ standard favor this kind of optimization? Are