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
19
votes
3 answers

using directive vs using declaration swap in C++

Please refer to the code below: #include namespace N { template class C { public: void SwapWith(C & c) { using namespace std; // (1) //using std::swap; // (2) …
ilovekonoka
  • 303
  • 1
  • 2
  • 6
19
votes
1 answer

Lost colorscheme after recovering swap file

I've searched for some time now and nobody seems to have the problem I do. I've got vim set up to use the colorscheme I like and it was all working perfectly until I opened a file that had a swap. I got the usual message asking if I wanted to delete…
jonthalpy
  • 1,042
  • 8
  • 22
18
votes
2 answers

Why does `basic_ios::swap` only do a partial swap?

C++11 §27.5.4.2/21: void swap(basic_ios& rhs); Effects: The states of *this and rhs shall be exchanged, except that rdbuf() shall return the same value as it returned before the function call, and rhs.rdbuf() shall return the same value as it…
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
18
votes
0 answers

Noexcept-specification in the std::vector's swap function

I noticed that the std::vector container's swap function has a different noexcept-specification than all other containers. Specifically, the function is noexcept if the expression std::allocator_traits::propagate_on_container_swap ||…
LoS
  • 448
  • 1
  • 3
  • 15
18
votes
5 answers

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','foo'),('bar1','foo1'),('barbar','foofoo')] I am…
subixonfire
  • 183
  • 1
  • 1
  • 4
18
votes
3 answers

How to implement swap()?

Possible Duplicate: how to provide a swap function for my class? Every time I think I understand it, I see something that really confuses me. If you want to provide an implementation of swap for your own class, what do you do? The list of…
user541686
  • 205,094
  • 128
  • 528
  • 886
18
votes
3 answers

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 to move part of the process memory to the…
Luca Martinetti
  • 3,396
  • 6
  • 34
  • 49
17
votes
5 answers

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[size - i - 1] ); } } What this method does right…
xian
  • 4,657
  • 5
  • 34
  • 38
17
votes
2 answers

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
  • 14,262
  • 17
  • 77
  • 116
17
votes
1 answer

Is shared_ptr swap thread safe?

Here are some code snippets. std::shared_ptr global(new int(1)); void swapper(int x) { std::shared_ptr sp(new int(x)); global.swap(sp); } Suppose i wanted to call swapper in parallel threads. Would that be threadsafe? I am…
Johannes
  • 6,490
  • 10
  • 59
  • 108
17
votes
1 answer

Swapping Axes in Pandas

What is the most efficient way to swap the axes of a Pandas Dataframe? For example, how could df1 be converted to df2 below? In [2]: import pandas as pd In [3]: df1 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]}) In [4]:…
Clayton
  • 1,525
  • 5
  • 19
  • 35
16
votes
4 answers

C++ string swap character places

is there a way to swap character places in a string? For example if I have "03/02" I need to get "02/03". Any help is appreciated!
RnD
  • 1,019
  • 5
  • 23
  • 49
16
votes
3 answers

Why can't I swap two items in a list in one line?

Why does this not work (values are not swapped): lol = ["test","test2"] lol[lol.index("test")], lol[lol.index("test2")] = lol[lol.index("test2")], lol[lol.index("test")] But this works (values are swapped): i1 = lol.index("test") i2 =…
tinusf
  • 192
  • 1
  • 8
16
votes
8 answers

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, some gave correct results, some didn't. Is…
Pattrick
  • 177
  • 2
  • 3
16
votes
4 answers

Is there an elegant way to swap references in C++?

Sometimes classes are referencing other classes. Implementing std::swap() for such classes cannot be straightforward, because it would lead to swapping of original instances instead of references. The code below illustrates this behavior: #include…
bkxp
  • 1,115
  • 1
  • 12
  • 20