在std :: multiset中,如果找到一个元素,则有一个函数或算法可以擦除一个样本(唯一或重复) [英] In std::multiset is there a function or algorithm to erase just one sample (unicate or duplicate) if an element is found

查看:290
本文介绍了在std :: multiset中,如果找到一个元素,则有一个函数或算法可以擦除一个样本(唯一或重复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是重复的,但是我没有找到任何搜索内容: 在std::multiset上调用erase(value)时,将删除具有找到值的所有元素.我唯一想到的解决方案是:

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:

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 ?

推荐答案

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.

这篇关于在std :: multiset中,如果找到一个元素,则有一个函数或算法可以擦除一个样本(唯一或重复)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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