C ++容器问题。 [英] C++ Container question.

查看:85
本文介绍了C ++容器问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我使用" vector< int> V"在我的程序中。如果v包含以下内容:


(前)1 2 3 4 2 5 7 1(结束),


我想删除一些元素,说2,我希望得到的结果为v。是
如下:


(前)1 3 4 5 7 1(结束)


什么我应该使用哪些功能? "擦除"或删除?另一方面,如果我的

向量非常大(比如50000个元素)。什么功能是有效的?

上述行动?


感谢您的帮助。


Pat

Hi,

I use "vector<int> v" in my program. If "v" contains the following:

(front) 1 2 3 4 2 5 7 1 (end),

I want to remove some element, say "2", and I want the resultant "v" to be
as follows:

(front) 1 3 4 5 7 1 (end)

What function should I use? "erase" or "remove"? On the other hand, if my
vector is very larger (say 50000 elements). What function is efficient to do
the above action?

Thanks for your help.

Pat

推荐答案



" Pat" < Pa*@Pat.com>在留言新闻中写道:40 ********** @ rain.i-cable.com ...

"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-cable.com...


我用vector< ; INT> V"在我的程序中。如果v包含以下内容:

(正面)1 2 3 4 2 5 7 1(结束),

我想删除一些元素,说2,然后我想要得到的v
如下:

(正面)1 3 4 5 7 1(结束)

我应该使用什么功能? "擦除"或删除?另一方面,如果我的
向量非常大(比如50000个元素)。什么功能对
有效做上述操作?

感谢您的帮助。

Pat
Hi,

I use "vector<int> v" in my program. If "v" contains the following:

(front) 1 2 3 4 2 5 7 1 (end),

I want to remove some element, say "2", and I want the resultant "v" to be
as follows:

(front) 1 3 4 5 7 1 (end)

What function should I use? "erase" or "remove"? On the other hand, if my
vector is very larger (say 50000 elements). What function is efficient to do the above action?

Thanks for your help.

Pat




remove是一个从容器中删除给定值的函数。在

中,它必须遍历整个容器,寻找任何

匹配值并删除所有这些值。


erase另一方面是vector(和其他)的方法,它在指定的位置删除了
元素。因此它比

删除更有效。但另一方面,如果你要通过

整个向量循环来找到你想要删除的元素,你可能会使用

使用取而代之的是。


所以在你的情况下


std :: remove(v.begin(),v.end(),2) ;


似乎是对的。


john



remove is a function which removes a given value from the container. In
other words it must loop through the entire container looking for any
matching values and removing all of them.

erase on the other hand is a method of vector (and others) which removes
elements at a specified positions. It is therefore much more efficient than
remove. But on the other hand if you are going to have to loop though the
entire vector to find the element(s) you want to erase, you might as well
use remove instead.

So in your case

std::remove(v.begin(), v.end(), 2);

would seem to be right.

john


" Pat" ; < Pa*@Pat.com>在消息新闻中写道:40 ********** @ rain.i-cable.com ...
"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-cable.com...
我使用vector< int> V"在我的程序中。如果v包含以下内容:

(正面)1 2 3 4 2 5 7 1(结束),

我想删除一些元素,说2,然后我想要得到的v
如下:

(正面)1 3 4 5 7 1(结束)

我应该使用什么功能? "擦除"或删除?另一方面,如果我的
向量非常大(比如50000个元素)。什么函数对
有效执行上述操作?
I use "vector<int> v" in my program. If "v" contains the following:

(front) 1 2 3 4 2 5 7 1 (end),

I want to remove some element, say "2", and I want the resultant "v" to be
as follows:

(front) 1 3 4 5 7 1 (end)

What function should I use? "erase" or "remove"? On the other hand, if my
vector is very larger (say 50000 elements). What function is efficient to do the above action?




使用擦除。


vector :: iterator i = find(v.begin(),v.end(),2);

v.erase(i);


任何人都知道v。 erase(v.end())是未定义的还是没有效果?


vector :: erase的运行时间是O(N),因为容器必须复制

元素。


如果你做了很多擦除,请考虑另一种设计:使用std :: list

其中擦除为O(1) ,std :: deque,其中擦除是O(M),其中M是

blocksize,使用remove_if一次擦除多个元素,实际上只是

移动要移除的元素到数组的末尾,然后调用两个参数的

v.erase,在每个对象中创建一个删除标记(允许你轻松撤消删除: )等等



Use erase.

vector::iterator i = find(v.begin(), v.end(), 2);
v.erase(i);

Anyone know if v.erase(v.end()) is undefined or has no effect?

The running time of vector::erase is O(N) because the container has to copy
elements.

If you do lots of erasing, consider an alternative design: use std::list
where erasing is O(1), std::deque where erasing is O(M) where M is the
blocksize, erase many elements at once using remove_if which actually just
moves the elements to be removed to the end of the array and then call the
v.erase of two arguments, create a deleted flag in each object (which allows
you to undo your delete easily :), etc.




" John Harrison" <乔************* @ hotmail.com>在留言中写道

news:c6 ************ @ ID-196037.news.uni-berlin.de ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6************@ID-196037.news.uni-berlin.de...

Pat < Pa*@Pat.com>在留言新闻中写道:40 ********** @ rain.i-cable.com ...

"Pat" <Pa*@Pat.com> wrote in message news:40**********@rain.i-cable.com...


我用vector< ; INT> V"在我的程序中。如果v包含以下内容:

(正面)1 2 3 4 2 5 7 1(结束),

我想删除一些元素,说2,然后我想要得到的v到
如下:

(正面)1 3 4 5 7 1(结束)

我应该使用什么功能? "擦除"或删除?另一方面,如果
我的向量非常大(比如50000个元素)。什么函数是高效的
Hi,

I use "vector<int> v" in my program. If "v" contains the following:

(front) 1 2 3 4 2 5 7 1 (end),

I want to remove some element, say "2", and I want the resultant "v" to be as follows:

(front) 1 3 4 5 7 1 (end)

What function should I use? "erase" or "remove"? On the other hand, if my vector is very larger (say 50000 elements). What function is efficient


上面的动作?

感谢您的帮助。

Pat

remove是一个从容器中删除给定值的函数。换句话说,它必须遍历整个容器,寻找任何匹配值并删除所有这些值。

擦除另一方面是一种矢量方法(和其他)删除指定位置的元素。因此,它比删除更有效
the above action?

Thanks for your help.

Pat

remove is a function which removes a given value from the container. In
other words it must loop through the entire container looking for any
matching values and removing all of them.

erase on the other hand is a method of vector (and others) which removes
elements at a specified positions. It is therefore much more efficient



。但另一方面,如果你不得不通过
整个向量循环找到你想要删除的元素,你可能会使用删除代替。

所以在你的情况下

std :: remove(v.begin(),v.end(),2);

似乎是正确的。

约翰


than remove. But on the other hand if you are going to have to loop though the
entire vector to find the element(s) you want to erase, you might as well
use remove instead.

So in your case

std::remove(v.begin(), v.end(), 2);

would seem to be right.

john




其实没有。我忘记了删除没有删除任何内容的问题,

只是重新排列向量,以便要删除的元素在最后

的向量中然后可以删除。换句话说,你想要的是


v.erase(std :: remove(v.begin(),v.end(),2),v.end());


john



Actually no. I''m forgetting the gotcha the remove does not erase anything,
just rearranges the vector so that the elements to be removed are at the end
of the vector where they can then be erased. In other words what you want is

v.erase(std::remove(v.begin(), v.end(), 2), v.end());

john


这篇关于C ++容器问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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