c++ - inserting on a pointer to std::set -
The values behave differently with the point and without setting the value, what is the problem with this code? For loop, the first pointer is inserted on the set and the second occurs without the pointer. But everything else is fine except that.
#include & lt; Iostream & gt; # Include & lt; Set & gt; using namespace std; typedef structure {int local; Int Global; } Node; Node creatoth (int global, int local) {node n; n.local local =; n.global = global; Return (n); } Compare Bull (node A, node B) {a.global & lt; b.global; } Int main () {std :: pair & lt; Std :: set & lt; Node & gt; :: iterator, bull & gt; ITR; Set up & lt; Node, bool (*) (node, node) & gt; * graph_set_pointer = New set & lt; Node, Boole (*) (node, node) & gt; (Comparison); For (int i = 10; i>; - i) {itr = graph_set_pointer-> Insert (CreateNode (i, i)); Cout & lt; & Lt; "Global =" & lt; & Lt; I & lt; & Lt; "Local =" & lt; & Lt; I; Cout & lt; & Lt; "Inserted_global =" & lt; & Lt; (* Eat.fust). Global & lt; & Lt; Endl; } cout & lt; & Lt; "Number of items in pointer set =" & lt; & Lt; graph_set_pointer- & gt; Size () & lt; & Lt; "\ N \ n"; Set up & lt; Node, bool (*) (node, node) & gt; graph_set_object (comparison); For (Int i = 10; i> I; i) {IRR = Graph_set_object.inert (Adnode (I, I)); Cout & lt; & Lt; "Global =" & lt; & Lt; I & lt; & Lt; "Local =" & lt; & Lt; I; Cout & lt; & Lt; "Inserted_global =" & lt; & Lt; (* Eat.fust). Global & lt; & Lt; Endl; } cout & lt; & Lt; "Number of items in non-pointer set =" & lt; & Lt; Graph_set_object.size () & lt; & Lt; "\ N"; Delete graph_set_pointer; Return 0; } Output:
global = 10 local = 10 insert = globals = 10 global = 9 local = 9 entered_global = 9 global = 8 local = 8 Gone_global = 8 global = 7 local = 7 insert = globals = 7 global = 6 local = 6 insert = globals = 7 global = 5 local = 5 entered = global = 7 global = 4 local = 4 entered = globals = 7 global = 3 local = 3 Gone = global = 7 global = 2 local = 2 inserted_global = 7 global = 1 local = 1 inserted_global = 7 points in pointer set Number of = 4 global = 10 local = 10 insert = globals = 10 global = 9 local = 9 combined = globals = 9 global = 8 local = 8 entered_global = 8 global = 7 local = 7 combined = globals = 7 global = 6 local = 6 combined = globals = 6 Global = 5 Local = 5 entered = globals = 5 global = 4 local = 4 entered = globals = 4 global = 3 local = 3 entered = globals = 3 global = 2 local = 2 inserted_global = 2 global = 1 local = 1 inserted_global = 1 No number of objects in non pointer set Or = 10
The problem may be the comparison () function, which results in comparative results Not returning. Try:
Compare bool (node A, node B) {return a.global & lt; b.global; } In the future you may want to consider passing- for the GCC (4.7.3) the wall parameter compiler will warn you about such mistakes lie (3.2) by default Regards, VC ++ (2010) reports an error.
Comments
Post a Comment