插入多重集:在该值的第一次出现之前而不是在最后一次出现之后 [英] Inserting in a multiset: before the first occurence of that value instead of after the last occurence

查看:75
本文介绍了插入多重集:在该值的第一次出现之前而不是在最后一次出现之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,multiset在所有相同值的范围的末尾插入一个值.

As the title says multiset inserts a value at the end of the range of all the same values.

(例如:在多集1,2,2,3中插入2使其成为1,2,2,/*new*/ 2,3).

(Ex: Inserting 2 in a multiset 1,2,2,3 makes it 1,2,2,/*new*/ 2,3).

如何获取在所有相同值范围的开头插入的新值?

How do I get the new value inserted at the start of the range of all the same values?

(例如:在多集1,2,2,3中插入2应该使1,/*new*/ 2,2,2,3)

(Ex: Inserting 2 in multiset 1,2,2,3 should make 1,/*new*/ 2,2,2,3)

推荐答案

尝试一下

std::multiset<int> mset { 2,4,5,5,6,6 }; 
int val = 5;
auto it = mset.equal_range ( val ).first; //Find the first occurrence of your target value.  Function will return an iterator

mset.insert ( it, val );  //insert the value using the iterator 

这篇关于插入多重集:在该值的第一次出现之前而不是在最后一次出现之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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