Questions tagged [swap]

Changing position of two items.

swap
Filter by
Sorted by
Tagged with
452 votes
8 answers
328k views

Is there a standardized method to swap two variables in Python?

In Python, I've seen two variable values swapped using this syntax: left, right = right, left Is this considered the standard way to swap two variable values or is there some other means by which ...
WilliamKF's user avatar
  • 42.1k
166 votes
2 answers
28k views

What can I do with a moved-from object?

Does the standard define precisely what I can do with an object once it has been moved from? I used to think that all you can do with a moved-from object is do destruct it, but that would not be ...
fredoverflow's user avatar
117 votes
15 answers
479k views

How to convert big endian to little endian in C without using library functions?

I need to write a function to convert a big endian integer to a little endian integer in C. I cannot use any library function. How would I do this?
Alex Xander's user avatar
  • 3,971
190 votes
22 answers
139k views

Is there a native jQuery function to switch elements?

Can I easily swap two elements with jQuery? I'm looking to do this with one line if possible. I have a select element and I have two buttons to move up or down the options, and I already have the ...
juan's user avatar
  • 81.1k
50 votes
8 answers
114k views

Java method to swap primitives

how do I make my swap function in java if there is no method by which we can pass by reference? Could somebody give me a code? swap(int a, int b) { int temp = a; a = b; b = temp; } ...
higherDefender's user avatar
36 votes
5 answers
68k views

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

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 ...
Bakhtiyor's user avatar
  • 7,238
7 votes
5 answers
2k views

Examples of when a bitwise swap() is a bad idea?

You're not supposed to treat object pointers as pointers to raw binary data in OOP languages, including C++. Objects are "more than" their representation. So, for example, swaping two objects by ...
user541686's user avatar
  • 208k
101 votes
3 answers
32k views

how to provide a swap function for my class?

What is the proper way to enable my swap in STL algorithms? 1) Member swap. Does std::swap use SFINAE trick to use the member swap. 2) Free standing swap in the same namespace. 3) Partial ...
pic11's user avatar
  • 14.7k
233 votes
22 answers
241k views

How to swap two variables in JavaScript

I have this two variables: var a = 1, b = 2; My question is how to swap them? Only this variables, not any objects.
Lukas's user avatar
  • 7,564
125 votes
30 answers
235k views

Swap two variables without using a temporary variable

I'd like to be able to swap two variables without the use of a temporary variable in C#. Can this be done? decimal startAngle = Convert.ToDecimal(159.9); decimal stopAngle = Convert.ToDecimal(355.87);...
Sreedhar's user avatar
  • 29.7k
80 votes
13 answers
318k views

Efficient swapping of elements of an array in Java

I am wondering if there is a more efficient way of swapping two elements in an Array, than doing something like this: String temp = arr[1]; arr[1] = arr[2]; arr[2] = temp; Well, this is obviously ...
Robin's user avatar
  • 3,602
38 votes
4 answers
13k views

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 (...
user2082745's user avatar
172 votes
8 answers
23k views

Understand Python swapping: why is a, b = b, a not always equivalent to b, a = a, b?

As we all know, the pythonic way to swap the values of two items a and b is a, b = b, a and it should be equivalent to b, a = a, b However, today when I was working on some code, I accidentally ...
Shaun Han's user avatar
  • 2,764
59 votes
19 answers
223k views

How to write a basic swap function in Java [duplicate]

I am new to java. How to write the java equivalent of the following C code. void Swap(int *p, int *q) { int temp; temp = *p; *p = *q; *q = temp; }
Melinda's user avatar
  • 607
54 votes
13 answers
51k views

Is it possible to write swap method in Java? [duplicate]

Here is the question: write a method that swaps two variables. These two variables should be primitives. It doesn't need to be generic e.g. two int variables. Is there a way?!
Khaled Alshaya's user avatar
101 votes
6 answers
139k views

Swap two items in List<T>

Is there a LINQ way to swap the position of two items inside a List<T>?
Tony The Lion's user avatar
84 votes
2 answers
46k views

How does the standard library implement std::swap?

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 ...
Maximilian Mordig's user avatar
36 votes
5 answers
7k views

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, ...
Xeo's user avatar
  • 131k
42 votes
3 answers
141k views

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 ...
Jake's user avatar
  • 461
14 votes
2 answers
4k views

Why is this statement not working in java x ^= y ^= x ^= y;

int x=1; int y=2; x ^= y ^= x ^= y; I am expecting the values to be swapped.But it gives x=0 and y=1. when i tried in C language it gives the correct result.
sadananda salam's user avatar
7 votes
3 answers
31k views

Swap nodes in a singly-linked list

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 ...
CodeRat's user avatar
  • 73
83 votes
5 answers
247k views

C++ trying to swap values in a vector

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)...
user782311's user avatar
20 votes
13 answers
70k views

Python Array Rotation

So I am implementing a block swap algorithm in python. The algorithm I am following is this: Initialize A = arr[0..d-1] and B = arr[d..n-1] 1) Do following until size of A is equal to size of B a) ...
Samarth's user avatar
  • 441
19 votes
5 answers
34k views

Swap values in a tuple/list inside a list in python?

