从向量中成对删除元素 [英] Remove elements pairwise from vector

查看:89
本文介绍了从向量中成对删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的cpp-ians,


我正在使用结构向量。

vector< meta_segment> meta_segm(2421500);


,结构如下:

struct meta_segment

{

float id;

float num;

浮动平均值;

浮点数;

浮动sumofsquares;

float std;

struct pixel * head;

struct pixel * tail;

struct pixel * edge_head;

struct pixel * edge_tail;

struct segment * segment;

bool full;

};


我运行一个程序:

1.获取向量的随机元素

2.在元素上运行一个函数

3.使用''meta_segm.erase''从列表中删除元素。

并重复此过程,直到向量为空


我遇到的问题是在第2步的功能。这个函数使用

随机元素(A)(步骤1中)并搜索第二个元素

( B)基于不同的标准。所以我的函数实际上运行了两个

元素(A和B),而不是只有一个(A)。

现在,我想在第3步中翻译它并删除来自

向量的A和B.


我知道如何删除A,但我不知道如何删除B. B不是

必然是A'的邻居。我想找到它(基于我用来检测它的

相同的标准),但是我花了很多时间,因为

我将不得不这样做那么每一个元素。


欢迎任何有关如何编程此类问题的建议。


在此先感谢并亲切的问候,

Stef

Dear cpp-ians,

I am working with a vector of structures.
vector <meta_segment> meta_segm (2421500);

and the structure look like:
struct meta_segment
{
float id;
float num;
float mean;
float sum;
float sumofsquares;
float std;
struct pixel * head;
struct pixel * tail;
struct pixel * edge_head;
struct pixel * edge_tail;
struct segment * segment;
bool full;
};

I run a procedure that:
1. takes a random element of the vector
2. runs a function on the element
3. removes the element from the list with the ''meta_segm.erase''.
and repeats this procedure untill the vector is empty

The problem I have is in the function of step 2. This function uses the
random element (A) (out of step 1) and searches for a second element
(B) based on different criteria. So my function runs actually for two
elements (A and B), instead of only one (A).
Now, I want to translate this in step 3 and remove A and B from the
vector.

I know how to remove A, but I have no idea how to remove B. B is not
necessarly A''s neighbour. I thought of searching for it (based on the
same criteria I use to detect it), but I that takes to much time since
I will have to do it for every element then.

Any advice on how to program this kind of problem is welcome.

Thanks in advance and kind regards,
Stef

推荐答案



koperenkogel写道:

koperenkogel wrote:
亲爱的cpp-ians,

我正在使用结构向量。
vector< meta_segment> meta_segm(2421500);

我运行一个程序:
1.采用向量的随机元素
2.在元素上运行一个函数
3。使用''meta_segm.erase''从列表中删除元素。
并重复此过程,直到向量为空

我遇到的问题是在步骤2的功能中。函数使用
随机元素(A)(步骤1中)并根据不同的标准搜索第二个元素
(B)。所以我的函数实际上运行了两个
元素(A和B),而不是只有一个(A)。
现在,我想在步骤3中翻译它并从
vector。

我知道如何删除A,但我不知道如何删除B. B不是必须A'的邻居。我想要搜索它(基于我用来检测它的相同标准),但是我需要花费很多时间
,因为我必须为每个元素执行它。
Dear cpp-ians,

I am working with a vector of structures.
vector <meta_segment> meta_segm (2421500);

I run a procedure that:
1. takes a random element of the vector
2. runs a function on the element
3. removes the element from the list with the ''meta_segm.erase''.
and repeats this procedure untill the vector is empty

The problem I have is in the function of step 2. This function uses the random element (A) (out of step 1) and searches for a second element
(B) based on different criteria. So my function runs actually for two
elements (A and B), instead of only one (A).
Now, I want to translate this in step 3 and remove A and B from the
vector.

I know how to remove A, but I have no idea how to remove B. B is not
necessarly A''s neighbour. I thought of searching for it (based on the
same criteria I use to detect it), but I that takes to much time since I will have to do it for every element then.




在向量中搜索元素是O(N),因为没有

自然顺序。通常,顺序由插入,
的算法决定。这可能不是你想要的顺序

他们。更糟糕的是,从向量中删除随机元素是很昂贵的。


你最好使用std :: set。但是,如果你能确定一个允许你找到每个

元素''B''的订单,这只会工作



为了给你一个很好的答案,我们需要知道如何为给定的A找到一个B




HTH

Michiel Salters



Searching for an element in a vector is O(N), because there is no
natural ordering. Usually, the order is determined by the insertions,
or the last call to std::sort, or another algorithm that changes the
order of elements. That might not be the order in which you would want
them. Worse, removing a random element from a vector is expensive.

You would be better off using a std::set. However, this will work only
if you can determine a single order which allows you to find every
element ''B''.

To really give you a good answer, we''d need to know how you find a B
for a given A.

HTH
Michiel Salters


感谢anwser。我将尝试草拟问题:


我正在编写一个基于多分辨率的图像分割程序

技术。多分辨率分割是从一个像素对象开始的自下而上区域合并

技术。在随后的许多步骤中,较小的图像对象合并为较大的图像对象。在每一步中,

合并了一对相邻的图像对象,这代表了定义的异质性的最小增长。

那么会发生什么。我首先将单个像素合并为一对。之后我开始将这对夫妇分成四人一组,依此类推。一旦对象(=完全)达到了定义的

异质性,它就不再参与

,直到所有对象都满了为止。


