插入多重集:在该值第一次出现之前而不是在最后一次出现之后
Inserting in a multiset: before the first occurence of that value instead of after the last occurence
正如标题所说,multiset 在所有相同值的范围末尾插入一个值。
(例如:在多重集 1,2,2,3
中插入 2 使其成为 1,2,2,/*new*/ 2,3
)。
如何在所有相同值范围的开头插入新值?
(例如:在多重集 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
将迭代器获取到下限。
正如标题所说,multiset 在所有相同值的范围末尾插入一个值。
(例如:在多重集 1,2,2,3
中插入 2 使其成为 1,2,2,/*new*/ 2,3
)。
如何在所有相同值范围的开头插入新值?
(例如:在多重集 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
将迭代器获取到下限。