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
31
votes
4 answers

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 namespace ns { struct foo { foo() : i(0) {} int i; private: foo(const foo&); // not defined, foo& operator=(const foo&); //…
GManNickG
  • 494,350
  • 52
  • 494
  • 543
30
votes
5 answers

Best way to swap variable values in Go?

Is it possible to swap elements like in python? a,b = b,a or do we have to use: temp = a a = b b = temp
Nicky Feller
  • 3,539
  • 9
  • 37
  • 54
26
votes
6 answers

Is there ever a point to swap two variables without using a third?

I know not to use them, but there are techniques to swap two variables without using a third, such as x ^= y; y ^= x; x ^= y; and x = x + y y = x - y x = x - y In class the prof mentioned that these were popular 20 years ago when memory was very…
Celeritas
  • 14,489
  • 36
  • 113
  • 194
25
votes
1 answer

Why was std::swap moved to ?

Why has std::swap been moved to the header for C++11? N3290 C.2.7 says: 17.6.3.2 Effect on original feature: Function swap moved to a different header Rationale: Remove dependency on for swap. Effect on original feature:…
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
25
votes
4 answers

How fast is std::swap for integer types?

STL implements a generic std::swap function to swap 2 values. It can be presented in the following way: template 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…
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
24
votes
2 answers

sort() - No matching function for call to 'swap'

Just spent about an hour trying to figure out why I would get 20 error messages of the type "Semantic issue - no matching function for call to 'swap'" when I try to build the following class (in XCode). test.h #include #include…
Magnus
  • 17,157
  • 19
  • 104
  • 189
24
votes
3 answers

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 guaranteed to be interconvertible (they have the same set…
piedpiper
  • 1,222
  • 3
  • 14
  • 27
22
votes
4 answers

LINUX: How to lock the pages of a process in memory

I have a LINUX server running a process with a large memory footprint (some sort of a database engine). The memory allocated by this process is so large that part of it needs to be swapped (paged) out. What I would like to do is to lock the memory…
pol lol
  • 281
  • 1
  • 2
  • 5
20
votes
6 answers

Swapping ms-sql tables

I want to swap to tables in the best possible manner. I have an IpToCountry table, and I create a new one on a weekly basis according to an external CSV file which I import. The fastest way I've found to make the switch was doing the…
Shay
20
votes
1 answer

Why is swapping multidimensional arrays not noexcept?

I have the following snippet: #include #include int main(int argc, char** argv) { int x[2][3]; int y[2][3]; using std::swap; std::cout << noexcept(swap(x, y)) << "\n"; return 0; } Using GCC 4.9.0, this…
orlp
  • 112,504
  • 36
  • 218
  • 315
20
votes
19 answers

How to swap two string variables in Java without using a third variable

How do I swap two string variables in Java without using a third variable, i.e. the temp variable? String a = "one" String b = "two" String temp = null; temp = a; a = b; b = temp; But here there is a third variable. We need to eliminate the use of…
user1281029
  • 1,513
  • 8
  • 22
  • 32
20
votes
3 answers

Is the copy and swap idiom still useful in C++11

I refer to this question: What is the copy-and-swap idiom? Effectively, the above answer leads to the following implementation: class MyClass { public: friend void swap(MyClass & lhs, MyClass & rhs) noexcept; MyClass() { /* to implement */…
aCuria
  • 6,935
  • 14
  • 53
  • 89
19
votes
4 answers

Swapping rows within the same pandas dataframe

I'm trying to swap the rows within the same DataFrame in pandas. I've tried running a = pd.DataFrame(data = [[1,2],[3,4]], index=range(2), columns = ['A', 'B']) b, c = a.iloc[0], a.iloc[1] a.iloc[0], a.iloc[1] = c, b but I just end up with both the…
Zac
  • 329
  • 1
  • 3
  • 8
19
votes
4 answers

sql swap primary key values

is it possible to swap primary key values between two datasets? If so, how would one do that?
Thomas
  • 2,338
  • 2
  • 23
  • 32
19
votes
13 answers

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) If A is shorter, divide B into Bl and Br such that…
Samarth
  • 431
  • 1
  • 5
  • 13