1. About set

C++ STL The reason why it has been widely praised , It's also used by a lot of people , It's not just about offering things like vector, string, list And other convenient containers , what's more STL It encapsulates many complex data structure algorithms and a large number of common data structure operations .vector Package array ,list Encapsulates the linked list ,map and set Encapsulating binary trees and so on , When encapsulating these data structures ,STL According to the usage habits of programmers , Common operations provided as member functions , Such as : Insert 、 Sort 、 Delete 、 Search, etc. . Let users in STL In use , It's not strange .

About set, It must be noted that set Associative containers .set As a container, it is also a data type used to store the same data type , And it can extract data from a data set , stay set The value of each element in is unique , And the system can sort elements automatically according to their values . It should be noted that set The value of the median element cannot be changed directly .C++ STL Standard Association container in set, multiset, map, multimap The internal use is a very efficient balanced search binary tree : Red and black trees , Has become a RB Trees (Red-Black Tree).RB The statistical performance of the tree is better than that of the general balanced binary tree , So be STL Selected as the internal structure of the associated container .

About set There are several questions :

(1) why map and set The efficiency of insertion and deletion is higher than that of other sequence containers ?

Most people say , It's simple , Because for associated containers , There is no need for memory copy and memory movement . That's right , Such is the case .set All elements in the container are stored as nodes , Its node structure is similar to the linked list , Point to the parent and child nodes . The structure diagram may be as follows :

A
  / \
B C
/ \ / \
  D E F G

So when you insert it, you just need to do a little transformation , Just point the pointer to the new node . When you delete it, it's like , After a little transformation, point the pointer to the deleted node to other nodes OK 了 . All the operation here is to switch the pointer back and forth , It has nothing to do with memory movement .

(2) Why every time insert after , Previously saved iterator It won't fail ?

iterator This is equivalent to a pointer to a node , Memory doesn't change , How can a pointer to memory fail ( Of course, the deleted element itself is invalid ). be relative to vector Come on , Every time you delete and insert , The pointer may fail , call push_back It's the same with tail insertion . Because in order to ensure the continuous storage of internal data ,iterator The block pointed to may have been covered by other memory or the memory has been released during deletion and insertion . Even when push_back When , There may not be enough space inside the container , Need a new, bigger memory , Only release the previous memory , Apply for new and larger memory , Copy existing data elements to new memory , Finally, put the elements that need to be inserted at the end , So the previous memory pointer is not available . Especially with find When algorithms are used together , Keep this principle in mind : Don't use expired iterator.

(3) As the number of data elements increases ,set How does the insertion and search speed change ?

If you know log2 You should have a thorough understanding of the answer . stay set Search in is to use binary search , in other words , If there is 16 Elements , At best, you need to compare 4 You can find the result in one time , Yes 32 Elements , Compare at most 5 Time . So there are 10000 A? ? The maximum number of comparisons is log10000, At most 14 Time , If it is 20000 An element ? At most 15 Time . See , When the amount of data doubles , It's just a lot of searches 1 Time , More 1/14 It's just a long search time . When you understand that , You can put elements into it .

2.set The method commonly used in


begin()     , return set The first element of the container

end() , return set The last element of the container

clear()        , Delete set All the elements in the container

empty() , Judge set Whether the container is empty

max_size() , return set The maximum number of elements a container can contain

size() , Returns the current set The number of elements in the container

rbegin , The return value and end() identical

rend() , The return value and rbegin() identical

