STL_multiset

STL容器操作详解

构造函数

1.int size() const:返回容器元素个数
2.bool empty() const:判断容器是否为空,若返回true,表明容器已空
3.insert函数

pair<iterator,bool> insert( x):插入元素x

iterator insert(iterator it,x):在迭代器it处插入元素x

void insert(const value_type *first,const value_type *last):插入[first, last)之间元素

4.delete函数

iterator erase(iterator it):删除迭代器指针it处元素

iterator erase(iterator first,iterator last):删除[first, last)之间元素

size_type erase(const Key& key):删除元素值等于key的元素

5.iterator begin():返回首元素的迭代器指针

6.iterator end():返回尾元素的迭代器指针
7.int count(const Key& key) const:返回容器中元素等于key的元素的个数
8.swap

void swap(set& s):交换集合元素
void swap(multiset& s):交换多集合元素  
### C++ STL `unordered_multiset` vs `set`: Differences and Usage #### Key Characteristics Comparison Both `std::set` and `std::unordered_multiset` are associative containers that store elements based on their key values. However, there exist fundamental differences between them: - **Ordering**: Elements within a `std::set` are sorted according to the keys following a specific order defined by comparison objects or default less-than operators. Conversely, `std::unordered_multiset` does not maintain any particular ordering among its elements because it uses hashing mechanisms for element placement[^4]. - **Uniqueness Constraint**: In a `std::set`, each value must be unique—no duplicate entries allowed. On the contrary, `std::unordered_multiset` permits multiple occurrences of identical elements due to being designed specifically around allowing duplicates. - **Performance Implications**: Due to maintaining internal balance through red-black trees typically employed under-the-hood implementation details, operations like insertion/deletion/search tend generally slower compared against hash tables utilized inside unordered sets/multisets which offer average constant-time complexity O(1) performance characteristics assuming good distribution properties from chosen hash functions. #### Insertion Behavior For inserting new items into collections, both types provide similar interfaces yet differ slightly regarding what information gets returned upon successful addition: - When adding an item via `std::set::insert()`, one receives back either just an iterator pointing towards where the newly added member resides now alongside boolean flag indicating whether operation succeeded without replacing pre-existing entry having same key. - Meanwhile, calling `std::unordered_multiset::insert()` yields solely an iterator referencing location occupied post-insert regardless if object already existed beforehand since multiplicity supported inherently here[^1]. #### Code Demonstrations Below showcases how these two structures behave differently when performing insertions along with iterating over contents afterward: ```cpp #include <iostream> #include <set> #include <unordered_set> int main(){ // Example demonstrating set behavior std::set<int> mySet; auto result_pair = mySet.insert(5); if (!result_pair.second){ std::cout << "Element 5 was already present." << std::endl; } // Iterating over ordered collection for(const int& elem : mySet){ std::cout << elem << ' '; } // Now showing off unordered_multiset functionality std::unordered_multiset<int> multiSet; auto pos = multiSet.insert(7); // Only returns position // Display potentially unsorted sequence possibly containing repeats for(auto iter=multiSet.begin();iter!=multiSet.end();++iter){ std::cout<<*iter<<" "; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值