swap

How to swap String characters in Java?

牧云@^-^@ 提交于 2019-12-17 10:44:40
问题 How can I swap two characters in a String ? For example, "abcde" will become "bacde" . 回答1: Since String objects are immutable, going to a char[] via toCharArray, swapping the characters, then making a new String from char[] via the String(char[]) constructor would work. The following example swaps the first and second characters: String originalString = "abcde"; char[] c = originalString.toCharArray(); // Replace with a "swap" function, if desired: char temp = c[0]; c[0] = c[1]; c[1] = temp;

Is there a PHP function for swapping the values of two variables?

跟風遠走 提交于 2019-12-17 10:16:50
问题 Say for instance I have ... $var1 = "ABC" $var2 = 123 and under certain conditions I want to swap the two around like so... $var1 = 123 $var2 = "ABC" Is there a PHP function for doing this rather than having to create a 3rd variable to hold one of the values then redefining each, like so... $var3 = $var1 $var1 = $var2 $var2 = $var3 For such a simple task its probably quicker using a 3rd variable anyway and I could always create my own function if I really wanted to. Just wondered if something

Does C++11 change the behavior of explicitly calling std::swap to ensure ADL-located swap's are found, like boost::swap?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 09:38:34
问题 Background Consider for this question the following code: #include <utility> namespace ns { struct foo { foo() : i(0) {} int i; private: foo(const foo&); // not defined, foo& operator=(const foo&); // non-copyable }; void swap(foo& lhs, foo& rhs) { std::swap(lhs.i, rhs.i); } } template <typename T> void do_swap(T& lhs, T& rhs); // implementation to be determined int main() { ns::foo a, b; do_swap(a, b); } In C++03, this implementation of do_swap would be considered "broken": template

How does the standard library implement std::swap?

吃可爱长大的小学妹 提交于 2019-12-17 06:28:13
问题 How is the swap function implemented in the STL? Is it as simple as this: template<typename T> void swap(T& t1, T& t2) { T tmp(t1); t1=t2; t2=tmp; } In other posts, they talk about specializing this function for your own class. Why would I need to do this? Why can't I use the std::swap function? 回答1: How is std::swap implemented? Yes, the implementation presented in the question is the classic C++03 one. A more modern (C++11) implementation of std::swap looks like this: template<typename T>

How to swap two variables in JavaScript

拥有回忆 提交于 2019-12-17 01:37:06
问题 I have this two variables: var a = 1, b = 2; My question is how to swap them? Only this variables, not any objects. 回答1: Here's a one-liner to swap the values of two variables. Given variables a and b : b = [a, a = b][0]; Demonstration below: var a=1, b=2, output=document.getElementById('output'); output.innerHTML="<p>Original: "+a+", "+b+"</p>"; b = [a, a = b][0]; output.innerHTML+="<p>Swapped: "+a+", "+b+"</p>"; <div id="output"></div> 回答2: ES6 (Firefox and Chrome already support it

Swapping list with index

六月ゝ 毕业季﹏ 提交于 2019-12-14 03:34:39
问题 Just want to ask how do i swap the list at the index with the list that follows it and if the list at the index is on the bottom, swap that with the top. So that the index would swap places with the position the list is with the next number For example Normal = [1,2,3,4] and index of 1 would turn to = [1, 3, 2, 4]. making the 2 and 3 swap places and index of 3 would make [4, 2, 3, 1] 回答1: def swap(lst, swap_index): try: next_index = (swap_index + 1) % len(lst) lst[swap_index], lst[next_index]

Write a function swap_halves(s) that takes a string s, and returns a new string in which the two halves of the string have been swapped

雨燕双飞 提交于 2019-12-14 03:32:50
问题 Write a function swap_halves(s) that takes a string s , and returns a new string in which the two halves of the string have been swapped. For example, swap_halves("good day sunshine") would return 'sunshine good day' . I tried something like def swap_halves (s): '''Returns a new string in which the two halves of the spring have swapped''' return (s[0:len(s)] + s[len(s):] ) not sure how to do it without using if or other statements. 回答1: I don't know what exactly you want but this might work

How to swap the positions of Min and Max in an array?

百般思念 提交于 2019-12-13 17:21:24
问题 Here in the code I found the min and max values of any given array . Now I want to swap their positions, and print them out. Like Min at position of Max and vice versa. How can I change their positions? I have done it wrong, I guess. #include <iostream> using namespace std; int main() { int array[8] = { 0, 0, 0, 0, 0, 0, 0, 0}; int min = array[0]; int max = array[0]; int indexOfMin = 0; int indexOfMax = 0; int arrSize = sizeof(array)/sizeof(array[0]); int temp = 0; cout << "Enter an array: ";

Pythonic Swap?

别来无恙 提交于 2019-12-13 11:36:18
问题 I found that i have to perform a swap in python and i write something like this. arr[first], arr[second] = arr[second], arr[first] I suppose this is not so pythonic. Does somebody know how to do a swap in python more elegent? EDIT: I think another example will show my doubts self.memberlist[someindexA], self.memberlist[someindexB] = self.memberlist[someindexB], self.memberlist[someindexA] is this the only available solution for swap in python? I did searched a lot but didn't find a nice

Swap any type of two variables in c [duplicate]

谁都会走 提交于 2019-12-13 04:33:48
问题 This question already has answers here : Implement generic swap macro in C [duplicate] (6 answers) Closed 6 years ago . Is there any logic in c which can swap any type of two variables. i.e int, float, sequence of character. I can think of a logic of storing every type of variables as sequence of characte and swap it like normal string but i does not its good idea. 回答1: Let's see how you'd do this for two char variables. You'd do something like this. void swap(char* a, char* b) { char tmp =