Containers in STL:
set container common interfaces and usage of multiset container:
set / multiset container: also known as a set, all elements are automatically sorted when inserted (from small to large, i.e. ascending)
Bottom structure of set / multiset container: these two containers belong to associative containers, and the bottom structure is realized by binary tree;
Difference between set and multiset:
1. Duplicate elements are not allowed in the set container;
2. There can be duplicate elements in the multiset container;
Constructor for set and multiset containers:
1.set<T> st; Default (parameterless) constructor
2.set(const set & st); copy constructor
#include <iostream> #Include < set > / / use the container in STL to get the header file containing it. The header files of set and multiset containers are the same, both of which are set #Include < algorithm > / / use the algorithm provided by STL to get the header file containing it using namespace std; void printSet(const set<int> & s) { for(set<int>::const_iterator it=s.begin();it!=s.end();it++) { cout << *it << " "; } cout << endl; } void test_1() //Constructor for set and multiset containers { set<int> s1; //Default constructor //set and multiset containers can only be inserted using insert, not push_back() wait s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(50); s1.insert(20); s1.insert(40); //Traversal output printing //set container features: data will be automatically sorted when inserted (from small to large) //Duplicate data is not allowed in the set container, so 20 will only appear once //10 20 30 40 50 printSet(s1); set<int> s2(s1); //copy constructor printSet(s2); } int main() { test_1(); system("pause"); return 0; }
Assignment of set and multiset containers:
1.set& operator=(const set & st); Assign a value to the newly created set container by overloading the assignment operator;
#include <iostream> #Include < set > / / use the container in STL to get the header file containing it. The header files of set and multiset containers are the same, both of which are set #Include < algorithm > / / use the algorithm provided by STL to get the header file containing it using namespace std; void printSet(const set<int> & s) { for(set<int>::const_iterator it=s.begin();it!=s.end();it++) { cout << *it << " "; } cout << endl; } void test_1() //Assignment of set and multiset containers { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(50); s1.insert(20); s1.insert(40); printSet(s1); set<int> s2; s2 = s1; //Assign a value to the newly created set container by overloading the assignment operator printSet(s2); } int main() { test_1(); system("pause"); return 0; }
Size and exchange of set and multiset containers:
Size of set and multiset containers:
1.empty(); Judge whether the set container is empty;
2.size(); Used to view the size of the container (number of elements);
Container and multi set swap:
1.swap(st); Exchange the container st with its own set container;
#include <iostream> #Include < set > / / use the container in STL to get the header file containing it. The header files of set and multiset containers are the same, both of which are set #Include < algorithm > / / use the algorithm provided by STL to get the header file containing it using namespace std; void printSet(const set<int> & s) { for(set<int>::const_iterator it=s.begin();it!=s.end();it++) { cout << *it << " "; } cout << endl; } void test_1() //Size of set and multiset containers { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(50); s1.insert(40); printSet(s1); if(s1.empty()) //Judge whether the current container is empty. If the container is empty, return true; otherwise, return false { cout << "s1 Container is empty" << endl; } else { cout << "s1 Container is not empty" << endl; cout << "s1 The size of the is:" << s1.size() << endl; //Used to view the size of the container (number of elements) } } void test_2() //Exchange of set and multiset containers { set<int> s1; s1.insert(10); s1.insert(30); s1.insert(20); s1.insert(50); s1.insert(40); set<int> s2; s2.insert(100); s2.insert(300); s2.insert(200); s2.insert(500); s2.insert(400); cout << "Before exchange:" << endl; printSet(s1); printSet(s2); s1.swap(s2); //Swap s1 and s2 cout << "After exchange:" << endl; printSet(s1); printSet(s2); } int main() { test_1(); test_2(); system("pause"); return 0; }
Insertion and deletion of set and multiset containers:
1.insert(elem); Insert an element into the set container;
2.erase(elem); Delete the element matching element elem in the set container;
4.erase(pos); Delete the data at the specified location of the set container through the iterator;
5.erase(begin,end); Delete the data between the set container interval [begin, end) through the iterator;
6.clear(); Empty the current set container;
#include <iostream> #Include < set > / / use the container in STL to get the header file containing it. The header files of set and multiset containers are the same, both of which are set #Include < algorithm > / / use the algorithm provided by STL to get the header file containing it using namespace std; void printSet(const set<int> & s) { for(set<int>::const_iterator it=s.begin();it!=s.end();it++) { cout << *it << " "; } cout << endl; } void test_1() //Insertion and deletion of set and multiset containers { set<int> s1; //Insert of set container s1.insert(30); s1.insert(10); s1.insert(20); s1.insert(50); s1.insert(40); printSet(s1); s1.erase(s1.begin()); //Delete the data at the specified location of the set container through the iterator //20 30 40 50 printSet(s1); s1.erase(30); //Delete the data matching 30 in the set container //20 40 50 printSet(s1); s1.erase(s1.begin(),s1.end()); //Delete the data between the set container interval [s1.begin(),s1.end()) through the iterator printSet(s1); s1.clear(); //Empty the current container printSet(s1); } int main() { test_1(); system("pause"); return 0; }
Statistics and lookup of set and multiset containers:
Find of set container:
1.find(key); Find out whether the element key exists in the set container. If so, return the iterator of the element; If it does not exist, return set end();
Element key does not exist, return set End () can be understood as finding the last element of the set container without finding the element key, so it returns the iterator at the end;
Statistics of set container:
1.count(key); Count the number of occurrences of element keys in the set container, and the return value is the number of occurrences of element keys;
For the set container, only 0 or 1 can be returned; For a multiset container, the return value can be greater than 1;
Because the elements in the set container cannot be repeated, but the elements in the multiset container are allowed to be repeated. That is, in the set container, the element key can appear at most once, so the set container can only return 0 or 1;
#include <iostream> #Include < set > / / use the container in STL to get the header file containing it. The header files of set and multiset containers are the same, both of which are set #Include < algorithm > / / use the algorithm provided by STL to get the header file containing it using namespace std; void printSet(const set<int> & s) { for(set<int>::const_iterator it=s.begin();it!=s.end();it++) { cout << *it << " "; } cout << endl; } void printMultiset(const multiset<int> & s) { for(multiset<int>::const_iterator it=s.begin();it!=s.end();it++) { cout << *it << " "; } cout << endl; } void test_1() //Lookup of set and multiset containers { set<int> s1; s1.insert(30); s1.insert(10); s1.insert(20); s1.insert(50); s1.insert(40); set<int>::iterator pos = s1.find(30); //Receive the return value of the find() function with an iterator if(pos==s1.end()) { cout << "Target element not found" << endl; } else { cout << "Target element found:" << *pos << endl; } } void test_2() //Statistics of set and multiset containers { set<int> s1; //set container s1 s1.insert(30); s1.insert(10); s1.insert(20); s1.insert(30); s1.insert(50); s1.insert(40); s1.insert(30); printSet(s1); int num_1 = s1.count(30); //num_1 is the number of occurrences of 30 in the set container cout << "The number of occurrences of the target element is:" << num_1 << endl; multiset<int> s2; //multiset container s2 s2.insert(30); s2.insert(10); s2.insert(20); s2.insert(30); s2.insert(50); s2.insert(40); s2.insert(30); printMultiset(s2); int num_2 = s2.count(30); //num_2 is the number of occurrences of 30 in the multiset container cout << "The number of occurrences of the target element is:" << num_2 << endl; } int main() { test_1(); test_2(); system("pause"); return 0; }
Difference between set and multiset:
1.set cannot insert duplicate data, but multiset can;
2. When set inserts data, it will return the insertion result and tell you whether the insertion operation is successful;
The result returned by the set container is a pair group. There are two things in the pair group, one is an iterator, and the other is a bool type quantity, which is used to judge whether the insertion is successful;
3. When the multiset container inserts data, it will not detect whether the data is repeatedly inserted, so the multiset container can insert the same data;
The multiset container only returns an iterator, so there is no detection of the insertion operation, and the same data can be inserted;
#include <iostream> #Include < set > / / use the container in STL to get the header file containing it. The header files of set and multiset containers are the same, both of which are set #Include < algorithm > / / use the algorithm provided by STL to get the header file containing it using namespace std; void printSet(const set<int> & s) { for(set<int>::const_iterator it=s.begin();it!=s.end();it++) { cout << *it << " "; } cout << endl; } void printMultiset(const multiset<int> & s) { for(multiset<int>::const_iterator it=s.begin();it!=s.end();it++) { cout << *it << " "; } cout << endl; } void test_1() //Differences between set and multiset containers { set<int> s; pair<set<int>::iterator,bool> ret = s.insert(50); //Use a pair to receive the return value of insert() for the group if(ret.second) //Take out the second thing of the group, that is, a bool type quantity { cout << "The first insertion was successful" << endl; } else { cout << "Failed to insert 50 for the first time" << endl; } ret = s.insert(50); if(ret.second) { cout << "The second insertion was successful" << endl; } else { cout << "Failed to insert 50 for the second time" << endl; } multiset<int> ms; ms.insert(50); ms.insert(50); ms.insert(50); ms.insert(50); ms.insert(50); //The 50 multiset container allows the same data to be inserted printMultiset(ms); } int main() { test_1(); system("pause"); return 0; }
Pair pair group: data that appears in pairs. Two data can be returned by pair group
How pair creates groups:
1.pair<T1,T2> p(value_1,value_2); Parameterized constructor
2.pair<T1,T2> p = make_ pair(value_1,value_2); Using member function make_pair() creates a pair group;
#include <iostream> #include <string> using namespace std; void test_1() //pair group creation { pair<string,int> p1("Yauge",18); //Parameterized constructor cout << "full name:" << p1.first << " Age:" << p1.second << endl; //Get the first data and the second data through first and second respectively pair<string,int> p2 = make_pair("Yauge",18); //Using member function make_pair() creates a pair group cout << "full name:" << p2.first << " Age:" << p2.second << endl; //Get the first data and the second data through first and second respectively } int main() { test_1(); system("pause"); return 0; }
set and multiset containers specify collation: (for built-in data types)
Using the technology of imitation function (overload () operator), modify the sorting rule of set container (the default sorting rule of set container is from small to large, and we manually modify it to from large to small);
However, because the set container will automatically sort when inserting, if you want to modify the default sorting rule, you should modify the sorting rule before inserting;
#include <iostream> #Include < set > / / use the container in STL to get the header file containing it. The header files of set and multiset containers are the same, both of which are set #Include < algorithm > / / use the algorithm provided by STL to get the header file containing it using namespace std; class MyCompare //Make sorting rules { public: bool operator()(int v1,int v2) //The modification of default sorting rules is realized by using the technology of imitation function (overload () operator) { return v1 > v2; //The sorting rule is changed from large to small } }; void test_1() //set container modify collation (for built-in data types) { set<int> s1; s1.insert(20); s1.insert(10); s1.insert(50); s1.insert(30); s1.insert(40); cout << "Before modifying sorting rules:" << endl; for(set<int>::const_iterator it=s1.begin();it!=s1.end();it++) { cout << *it << " "; } cout << endl; //Because the set container will automatically sort when inserting, if you want to modify the default sorting rule, you should modify the sorting rule before inserting set<int,MyCompare> s2; //The modification of default sorting rules is realized by using the technology of imitation function (overload () operator) s2.insert(20); s2.insert(10); s2.insert(50); s2.insert(30); s2.insert(40); cout << "After the sorting rule is modified:" << endl; for(set<int,MyCompare>::iterator it=s2.begin();it!=s2.end();it++) { cout << *it << " "; } cout << endl; } int main() { test_1(); system("pause"); return 0; }
set and multiset containers specify collation: (for custom data types)
When using set and multiset containers to store custom data types, you need to specify the sorting rules in advance, otherwise the compiler does not know how to insert our custom data into set and multiset containers; (because the set and multiset containers will automatically sort when inserting data)
#include <iostream> #Include < set > / / use the container in STL to get the header file containing it. The header files of set and multiset containers are the same, both of which are set #include <string> #Include < algorithm > / / use the algorithm provided by STL to get the header file containing it using namespace std; class Person //human beings { public: Person(string name,int age) { this->m_Name = name; this->m_Age = age; } string m_Name; //full name int m_Age; //Age }; class MyComparePerson //Make sorting rules { public: bool operator()(const Person & p1,const Person & p2) //The modification of default sorting rules is realized by using the technology of imitation function (overload () operator) { //Sort by age return p1.m_Age > p2.m_Age; } }; void printPersonSet(const set<Person,MyComparePerson> & s) { for(set<Person,MyComparePerson>::const_iterator it=s.begin();it!=s.end();it++) { cout << "full name:" << it->m_Name << " Age:" << it->m_Age << endl; } } void test_1() //set container modify collation (for user-defined data types) { //Create set container set<Person,MyComparePerson> s; //Create Person object Person p1("Liu Bei",35); Person p2("Cao Cao",45); Person p3("Sun Quan",40); Person p4("Zhao Yun",25); Person p5("Fei Zhang",29); Person p6("Guan Yu",36); //Insert the Perosn object into the set container s.insert(p1); s.insert(p2); s.insert(p3); s.insert(p4); s.insert(p5); s.insert(p6); //Traverse printout printPersonSet(s); } int main() { test_1(); system("pause"); return 0; }
The above are some common interfaces and usages of set container and multiset container in STL. If there are mistakes in the notes, please point them out and discuss them!