Questions tagged [swap]

Changing position of two items.

Act of rearranging the locations of two items such that the first item now occupies the place of the second and the second item now occupies the place of the first.

2503 questions
40
votes
3 answers

Replace Div with another Div

I have this thing I m trying to do. I have a main picture of a map and within that map there are regions. These regions have hot spots on them so you can click them and it will replace the whole map with only the region. (just a simple div swap). I…
Jake
  • 441
  • 1
  • 6
  • 12
40
votes
10 answers

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

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.
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
39
votes
10 answers

Is there a built in swap function in C?

Is there any built in swap function in C which works without using a third variable?
user1149207
39
votes
7 answers

Difference Swapping and Paging

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.
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
39
votes
2 answers

What's the point of iter_swap?

I was just wondering, why would anybody write this: std::iter_swap(i, k); instead of this? std::swap(*i, *k); // saved a few keystrokes! Then I looked into the implementation of iter_swap, and of course it only uses swap instead of std::swap…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
38
votes
4 answers

Is specializing std::swap deprecated now that we have move semantics?

Possible Duplicate: Move semantics == custom swap function obsolete? This is how std::swap looks like in C++11: template void swap(T& x, T& y) { T z = std::move(x); x = std::move(y); y = std::move(z); } Do I still have to…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
38
votes
9 answers

pandas how to swap or reorder columns

I know that there are ways to swap the column order in python pandas. Let say I have this example dataset: import pandas as pd employee = {'EmployeeID' : [0,1,2], 'FirstName' : ['a','b','c'], 'LastName' : ['a','b','c'], …
Yun Tae Hwang
  • 1,249
  • 3
  • 18
  • 30
38
votes
4 answers

Swap slices of Numpy arrays

I love the way python is handling swaps of variables: a, b, = b, a and I would like to use this functionality to swap values between arrays as well, not only one at a time, but a number of them (without using a temp variable). This does not what…
user2082745
  • 381
  • 1
  • 3
  • 5
36
votes
16 answers

How to swap String characters in Java?

How can I swap two characters in a String? For example, "abcde" will become "bacde".
user107023
  • 377
  • 1
  • 3
  • 3
36
votes
5 answers

Swap rows with columns (transposition) of a matrix in javascript

For instance I have a matrix like this: |1 2 3| |4 5 6| |7 8 9| and I need it to convert into a matrix like this: |1 4 7| |2 5 8| |3 6 9| What is the best and optimal way to achieve this goal?
Bakhtiyor
  • 7,198
  • 15
  • 55
  • 77
36
votes
7 answers

Benefits of a swap function?

Browsing through some C++ questions I have often seen comments that a STL-friendly class should implement a swap function (usually as a friend.) Can someone explain what benefits this brings, how the STL fits into this and why this function should…
Rob
  • 76,700
  • 56
  • 158
  • 197
34
votes
5 answers

Move semantics == custom swap function obsolete?

Recently, many questions pop up on how to provide your own swap function. With C++11, std::swap will use std::move and move semantics to swap the given values as fast as possible. This, of course, only works if you provide a move constructor and a…
Xeo
  • 129,499
  • 52
  • 291
  • 397
33
votes
8 answers

Is it possible to swap columns around in a data frame using R?

I have three variables in a data frame and would like to swap the 4 columns around from "dam" "piglet" "fdate" "ssire" to "piglet" "ssire" "dam" "tdate" Is there any way I can do the swapping using R? Any help would be very much…
baz
  • 6,817
  • 11
  • 36
  • 37
32
votes
6 answers

Is it safe to swap two different vectors in C++, using the std::vector::swap method?

Suppose that you have the following code: #include #include #include int main() { std::vector First{"example", "second" , "C++" , "Hello world" }; std::vector Second{"Hello"}; …
user11121949
32
votes
2 answers

Swap two values in a numpy array.

Is there something more efficient than the following code to swap two values of a numpy 1D array? input_seq = arange(64) ix1 = randint(len(input_seq)) ixs2 = randint(len(input_seq)) temp = input_seq[ix2] input_seq[ix2] = input_seq[ix1]…
Gioelelm
  • 2,645
  • 5
  • 30
  • 49