插入多音符:在该值的第一次发生之前,而不是在上次发生之后

Inserting in a multiset: before the first occurence of that value instead of after the last occurence

本文关键字:之后 音符 第一次 插入      更新时间:2023-10-16

标题所示多空心在所有相同值的范围内插入一个值。

(ex:在MultiSet 1,2,2,3中插入2使其成为1,2,2,/*new*/ 2,3(。

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

(ex:在MultiSet 1,2,2,3中插入2应该使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 

使用函数insert(iterator hint, const value_type& value)代替insert(const value_type& value)。根据文档,这将在hint之前插入。您可以使用 std::multiset::equal_range将迭代器置于下限。