C ++ Primer (fifth edition) Study Notes _ 7_standard template library _ multiset multiple collection containers, _ 7_multiset

Source: Internet
Author: User

C ++ Primer (fifth edition) Study Notes _ 7_standard template library _ multiset multiple collection containers, _ 7_multiset

C ++ Primer (fifth edition) Study Notes _ 7_standard template library _ multiset multi-set container

The multi-set container multiset, like set, uses the red/black tree to organize element data. The only thing that is not needed is that multiset allows repeated element key values to be inserted. Its structure is as follows:

1. multiset element insertion

# Include <iostream> # include <stdio. h >#include <vector >#include <set> # include <string> using namespace std; int main () {multiset <string> str; str. insert ("abc"); str. insert ("123"); str. insert ("111"); str. insert ("aaa"); str. insert ("123"); // all elements in the for (multiset <string >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <* iter <""; cout <endl; return 0 ;}

Running result:

111 123 123 aaa abc

 

2. Delete the multiset Element

(1) The erase () method can be used to delete all repeated elements of a value in the multiset object and return the number of deleted elements.

(2) clear elements using the clear () method.

# Include <iostream> # include <stdio. h >#include <vector >#include <set> # include <string> using namespace std; int main () {multiset <string> str; str. insert ("abc"); str. insert ("123"); str. insert ("111"); str. insert ("aaa"); str. insert ("123"); // all elements in the for (multiset <string >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <* iter <""; cout <endl; int n = str. erase ("123"); cout <"Number of deleted elements:" <n <endl; cout <"output remaining elements after deletion" <endl; for (multiset <string >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <* iter <""; cout <endl; str. clear (); cout <"remaining elements after output deletion" <endl; for (multiset <string >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <* iter <""; cout <endl; return 0 ;}

Running result:

111 123 123 aaa abc

Number of deleted elements: 2

Output remaining elements after deletion

111 aaa abc

Output remaining elements after deletion

 

3. find ()

Use the find () method to find elements:

If it is found, the iterator position of the element is returned (if the element already exists, the iterator position of the first element already exists). If it is found, the iterator position of the element is returned. (If the element already exists, the iterator position of the first element already exists );

If no value is found, the end () iterator position is returned.

# Include <iostream> # include <stdio. h >#include <vector >#include <set> # include <string> using namespace std; int main () {multiset <string> str; str. insert ("abc"); str. insert ("123"); str. insert ("111"); str. insert ("aaa"); str. insert ("123"); // all elements in the for (multiset <string >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <* iter <"; cout <endl; multiset <string >:: iterator iter; iter = str. find ("123"); if (iter! = Str. end () // locate cout <* iter <endl; else // cout not found <"not found" <endl; iter = str. find ("bbb"); if (iter! = Str. end () cout <* iter <endl; else cout <"not found" <endl; return 0 ;}

Running result:

111 123 123 aaa abc

123

Not found


4. Custom comparison functions

By default, elements are inserted in the ascending order of key values. Because the internal data structure is a red/black tree, the comparison function is consistent with the set function. There are two writing methods,

(1) If the element is not a struct, You can compile a comparison function. The following describes how to insert elements into mutlset in a sequence from large to small:

# Include <iostream> # include <stdio. h >#include <vector >#include <set> # include <string> using namespace std; struct myComp {bool operator () (string a, string B) {return a> B ;}}; int main () {multiset <string, myComp> str; str. insert ("abc"); str. insert ("123"); str. insert ("111"); str. insert ("aaa"); str. insert ("123"); // all elements in the for (multiset <string, myComp >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <* iter <""; cout <endl; return 0 ;}


Running result:

Abc AAAS 123 123 111

 

(2) If the element is a struct, you can directly write the comparison function in the struct.

# Include <iostream> # include <stdio. h >#include <vector >#include <set> # include <string> using namespace std; struct Info {string name; float score; bool operator <(Info) const {return. score <score ;}}; int main () {multiset <Info> str; // Insert the element Info; info. name = "Jack"; info. score = 60; str. insert (info); info. name = "Bomi"; info. score = 80; str. insert (info); info. name = "Peti"; info. score = 8 0; str. insert (info); info. name = "Kity"; info. score = 70; str. insert (info); for (multiset <Info >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter). name <"" <(* iter). score <endl; return 0 ;}

Running result:

Bomi 80

Peti 80

Kity 70

Jack 60

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.