简体   繁体   English

插入指向std :: set的指针

[英]inserting on a pointer to std::set

Inserting values on Set with and without pointer behaves differently. 在带有和不带有指针的Set上插入值的行为不同。 What is wrong with this code? 此代码有什么问题? The first for loop is insertion on set using pointer and the second is without pointer. 第一个for循环是使用指针插入集合,第二个是不使用指针。 But apart from that everything else is exactly same. 但是除此之外,其他所有内容都完全相同。

#include <iostream>
#include <set>

using namespace std;

typedef struct{
    int local;
    int global;
}Node;
Node CreateNode(int global, int local) {
    Node n; n.local=local; n.global=global;
    return(n);
}
bool compare(Node a, Node b){
    a.global < b.global;
}

int main()
{
   std::pair<std::set<Node>::iterator,bool> itr;

   set<Node,bool(*)(Node,Node)> *graph_set_pointer = new set<Node,bool(*)(Node,Node)>(compare);

   for(int i=10;i>0;--i){
     itr = graph_set_pointer->insert(CreateNode(i,i));
     cout << "global = " << i << " local = " << i ;
     cout << " inserted_global = " << (*itr.first).global << endl;
   }

   cout << "Number of items in pointer set = " << graph_set_pointer->size() << "\n\n";

   set<Node,bool(*)(Node,Node)> graph_set_object(compare);
   for(int i=10;i>0;--i){
     itr = graph_set_object.insert(CreateNode(i,i));
     cout << "global = " << i << " local = " << i ;
     cout << " inserted_global = " << (*itr.first).global << endl;
   }

   cout << "Number of items in non pointer set = " << graph_set_object.size() <<"\n";

   delete graph_set_pointer;

   return 0;
}

Output: 输出:

global = 10 local = 10 inserted_global = 10
global = 9 local = 9 inserted_global = 9
global = 8 local = 8 inserted_global = 8
global = 7 local = 7 inserted_global = 7
global = 6 local = 6 inserted_global = 7
global = 5 local = 5 inserted_global = 7
global = 4 local = 4 inserted_global = 7
global = 3 local = 3 inserted_global = 7
global = 2 local = 2 inserted_global = 7
global = 1 local = 1 inserted_global = 7
Number of items in pointer set = 4

global = 10 local = 10 inserted_global = 10
global = 9 local = 9 inserted_global = 9
global = 8 local = 8 inserted_global = 8
global = 7 local = 7 inserted_global = 7
global = 6 local = 6 inserted_global = 6
global = 5 local = 5 inserted_global = 5
global = 4 local = 4 inserted_global = 4
global = 3 local = 3 inserted_global = 3
global = 2 local = 2 inserted_global = 2
global = 1 local = 1 inserted_global = 1
Number of items in non pointer set = 10

The problem may be the compare() function which doesn't return the result of comparison. 问题可能是compare()函数没有返回比较结果。 Try: 尝试:

bool compare(Node a, Node b){
    return a.global < b.global;
}

In the future you may consider passing -Wall parameter to GCC (4.7.3). 将来,您可以考虑将-Wall参数传递给GCC(4.7.3)。 The compiler will warn you about such mistakes. 编译器将警告您此类错误。 clang (3.2) warns about them by default, VC++ (2010) reports an error. clang(3.2)默认警告它们,VC ++(2010)报告错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM