STL -- set and multi-set (set/multiset)

Source: Internet
Author: User

Compared with basic containers, associated containers focus more on fast and efficient data retrieval. These containers retrieve data based on the key value. The key can be a value or a member of the container. Members in this category are sorted in order after initialization. Set and multiset container classes (set and multi-set): # include <set> internal implementation: red/Black Tree insertion/deletion search complexity log (n) the values of the contained elements are unique (map) <multiset> A set of repeated elements is a container, and the values of the elements contained in the set are unique. This is useful when collecting a specific value of data. Elements in the set are arranged in a certain order and used as instances in the set. If you need a key/value pair (pair) to store data, map (also an associated container, which will be discussed later) is a better choice. A collection is organized by a linked list. The insert and delete operations are faster than the vector operations, but the query or addition of elements at the end is somewhat slow. In the collection, all members are arranged. If you insert values 3,123, 2, 65,123, to a set successively, the output result is :,.The difference between a set and a multi-set is that set supports unique key values. The values in a set are specific and only appear once. A copy key can appear in a multi-set, the same value can appear multiple times.Set and multiset template parameters

In some compiler implementations, the third parameter can be omitted. The second parameter uses an appropriate iterator to define a specific relational operator for the key and create an order when traversing the value in the container. The Set iterator is bidirectional and constant. Therefore, the iterator cannot be used to modify the element value. Set defines three constructors: 1. default constructorExplicit set (const Compare & = compare (); for example, set <int, less <int> set1; less <int> is a standard class, used to form a function object in descending order. In ascending order, greater <int> is used. 2. initialize the constructor of the set object by specifying a pre-defined interval.Template <class InputIterator> set (InputIterator, InputIterator, const Compare & = compare (); for example: set <int, less <int> set2 (vector1.begin (), vector1.end ()); 3. copy constructorSet (const set <Key, Compare &>); for example: set <int, less <int> set3 (set2); set container details: header file # include <set> defines the variable set <int> myset; main member functions:
Multiset operation details: header file # include <set> defines variable multiset <int> mymulset; main member functions
Multiset usage:> SS {a. x <SS, ltstr> st;Code1> <(SS _ s) (x = _ s. x) y <x <SS> st;Code2

Multiset example:

<> Main (<> (I =; I <; ++ (<>:: iterator p = set1.begin (); p! = Set1.end (); ++ p) cout <* p <(set1.insert (). second) cout <(set1.find ()! = Set1.end () {cout <(set1.find ()! = Set1.end () {cout <a [] = {, <> + <(multiset <> :: iterator p =. begin (); p! = A. end (); ++ <* p <View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.