在C ++中看起来如何:我有一个向量X(nrow x ncol)和

元素是结构:

struct meta_segment < br $>
{...

struct segment * group;

// Group是指向数组的指针Y [nrow] [ncol]

...};

和数组Y [nrow] [ncol]是一个图像,像素是结构:

struct segment

{...

struct meta_segment * meta_segm;

// Meta-segm是指向矢量的指针

。 ..};


所以X和Y都指向彼此。


现在我想让程序按如下方式工作:

1.从向量中选择一个随机A X

2. A-> X中的组指向Y中的像素a

3.搜索Y中最接近a的像素b。我写了

这个,并在结果中获得了Y的坐标。

4. b-> meta-segm指向X中的B

5.合并A和B(+ a和b):

*从X中删除A和B并将它们放入另一个向量C

in X2(使用push_back)

*替换指针:

- C->组指向a和b

- a-> meta-segm和b-> meta-segm指向C


这样做直到X为空,然后为X2重复该过程

等等。


现在我的问题是:

我可以从X中删除B,因为我只有指针吗?

当我从X中删除A

时,X中元素的其他指针是否恒定?或者他们也改变了吗?


亲切的问候,

Stef

Thanks for anwser. I will try to sketch the problem:

I am writing an image segmentation program based on a multi-resolution
technique. Multi-resolution segmentation is a bottom up region-merging
technique starting with one-pixel objects. In numerous subsequent
steps, smaller image objects are merged into bigger ones. In each step,
that pair of adjacent image objects is merged which stands for the
smallest growth of the defined heterogeneity.
So what happens. I first merge single pixels into couples. Afterwards I
start merging the couples into groups of four and so on. Once a defined
heterogeneity is reached for an object (=full), it does not participate
any longer in the process till all objects are full.

How does this look like in C++: I have a vector X (nrow x ncol) and the
elements are structures :
struct meta_segment
{ ...
struct segment * group;
// Group is a pointer to an array Y[nrow][ncol]
...};
and array Y[nrow][ncol] is an image and the pixels are structures:
struct segment
{ ...
struct meta_segment * meta_segm;
// Meta-segm is a pointer to the vector
...};

So X and Y both point to eachother.

Now I want the program to work as follows:
1. Pick a random A out of vector X
2. A->group in X points to pixels a in Y
3. Search the pixels b in Y that are the closest to a. I have written
this, and get the coordinates of b in Y as result.
4. b->meta-segm points to B in X
5. Merge A and B (+ a and b) by:
* removing A and B out of X and putting them in another vector C
in X2(using push_back)
* replacing the pointers:
- C->group points to a and b
- a->meta-segm and b->meta-segm point to C

This is done till X is empty, and then the process is repeated for X2
and so on.

Now my questions are:
Can I remove B out of X, since I only have a pointer to it?
Are the other pointers to elements in X then constant when I remove A
out of X? Or do they change also?

Kind regards,
Stef


koperenkogel写道:
koperenkogel wrote:

亲爱的cpp-ians,

我正在使用结构向量。
vector< meta_segment> meta_segm(2421500);

和结构看起来像:
struct meta_segment
{
浮动id;
浮点数;
浮动意味着;浮点数;
浮点数sumofsquares;
浮点数;
struct pixel * head;
struct pixel * tail;
struct pixel * edge_head;
struct pixel * edge_tail;
struct segment * segment;
bool full;
};

我运行的程序:
1。采用向量的随机元素
2.在元素上运行一个函数
3.使用''meta_segm.erase''从列表中删除元素。
并重复此过程直到向量是空的

我遇到的问题是在步骤2的功能中。此函数使用
随机元素(A)(步骤1中)并搜索第二个元素< (B)基于不同的标准。所以我的函数实际上运行了两个
元素(A和B),而不是只有一个(A)。
现在,我想在步骤3中翻译它并从
vector。

我知道如何删除A,但我不知道如何删除B. B不是必须A'的邻居。我想要搜索它(基于我用来检测它的相同标准),但是我花了很多时间,因为
我将不得不为每个元素执行它。

Dear cpp-ians,

I am working with a vector of structures.
vector <meta_segment> meta_segm (2421500);

and the structure look like:
struct meta_segment
{
float id;
float num;
float mean;
float sum;
float sumofsquares;
float std;
struct pixel * head;
struct pixel * tail;
struct pixel * edge_head;
struct pixel * edge_tail;
struct segment * segment;
bool full;
};

I run a procedure that:
1. takes a random element of the vector
2. runs a function on the element
3. removes the element from the list with the ''meta_segm.erase''.
and repeats this procedure untill the vector is empty

The problem I have is in the function of step 2. This function uses the
random element (A) (out of step 1) and searches for a second element
(B) based on different criteria. So my function runs actually for two
elements (A and B), instead of only one (A).
Now, I want to translate this in step 3 and remove A and B from the
vector.

I know how to remove A, but I have no idea how to remove B. B is not
necessarly A''s neighbour. I thought of searching for it (based on the
same criteria I use to detect it), but I that takes to much time since
I will have to do it for every element then.




你不能修改你的检测程序,这样它不仅可以计算出有问题的元素,还可以告诉你它在哪里被发现?


或者你可以修改检测功能,这样它就不会返回

元素,但会告诉你元素的位置(它返回

迭代器,如果没有找到则返回end())。那个检测函数的调用者然后需要查找元素本身,但这不是问题,因为它有一个迭代器。


-

Karl Heinz Buchegger
kb **** **@gascad.at


这篇关于从向量中成对删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