set STL Standard Template Library's

An associated container

Associative containers based on specific ranking criteria, elements for automatic sorting. Ordering criterion presented in a functional form, used to compare the value of the element (value) or key elements (key). By default, to operator<compare, but you can also provide your own comparison function, the definition of a different sort criteria.

Commonly associated container implemented by a binary tree (binary tree). In a binary tree, each element (node) has a parent node and two child nodes; all the elements in the left subtree than their own small, all the elements in the right subtree than their own big. The main difference lies in the associated container type element and the manner of processing is repeated when the element.

Containers associated with a predefined STL: collection (set), a multiset (multiset), map (Map) and multiple map (multimap).

Second, what is the set?

Collection (set) associated with a container comprising a short sorted objects, does not allow duplicate elements.

A set of organized by a linked list, the insertion and deletion operations on faster than the vector (vector), but some will find slow or adding elements at the end. Embodied using a balanced binary tree data structure of a red-black tree.

Feature

  • set of elements are sorted.
  • set of elements is not repeated.
  • Insert and Delete efficiency map is set higher than the other container sequence, as for associative containers, no need for memory copy and move memory.

Advantages and disadvantages and application scenarios:

Advantages: use a balanced binary tree, easy to find the element, and to maintain the uniqueness of elements, and automatically sorted.

Cons: every time you insert values, we need to adjust the red-black tree, have a certain influence efficiency.
Applicable scene: Find a suitable element is often a cluster and requires the sort of scene.

Third, the definition and initialization

set<int> a; //定义一个int类型的集合a
set<int> b(a); //定义并用集合a初始化集合b
set<int> b(a.begin(), a.end()); //将集合a中的所有元素作为集合b的初始值
//类似的可以使用数组来初始化向量:
int n[] = { 1, 2, 3, 4, 5 };
list<int> a(n, n + 5);              //将数组n的前5个元素作为集合a的初值

Fourth, the basic operation

1) capacity function

  • Container size: a.size (); // returns the number of elements in a
  • Container maximum capacity: a.max_size (); // pre-allocated memory space of different size, returns the number of elements in a memory can accommodate a total of
  • Empty containers sentence: a.empty (); return true // if empty, false otherwise

2) modify the function

  • Insert function: a.insert (X); // time complexity of O (log n)

    Note: set during the time of insertion is not allowed to duplicate key values, if the newly inserted key duplicate with the original key, insert invalid (multiset can be repeated)

  • Delete function: a.erase (parameters);

    Element or parameter may be iterator returns an iterator to the next element, the time complexity is O (log n), the multiset Note s.erase (x) will delete all the value is x

set<int>::iterator it=a.begin();
a.erase(it);
a.erase(3);
  • To delete an element interval [first, end) of: a.erase (First Iterator, Iterator End);
  • Clear all the elements: a.clear ();
  • Two switching elements of the same type of containers: a.swap (B);
set<int>st1;
st1.insert(1);
st1.insert(2);
st1.insert(3);
set<int>st2;
st1.insert(4);
st1.insert(5);
st1.insert(6);
st1.swap(st2);
//st1: 4 5 6
//st2: 1 2 3

3) iterator

  • Iterating pointer: a.begin (); // time complexity is O (1)

  • Iterator end pointer: a.end ();

    Returns a set of tail iterator is known, in the STL section are left and right to open and close, then the end () function is the iterator returned iterator set point at the next position of the largest element, so --s.end () is the set point of the maximum element iterator, the time complexity is O (1)

  • Pointing constant iterating pointer: a.cbegin (); // this means that the pointer can not be referred to modify the contents, but can be modified or otherwise, but also can move the pointer.

  • End of the constant point iterator pointer: a.cend ();

  • Iterator reverse pointer to the last element: a.rbegin ();

  • Iterator reverse pointer to the previous element of the first element: a.rend ();

  • Returns the last key <= keyElem iterator elements: a.lower_bound (keyElem);

  • Returns the first key> keyElem iterator elements: a.upper_bound (keyElem);

//假设 st:1 2 3
    cout << "*(st.begin()): " << *(st.begin()) << endl;
    cout << "*(st.end()): " << *(--st.end()) << endl;
    cout << "*(st.cbegin()): " << *(st.cbegin()) << endl;
    cout << "*(st.cend()): " << *(--st.cend()) << endl;
    cout << "*(st.rbegin()): " << *(st.rbegin()) << endl;
    cout << "*(st.rend()): " << *(--st.rend()) << endl;
    cout << "*(st.lower_bound(2)): " << *(st.lower_bound(2)) << endl;
    cout << "*(st.upper_bound(2)): " << *(st.upper_bound(2)) << endl;
/*
*(st.begin()): 1
*(st.end()): 3
*(st.cbegin()): 1
*(st.cend()): 3
*(st.rbegin()): 3
*(st.rend()): 1
*(st.lower_bound(2)): 2
*(st.upper_bound(2)): 3
*/

4) other functions

  • The number of elements of the statistical key key: a.count (key); // There was 1, no or 0

  • Find function: a.find (the X-);

    Find the value of x in the set of elements, and returns an iterator that element, if not, returns set.end (), time complexity is O (log n)

Five different points, and the sequence of the container

  • set supports only the default constructor and copy constructor, no constructor other payload.
  • set can only use two overloaded functions insert insertion does not support push_back () and push_front () function.
  • set by the iterator can not only to remove the element by element value.
  • set the container does not provide the subscript operator. In order to obtain an element set through the key, you can use the find operation.
  • set STL in the reverse and sort algorithm does not support.

Guess you like

Origin www.cnblogs.com/jiyi-conding/p/11357265.html