Saturday, July 9, 2016

Iterator swap for vector proxy class

Here is an example of iterator swapping for the vector<bool> proxy class:

Here is the output:

kuyu@castor-ub:~/dkuyu/practice/cpp/iter_swap_bool$ g++ -Wformat=0 iter_swap_bool.cpp && ./a.out
Before swap
a.bytes=0x7ffdd8f29760 a.pos=3 a.bool=1
b.bytes=0x7ffdd8f29760 b.pos=12 b.bool=0
After 1st swap
a.bytes=0x7ffdd8f29760 a.pos=3 a.bool=0
b.bytes=0x7ffdd8f29760 b.pos=12 b.bool=1
After 2nd swap
a.bytes=0x7ffdd8f29760 a.pos=3 a.bool=1
b.bytes=0x7ffdd8f29760 b.pos=12 b.bool=0
kuyu@castor-ub:~/dkuyu/practice/cpp/iter_swap_bool$

Some questions to consider:
  1. What happens if the function starting at line 25 is deleted?
  2. What happens if the function at line 54 returned proxy& instead of only proxy?

No comments:

Post a Comment