swap

How to swap String characters in Java?

倖福魔咒の 提交于 2019-11-27 12:42:11
How can I swap two characters in a String ? For example, "abcde" will become "bacde" . coobird 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; String swappedString = new String(c); System.out.println(originalString); System.out.println

C++ trying to swap values in a vector

☆樱花仙子☆ 提交于 2019-11-27 12:17:53
This is my swap function: template <typename t> void swap (t& x, t& y) { t temp = x; x = y; y = temp; return; } And this is my function (on a side note v stores strings) call to swap values but whenever I try to call using values in a vector I get an error. I'm not sure what I'm doing wrong. swap(v[position], v[nextposition]); //creates errors I think what you are looking for is iter_swap which you can find also in <algorithm> . all you need to do is just pass two iterators each pointing at one of the elements you want to exchange. since you have the position of the two elements, you can do

Can I tell Linux not to swap out a particular processes' memory?

不羁岁月 提交于 2019-11-27 11:39:34
Is there a way to tell Linux that it shouldn't swap out a particular processes' memory to disk? Its a Java app, so ideally I'm hoping for a way to do this from the command line. I'm aware that you can set the global swappiness to 0, but is this wise? Thomas Kammeyer You can do this via the mlockall(2) system call under Linux; this will work for the whole process, but do read about the argument you need to pass. Do you really need to pull the whole thing in-core? If it's a java app, you would presumably lock the whole JVM in-core. I don't know of a command-line method for doing this, but you

Why does swapping values with XOR fail when using this compound form?

余生长醉 提交于 2019-11-27 11:24:38
I found this code to swap two numbers without using a third variable, using the XOR ^ operator. Code: int i = 25; int j = 36; j ^= i; i ^= j; j ^= i; Console.WriteLine("i:" + i + " j:" + j); //numbers Swapped correctly //Output: i:36 j:25 Now I changed the above code to this equivalent code. My Code: int i = 25; int j = 36; j ^= i ^= j ^= i; // I have changed to this equivalent (???). Console.WriteLine("i:" + i + " j:" + j); //Not Swapped correctly //Output: i:36 j:0 Now, I want to know, Why does my code give incorrect output? EDIT: Okay, got it. The first point to make is that obviously you

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

守給你的承諾、 提交于 2019-11-27 10:54:47
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 like that exists? Update: Using a 3rd variable or wrapping it in a function is the best solution. It's

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

荒凉一梦 提交于 2019-11-27 10:00:02
问题 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. 回答1: a=a+b; b=a-b; a=a-b; This is simple yet effective.... 回答2: 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. 回答3: Why not use the

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

时间秒杀一切 提交于 2019-11-27 09:57:32
问题 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? 回答1: No. This is not acceptable. This

Swap nodes in a singly-linked list

只愿长相守 提交于 2019-11-27 08:46:34
I am trying to swap two nodes. For example if the nodes are a and b I am passing the pointers (a-1)->next and (b-1)->next which are basically nodes a and b . void swap(struct stack **a,struct stack **b) { struct stack *temp1 = *a, *temp2 = *b, *temp3 = *b; *a = *b; (*b)->next = (temp1)->next; temp2 = temp1; (temp2)->next = temp3->next; } What am I doing wrong? When I am trying to print the nodes after calling the function it's an infinite loop. Please help. Grijesh Chauhan Why Infinite loop? Infinite loop is because of self loop in your list after calling swap() function. In swap() code

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

假如想象 提交于 2019-11-27 08:35:44
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 <typename T> void do_swap(T& lhs, T& rhs) { std::swap(lhs, rhs); } By explicitly specifying std:: , it

浅谈Linux服务器究竟设置多大交换分区合适

 ̄綄美尐妖づ 提交于 2019-11-27 07:51:48
年前一客户来电,说他们信息中心机房一台Linux服务器运行缓慢,系统服务出现间歇性停止响应,让过去帮忙处理一下这一问题。 到达现场之后,发现此服务器的物理内存是16G,而最初装机的时候,系统管理人员却只分配了2G的虚拟内存。查看内存的使用状况,物理内存并没有完全耗尽,但虚拟内存已经耗尽,整个系统CPU负载和磁盘IO都非常高。 知道了问题所在是由于交换分区不足导致,那么解决方法就是:将虚拟内存通过虚拟文件的方式增加到8G,系统运行状况明显好转。 其实虚拟内存并不是等到物理内存用尽了才使用的,是否尽量的使用或不使用swap,在内核空间有一个参数控制。 [root@server ~]# cat /proc/sys/vm/swappiness 60 swappiness=0 的时候表示最大限度使用物理内存,然后才是swap空间;swappiness=100 的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。 对于现在动辄几十GB、上百GB物理内存的服务器来说,究竟为其Linux系统设置多大的交换分区合适呢?为此,我引用红帽官方文库里的一段文字进行浅析说明。 目前Red Hat(红帽官方)推荐交换分区的大小应当与系统物理内存的大小保持线性比例关系。 不过在小于2GB物理内存的系统中,交换分区大小应该设置为内存大小的 两倍,如果内存大小多于2GB,