I have a list of tuples like this: [('foo','bar'),('foo1','bar1'),('foofoo','barbar')] What is the fastest way in python (running on a very low cpu/ram machine) to swap values like this... [('bar','...
subixonfire's user avatar
96 votes
8 answers
129k views

How to move specific item in array list to the first item

For example : A list A B C D E Given C , Switch to C A B D E Notice that the array size will change, some items may removed in run times Collections.swap(url, url.indexOf(itemToMove), 0); This ...
user782104's user avatar
  • 13.4k
92 votes
20 answers
84k views

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

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 ...
Taylor's user avatar
  • 1,710
58 votes
6 answers
17k views

iter_swap() versus swap() -- what's the difference?

MSDN says: swap should be used in preference to iter_swap, which was included in the C++ Standard for backward compatibility. But comp.std.c++ says: Most STL algorithms operate on iterator ...
user541686's user avatar
  • 208k
48 votes
7 answers
27k views

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

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 ...
sanity's user avatar
  • 35.6k
31 votes
4 answers
5k views

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

Background Consider the following code: #include <utility> namespace ns { struct foo { foo() : i(0) {} int i; private: foo(const foo&); // not ...
GManNickG's user avatar
  • 499k
19 votes
4 answers
16k views

sql swap primary key values

is it possible to swap primary key values between two datasets? If so, how would one do that?
Thomas's user avatar
  • 2,347
16 votes
8 answers
5k views

Potential Problem in "Swapping values of two variables without using a third variable"

I recently came along this method for swapping the values of two variables without using a third variable. a^=b^=a^=b But when I tried the above code on different compilers, I got different results, ...
Pattrick 's user avatar
15 votes
7 answers
6k views

Overloading global swap for user-defined type

The C++ standard prohibits declaring types or defining anything in namespace std, but it does allow you to specialize standard STL templates for user-defined types. Usually, when I want to specialize ...
Channel72's user avatar
  • 183
15 votes
8 answers
19k views

Can I swap colors in image using GD library in PHP?

I got the image like this (it's a graph): (source: kitconet.com) I want to change the colours, so the white is black, the graph line is light blue, etc.. is it possible to achieve with GD and PHP?
Skuta's user avatar
  • 5,852
6 votes
9 answers
54k views

Swapping Nodes on a single linked list

I am trying to make a swapNode function that can take any two nodes and swap them. I've made an algorithm that works if they're at least 2 nodes away, but I can't seem to come up with an algorithm ...
Reti's user avatar
  • 609
6 votes
6 answers
6k views

How do you swap two integer variables without using any if conditions, casting, or additional variables? [closed]

There are two integer variables. Can you swap those integer variables without using any if conditions, without casting, and without using additional variables? For example: int a = 10; int b = 5; a &...
dotnet lover's user avatar
77 votes
4 answers
5k views

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

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)...
Javed Akram's user avatar
  • 15.2k
57 votes
10 answers
9k views

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

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); ...
ashfaque's user avatar
  • 522
46 votes
3 answers
43k views

How do I swap tensor's axes in TensorFlow?

I have a tensor of shape (30, 116, 10), and I want to swap the first two dimensions, so that I have a tensor of shape (116, 30, 10) I saw that numpy as such a function implemented (np.swapaxes) and I ...
Alexis Rosuel's user avatar
38 votes
4 answers
21k views

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

Possible Duplicate: Move semantics == custom swap function obsolete? This is how std::swap looks like in C++11: template<typename T> void swap(T& x, T& y) { T z = std::move(x); ...
fredoverflow's user avatar
37 votes
2 answers
66k views

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 = ...
Gioelelm's user avatar
  • 2,725
26 votes
3 answers
15k views

Finding the minimum number of swaps to convert one string to another, where the strings may have repeated characters

I was looking through a programming question, when the following question suddenly seemed related. How do you convert a string to another string using as few swaps as follows. The strings are ...
piedpiper's user avatar
  • 1,340
8 votes
1 answer
886 views

Why is the XOR swap optimized into a normal swap using the MOV instruction?

While testing things around Compiler Explorer, I tried out the following overflow-free function for calculating the average of 2 unsigned 32-bit integers: uint32_t average_1(uint32_t a, uint32_t b) { ...
Uduru's user avatar
  • 509
8 votes
4 answers
26k views

How to "shuffle" an array? [duplicate]

I am having a tough time trying to create a "shuffleDeck()" method. What I am trying to do is create a method that will take an array parameter (which will be the deck of cards) shuffle the cards, ...
user2690972's user avatar
3 votes
4 answers
3k views

Why don't people use xor swaps? [closed]

I read on a site that using xor swaps is fast because it doesn't use a temporary variable. Here's an example: #include <stdio.h> int main(void) { int a=234,b=789; b=b^a; a=...
autistic's user avatar
  • 15.5k
36 votes
16 answers
134k views

How to swap String characters in Java?

How can I swap two characters in a String? For example, "abcde" will become "bacde".
user107023's user avatar
33 votes
8 answers
78k views

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 ...
baz's user avatar
  • 7,007
18 votes
3 answers
12k views

Can I tell Windows not to swap out a particular processes’ memory?

Is there a way to tell Windows that it shouldn't swap out a particular processes' memory to disk? Its a .Net windows service with fairly large memory usage. I got lot of physical RAM but the OS seems ...
Luca Martinetti's user avatar
17 votes
5 answers
26k views

Is there a better way to reverse an array of bytes in memory?

typedef unsigned char Byte; ... void ReverseBytes( void *start, int size ) { Byte *buffer = (Byte *)(start); for( int i = 0; i < size / 2; i++ ) { std::swap( buffer[i], buffer[...
xian's user avatar
  • 4,665
17 votes
2 answers
17k views

how to update swap values of two rows with single query

Is there a query with which i can exchange the values of two rows with single query?
S L's user avatar
  • 14.3k
16 votes
7 answers
44k views

Swap arrays by using pointers in C++

I have two arrays of pointers to doubles that I need to swap. Rather than just copy the data within the arrays, it would be more efficient just to swap the pointers to the arrays. I was always under ...
thornate's user avatar
  • 5,022

1
2 3 4 5 6