插入多重集:在该值的第一次出现之前而不是在最后一次出现之后

gst*_*502 2 c++ stl multiset

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

(例如:在一个多重插入2 1,2,2,3使得它1,2,2,/*new*/ 2,3)。

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

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

Wal*_*ari 5

尝试这个

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 
Run Code Online (Sandbox Code Playgroud)