C++ in set More related articles with detailed usage

  1. AngularJS select in ngOptions Usage details

    AngularJS select in ngOptions Usage details   One . usage ngOption There are different usages for different types of data sources , Mainly reflected in arrays and objects . Array : label for value in a ...

  2. C++ Medium STL in map Usage details ( turn )

    Original address : https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++ Medium STL in map Usage details   Map yes STL An associated container of , It provides ...

  3. C++ in const Usage details

    The main content of this paper comes from CSDN Forum : http://bbs.csdn.net/topics/310007610 I made the following additions . Add : 1. use const When declaring global variables , This variable is only visible in this document , class ...

  4. c/c++ in define Usage details and code examples

    https://blog.csdn.net/u012611878/article/details/52534622   Copyright notice : This article is an original blog article , No reprint without the permission of the blogger . https://blog. ...

  5. Spring in @Async Detailed explanation of usage and simple examples

    Spring in @Async usage introduction : stay Java Application , In most cases, it is through synchronization to achieve interactive processing : But when dealing with interactions with third-party systems , It is easy to cause slow response , Most of them used multithreading to do this before ...

  6. AngularJS in transclude Usage details

    This article mainly introduces AngularJS in transclude usage , A detailed analysis of transclude Specific functions . Use skills and related precautions , Friends in need can refer to This article gives an example of AngularJS in transcl ...

  7. Elasticsearch——Date Math Usage in index

    stay elasticsearch in , Sometimes you want to filter query data by index date , In this case, we need to use the date mathematical expression . For more information, please refer to Elasticsearch Translation summary Index based on mathematical expression of date The pattern is as follows : <s ...

  8. PHP in header Usage details with examples ( turn )

    header Usage of header() The delta function is going to be : Send an original HTTP header [Http Header] To client . header (header) It's the server that HTTP Xie Yi Zhuan HTML Data sent to the browser ...

  9. Python in enumerate Usage details

    enumerate() yes python Built in functions for . Apply to python2.x and python3.xenumerate In the dictionary is enumeration . List means enumerate The parameter is ergodic / Objects that can be iterated ( As listing . character string )e ...

  10. C++ Medium STL in map Usage details

    Map yes STL An associated container of , It offers one-on-one ( The first of these can be called a keyword , Each keyword can only be in map Once in , The second value may be called the keyword ) The data of   processing capacity , Because of this feature , It's probably done when we're dealing with one-to-one data ...

Random recommendation

  1. spring The design idea of transaction manager ( Two )

    See above <spring The design idea of transaction manager ( One )> For the second question , It's about the propagation level of the transaction , The definition is as follows : PROPAGATION_REQUIRED-- If there is no current transaction , Just create a new transaction . This is the most common ...

  2. 【BZOJ】3994: [SDOI2015] About a number and

    The question : \(T(1 \le T \le 50000)\) Time to ask , Give each time \(n, m(1 \le n, m \le 50000)\), seek \(\sum_{i=1}^{n} \sum_{j=1}^{m} ...

  3. 1_UILabel

    // // ViewController.swift // 1_UILabel // // Created by Larry on 2016/12/7. // Copyright 2016 year nf ...

  4. sql And CONCAT usage

    This is a java A written question from a netizen in the communication group during the interview , I think the assumption of the title should be that there is only one minimum number corresponding to a certain letter . The first step is to find out a sub table s1: select name,min(number) fro ...

  5. Spring in @Transactional Transaction rollback instance and source code

    One . Use scenario example In understanding @Transactional We have to know before we can use it @Transactional What's the usage? . Here's a chestnut : For example, there are many members in a department , The two are stored in the Department table and the member table respectively , In the delete ...

  6. pygame Simple dynamic graphs &amp; The movement of motion pictures

    I was learning pygame I read some blogs when I was young ( come from http://eyehere.net/2011/python-pygame-novice-professional-plant-zombie-1/), I think it's well written ...

  7. php Some security prevention issues in the proposal

    As long as we do a good job in all kinds of operations, we can basically prevent some friends from using the loopholes of the website itself to operate the website , A lot of in php All of them are like XSS use htmlentities() The prevention of XSS Attacks and sql Injection can be done with mysql_real_esc ...

  8. About ASP.NET Load balancing in

    ASP.NET Do load balancing in the site : be based on HTTP We may find that we need to solve two problems : First, load balancing , We need a load balancer . Can pass DNS Polling to do , stay DNS The server is configured to load balance us every time ...

  9. eclipse C Development Stm32

    Copyright notice : This article is an original blog article , No reprint without the permission of the blogger . 1. download eclipse The required operating environment ,JDK/JRE. stay http://wiki.eclipse.org/Eclipse/Installation ...

  10. Learning notes -JS Open class two

    typeof Use of operators JS Built in objects Array/Date/Math/String You can think of it as a reference type Do the following test : <scripttype="text/javascript" ...