迭代 std::set (c++) 时出现问题

发布于 2024-11-27 19:22:02 字数 900 浏览 2 评论 0原文

我一整天都遇到设置问题。有些我可以解决,有些则不能。这个问题从早上开始就困扰着我,我已经失去了耐心。请帮忙,强大的 stackoverflow!

所以,我有一个包含我的自定义对象的集合,它称为“vect”并且基于 eigen::matrix。这意味着,我使用 [] 运算符从 vects 获取值。

set<vect*> *tvps=getTheSet();

for (set<vect*>::iterator iter = tvps->begin(); iter != tvps->end(); ++iter)
{
    vect v= **iter;   // Don't really know why two asterisks,
                      // but my compiler would complain
    int x=v[0];
    int y=v[1];
    doStuffWith( v[0],v[1]);

}

现在,这将编译并运行以及一切。但我从迭代器得到的值有 30% 是垃圾:

x: 110   y: 90
x: 230   y: 130
x: 250   y: 100
x: 230   y: 130
x: 110   y: 290
x: 140   y: 260
x: 180   y: 280
x: 150   y: 210
x: -2147483648   y: 0
x: 180   y: 280
x: 170   y: 230
x: 240   y: 270
x: -2147483648   y: 0
x: -429917536   y: 0
x: 0   y: -2147483648

我在集合组合在一起时检查了哪些值被放入。只有 10 到 300 之间的值。我怎么会在里面找到其他人呢?我搞砸了迭代器吗?

I am having problems with sets all day. Some I can solve, others not. This one is bugging me since the morning, and I have run out of patience. Please help, mighty stackoverflow!

So, I have a set which contains a custom object of mine, its called "vect" and is based on eigen::matrix. That means, I get values from vects with the [] operator.

set<vect*> *tvps=getTheSet();

for (set<vect*>::iterator iter = tvps->begin(); iter != tvps->end(); ++iter)
{
    vect v= **iter;   // Don't really know why two asterisks,
                      // but my compiler would complain
    int x=v[0];
    int y=v[1];
    doStuffWith( v[0],v[1]);

}

Now, this will compile and run and everything. But the values I get from the iterator are 30% trash:

x: 110   y: 90
x: 230   y: 130
x: 250   y: 100
x: 230   y: 130
x: 110   y: 290
x: 140   y: 260
x: 180   y: 280
x: 150   y: 210
x: -2147483648   y: 0
x: 180   y: 280
x: 170   y: 230
x: 240   y: 270
x: -2147483648   y: 0
x: -429917536   y: 0
x: 0   y: -2147483648

I checked at the point where the set was put together which values where put in. Only the ones between 10 and 300.. as intended. How comes I find others in it? Did I screw up with the iterator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉睡月亮 2024-12-04 19:22:02

对于您发生这种情况的原因有几种可能:

  • 您是否重复使用该套件。如果是,您在重用之间是否clear()它?
  • 您正在存储指向集合中向量的指针,您是否正在更改集合内的向量?

也许您想存储指向向量的指针,否则编译器会因为它不知道如何比较两个向量而给您一个错误。为什么不尝试实施比较< /a> 直接函数并存储向量 ?这样你的工作就会变得更加容易并且更不容易出错。

另外你现在遇到的问题是集合中的元素似乎没有排序..事实上它们是按指针值排序的,而不是按 X 和 Y 排序(我想这就是你想要的)

There are a few possibilities as to why this is happening to you:

  • Are you reusing the set. If yes, do you clear() it between reuses ?
  • You are storing pointers to vectors in the set, are you changing the vectors inside the set ?

Perhaps you want to store the pointer to the vector because otherwise the compiler gives you an error due to the fact that it doesn't know how to compare two vectors. Why don't you try to implement the comparison function and store vectors directly ? that way your work would be a lot easier and much less error prone.

Also the problem you have now is that the elements in the set don't seem to be ordered.. in fact they are ordered by the pointer value, and not by X and Y (and I suppose that's what you want)

破晓 2024-12-04 19:22:02

您有一组指向向量的指针。在 **iter 中,首先取消引用迭代器,然后将指针放置在内部。

保留指针也可能导致您的问题:如果您释放或忘记初始化其中一些指针,结果将是垃圾。

You have a set of pointers to vectors. In **iter first you dereference iterator, then pointer laying inside.

Keeping pointers can also be causing your problem: if you freed or forgot to initialize some of them, result would be rubbish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文