stl--Sets and multiple sets (Set/multiset) _c

Source: Internet
Author: User
Compared to basic containers, associative containers are more focused on the ability to retrieve data quickly and efficiently. These containers retrieve data based on a key value (key), which can be a value or a member of a container. The members of this class are ordered in a certain order after initialization. This article address: http://www.cnblogs.com/archimedes/p/cpp-set-multiset.html, reprint Please indicate the source address. Set and Multiset container classes (sets and multiple sets): #include <set> <set> internal IT implementation: Red black tree Insert Delete Find complexity log (n) the value of the element contained in it is unique (map) <multiset > allows a repeating element collection (set) to be a container in which the values of the elements contained are unique. This is useful when collecting the specific value of a data. The elements in the collection are arranged in a certain order and are used as instances in the collection. If you need a key/value pair (pair) to store the data, the map (also an associative container, which will be mentioned later) is a better choice. A collection is organized by a linked list that is faster than a vector (vectors) on insert and delete operations, but is somewhat slower to find or add elements at the end. In the collection, all of the members are arranged well. If one is inserted into a single set: 12,2,3,123,5,65 the set is output: The difference between the 2,3,5,12,65,123 set and episodes (Multiset) is that the set supports unique key values, the values in set are specific, and only one occurrence , and the replica key can appear in Multiset, and the same value can appear multiple times. Set and multiset template Parameters Template<class Key, class compare, class allocator=allocator> The first parameter key is the type of key stored The second parameter is the type of the comparison function defined for the sort value The third parameter is the type of storage assignment that is implemented. In some compiler implementations, the third parameter can be omitted. The second parameter defines a specific relational operator for the key using the appropriate form of iterator, and is used to establish the order when traversing the value in the container. The iterator of the set is bidirectional and constant, so the iterator cannot modify the value of the element when it is in use. Set defines three constructors: 1, default constructor explicit set (const compare&=compare ()); such as:set<int,less<int> > Set1; Less<int> is a standard class that forms a descending sort function object. Ascending order is made with greater<int>. 2. Initializes the constructor of the set object by specifying a predefined interval template<class inputiterator> set (Inputiterator, inputiterator, const Compare &=compare ()); such as: Set<int,less<int> >set2 (Vector1.begin (), Vector1.end ()); 3. Copy constructor set (const set<key,compare&>); such as: Set<int, Less<int>>set3 (Set2);   Set Container Detailed: header file   #include <set> definition variable   set <int> myset; Primary member function: Myset.insert (elem)   inserting data into the collection, if it already exists, not inserting myset.erase (elem)       Deleting the values in the collection equals Elem Elements Myset.find (elem)        lookup values equal to elem elements, if found to return the iterator pointing to Elem, otherwise return end (), Myset.clear (   Clears all data in the collection Myset.size ()   Returns the number of data in the collection Multiset operations: Header files   #include <set> define variables   multiset <int& Gt Mymulset; The primary member function Mymulset.insert (elem)   inserts data into multiple collections, Mymulset.erase (elem)   deletes all elements in multiple collections that are equal to Elem, or returns 0 if the deletion successfully returns the number of deletes Mymulset.count (Elem)   Returns the number of times a data elem occurs in a multiple collection Multiset usage:
#include <set>
using namespace std;

struct SS {int x,y;};
struct LTSTR { 
    bool operator () (SS A, SS B)
    {return a.x < b.x;}  
};
int main () 
{ 
    set <ss, ltstr> st;//The SS elements in St are sorted by x from small to large ...
}
#include <set>
using namespace std;
struct SS {
    int x,y;
    BOOL operator < (struct SS _s) const {
        if (x = = _s.x) return y < _s.y;
        return x < _s.x;
    }
;
int main () 
{ 
    set <SS> st;//ST SS elements are sorted by x from small to large ...
}

Multiset Examples:

#include <iostream> #include <set> using namespace std;
    int main (void) {set<int> set1;
    for (int i = 0; i < ++i) Set1.insert (i);
    for (Set<int>::iterator p = set1.begin ();p!= set1.end (); ++p) cout << *p << "";  if (Set1.insert (3). Second)//insert 3 into SET1/Insert Success Set1.insert (3). Second returns True, otherwise returns false///In this case, the insertion will fail cout
    << "Set insert Success";
    else cout << "set insert Failed";
    if (Set1.find (3)!= set1.end ()) {//Lookup element 3 cout << "Find it ..." << Endl;
    else {cout << "not find it ..." << Endl;
    } if (Set1.find)!= set1.end ()) {//Lookup element cout << "Find it ..." << Endl;
    else {cout << "not find it ..." << Endl;
    int a[] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
    Multiset<int> A;
    A.insert (Set1.begin (), Set1.end ());
    A.insert (A, A + 10);
    cout << Endl; for (Multiset<int>:: Iterator P = a.begin ();p!= a.end (); ++p) cout << *p << "";
    Cin.get ();
return 0; }

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.