C++ 在 std::multiset 中,如果找到一个元素,是否有一个函数或算法可以只擦除一个样本(单一或重复)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9167745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In std::multiset is there a function or algorithm to erase just one sample (unicate or duplicate) if an element is found
提问by Martin
Perhaps this is a duplicate but I did not find anything searching:
When erase(value)
is called on std::multiset
all elements with the value found are deleted. The only solution I could think of is:
也许这是一个重复,但我没有找到任何搜索: Whenerase(value)
调用std::multiset
所有具有找到的值的元素被删除。我能想到的唯一解决方案是:
std::multiset<int>::iterator hit(mySet.find(5));
if (hit!= mySet.end()) mySet.erase(hit);
This is ok but I thought there might be better. Any Ideas ?
这没关系,但我认为可能有更好的。有任何想法吗 ?
回答by user2251346
auto itr = my_multiset.find(value);
if(itr!=my_multiset.end()){
my_multiset.erase(itr);
}
I would imagine there is a cleaner way of accomplishing the same. But this gets the job done.
我想有一种更干净的方法来完成同样的事情。但这可以完成工作。
回答by Strange
Try this one:
试试这个:
multiset<int> s;
s.erase(s.lower_bound(value));
As long as you can ensure that the value
exits in the set. That works.
只要你能保证value
在设置的出口。那个有效。
回答by varun kunchakuri
if(my_multiset.find(key)!=my_multiset.end())
my_multiset.erase(my_multiset.equal_range(key).first);
This is the best way i can think of to remove a single instance in a multiset in c++
这是我能想到的在 C++ 中删除多集中单个实例的最佳方法
回答by Arun
I would try the following.
我会尝试以下。
First call equal_range()
to find the range of elements that equal to the key.
首先调用equal_range()
查找等于键的元素范围。
If the returned range is non-empty, then erase()
a range of elements (i.e. the erase()
which takes two iterators) where:
如果返回的范围是非空的,那么erase()
一个元素范围(即erase()
需要两个迭代器的),其中:
the first argument is the iterator to the 2nd element in the returned range (i.e. one past
.first
returned) andthe second argument as the returned range pair iterator's
.second
one.
第一个参数是返回范围内第二个元素的迭代器(即
.first
返回的过去)和第二个参数作为返回的范围对迭代器的
.second
一个。
Edit after reading templatetypedef's (Thanks!) comment:
阅读templatetypedef的(谢谢!)评论后编辑:
If one (as opposed to all) duplicate is supposed to be removed: If the pair returned by equal_range()
has at least two elements, then erase()
the first element by passing the the .first of the returned pair to single iterator version of the erase()
:
如果应该删除一个(而不是所有)重复项:如果 返回的对equal_range()
至少有两个元素,则erase()
第一个元素通过将返回对的 .first 传递给 的单个迭代器版本erase()
:
Pseudo-code:
伪代码:
pair<iterator, iterator> pit = mymultiset.equal_range( key );
if( distance( pit.first, pit.second ) >= 2 ) {
mymultiset.erase( pit.first );
}
回答by Yukty
This worked for me:
这对我有用:
multi_set.erase(multi_set.find(val));
if val exists in the multi-set.
如果 val 存在于多集中。
回答by Dipen Dadhaniya
We can do something like this:
我们可以这样做:
multiset<int>::iterator it, it1;
it = myset.find(value);
it1 = it;
it1++;
myset.erase (it, it1);
回答by user3075328
In fact, the correct answer is:
事实上,正确答案是:
my_multiset.erase(my_multiset.find(value));
my_multiset.erase(my_multiset.find(value));