迭代 std::set (c++) 时出现问题
我一整天都遇到设置问题。有些我可以解决,有些则不能。这个问题从早上开始就困扰着我,我已经失去了耐心。请帮忙,强大的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您发生这种情况的原因有几种可能:
clear()
它?也许您想存储指向向量的指针,否则编译器会因为它不知道如何比较两个向量而给您一个错误。为什么不尝试实施比较< /a> 直接函数并存储向量 ?这样你的工作就会变得更加容易并且更不容易出错。
另外你现在遇到的问题是集合中的元素似乎没有排序..事实上它们是按指针值排序的,而不是按 X 和 Y 排序(我想这就是你想要的)
There are a few possibilities as to why this is happening to you:
clear()
it between reuses ?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)
您有一组指向向量的指针。在 **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.