【发布时间】:2021-09-20 19:48:28
【问题描述】:
有这样的结构
class Component {
string name;
int x;
int y;
};
struct Relation_h { // also for Relation_v (h for horizontal, v for vertical)
Component *;
//some data in here;
};
我有一个初始向量 vector<Component> data 和 vector<list<Relation>> relation_h and relation_v
我想sort the vector data分别对应它的x和y然后构造关系
和Component* actually points to the data[i] for i in range(0, data.size())
但问题是the pointer points to is not the original object which I want to access after sorting with x and y
有没有办法处理这种情况?
【问题讨论】:
-
诀窍可能不是尝试对数据本身进行排序。 See this。简而言之,有一个索引数组并根据原始排序标准对其进行排序。然后使用索引数组以排序的方式访问元素。另外,请发帖minimal reproducible example——当你说你正在对数据进行排序时,我们不知道你在做什么。
-
另外,这里不需要指针。你有一个对象向量——与指针有什么关系?也许是因为你没有想到按照我在第一条评论中建议的方式做事?
-
@PaulMcKenzie 感谢您的回答,我想对数据进行排序的原因是,当我构建relation_h 或relation_v 只是为了方便直接push_back 时
-
好吧,我不能轻易地对未显示的代码发布答案。但是根据您的描述,使用索引数组和排序似乎可以轻松实现您的目标。
-
请不要在问题中编辑答案,如果您已经解决了问题,您可以发布自己问题的答案