C++ functor - unexpected behaviour? -
I wrote this program, which is like some entries using a fun doctor:
< Pre> # include & lt; Iostream & gt; #includes & lt; list & gt; # Include & lt; Set & gt; using namespace std; Struct IntSorter {Unsigned integer comparison; Integer () {std :: cout & lt; & Lt; "New Antostroaster" & lt; & Lt; Std :: endl; Comparison = 0; } Bull Operator () (Contract and A, Continuit and B) {std :: cout & lt; & Lt; "C is" & lt; & Lt; Comparison & lt; & Lt; Std :: endl; ++ Comparison; Std :: cout & lt; & Lt; "C is now" & lt; & Lt; Comparison & lt; & Lt; Std :: endl; A & lt; B; }}; Int main (int argc, char * argv []) {list & lt; Int & gt; My list; My_list.push_back (4); My_list.push_back (3); My_list.push_back (5); My_list.push_back (1); Intecer s; My_list.sort (s); (List & lt; int & gt; :: iterator = my_list.begin (); it! = My_list.end (); this ++) {std :: cout & lt; & Lt; * This is & lt; & Lt; Std :: endl; } Return 0; } Sorting works fine, but the share of the count of numbers gives me a result which I did not expect: ./prog -a-B-C-D is the newest current C is 0C is now 1c C is 1C is now C is 1c is 1C Now 2C is 2c now 3 1 3 4 5
I was thinking that the structure will be reused, will be compared and the number of accumulation will be stored. However, this appears to be a copy, or any other transaction has been printed as numbers 1,1, 2, 2 3, instead of 1,2,3,4,5 what am I doing wrong?
You are not really doing anything wrong This problem is completely in your hopes - sorter Values are passed, so you make a copy when passing it. The sort function is also free to create more copies (such as a recursive sort, possibly sending a copy for each recurring invitation).
Here's a simple lesson: To create and / or copy any such item to cheap .
Comments
Post a Comment