Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
set
Summary: All elements are automatically sorted when inserted
Set and multiset belong to associative containers, and the underlying structure is realized by binary tree
The difference between set and multiset
-
Set does not allow duplicate elements in the container
-
A multiset allows repeating elements in a container
Set constructor and assignment
Function prototype:
- set<T> st; // Default constructor
- set(const set &st); // Copy the constructor
- set operator= (const set &st); // Override the equal operator
Copy the code
“Demo” :
#include <iostream>
#include <set>
using namespace std;
void printSet(const set<int> &st)
{
if (st.empty())
{
cout << "set is empty ." << endl;
}
else
{
for (set<int>::const_iterator it = st.begin(a); it ! = st.end(a); it++) { cout << *it <<"";
}
cout << " , size : " << st.size() << endl; }}void test01(a)
{
set<int> st;
st.insert(50);
st.insert(30);
st.insert(10);
st.insert(20);
st.insert(40);
printSet(st);
// Copy construct
set<int> st2(st);
printSet(st2);
/ / assignment
set<int> st3;
st3 = st2;
printSet(st3);
}
int main(a)
{
test01(a);return 0;
}
Copy the code
Summary:
-
The set container inserts elements with inserts
-
The set is automatically sorted when elements are inserted
Set size and swap
Function prototype:
- size(a);// Returns the number of elements in the container
- empty(a);// Check whether the container is empty
- swap(a);// Swap two collection containers
Copy the code
“Demo” :
#include <iostream>
#include <set>
using namespace std;
void printSet(const set<int> &st)
{
if (st.empty())
{
cout << "set is empty ." << endl;
}
else
{
for (set<int>::const_iterator it = st.begin(a); it ! = st.end(a); it++) { cout << *it <<"";
}
cout << " , size : " << st.size() << endl; }}void test01(a)
{
set<int> st;
st.insert(50);
st.insert(30);
st.insert(10);
st.insert(20);
st.insert(40);
set<int> st2;
st2.insert(100);
st2.insert(300);
st2.insert(400);
st2.insert(200);
st2.insert(500);
cout << "===== before swap ===== " << endl;
cout << "set1 : ";
printSet(st);
cout << "set2 : ";
printSet(st2);
st.swap(st2);
cout << "===== after swap ===== " << endl;
cout << "set1 : ";
printSet(st);
cout << "set2 : ";
printSet(st2);
}
int main(a)
{
test01(a);return 0;
}
Copy the code
Set inserts and deletes elements
Function prototype:
- insert(elem); // Insert elements into the container
- clear(a);// Clear all elements
- erase(pos); // Delete the element referred to by the pos iterator and return the iterator for the next element
- erase(begin,end); // Remove the element in the [begin,end] interval and return an iterator for the next element
- erase(elem); // Remove the element with the value elem from the container
Copy the code
“Demo” :
#include <iostream>
#include <set>
using namespace std;
void printSet(const set<int> &st)
{
if (st.empty())
{
cout << "set is empty ." << endl;
}
else
{
for (set<int>::const_iterator it = st.begin(a); it ! = st.end(a); it++) { cout << *it <<"";
}
cout << " , size : " << st.size() << endl; }}void test01(a)
{
set<int> st;
// Insert elements
st.insert(50);
st.insert(30);
st.insert(10);
st.insert(20);
st.insert(40);
printSet(st);
// Delete elements
st.erase(st.begin());
printSet(st);
// Empty the element
st.clear(a);printSet(st);
}
int main(a)
{
test01(a);return 0;
}
Copy the code
Set lookup and statistics
Function prototype:
- find(key); Set.end (); // Return the iterator corresponding to the key if it exists.
- count(key); // Count the number of key elements
Copy the code
“Demo” :
#include <iostream>
#include <set>
using namespace std;
void printSet(const set<int> &st)
{
if (st.empty())
{
cout << "set is empty ." << endl;
}
else
{
for (set<int>::const_iterator it = st.begin(a); it ! = st.end(a); it++) { cout << *it <<"";
}
cout << " , size : " << st.size() << endl; }}void test01(a)
{
set<int> st;
// Insert elements
st.insert(50);
st.insert(30);
st.insert(10);
st.insert(20);
st.insert(40);
printSet(st);
// Find elements
set<int>::iterator pos = st.find(30);
if(pos ! = st.end())
{
cout << "find " << *pos << endl;
}
else
{
cout << "not found . " << endl;
}
// Count elements
int num = st.count(10);
cout << "num = " << num << endl;
}
int main(a)
{
test01(a);return 0;
}
Copy the code
Set is different from multiset
-
A set cannot insert duplicate data, whereas a multiset can
-
When the set inserts data, it returns the insert result, indicating whether the insert was successful
-
Multiset does not detect data and can insert duplicate data
“Demo” :
#include <iostream>
#include <set>
using namespace std;
// The difference between set and multiset
void test01(a)
{
set<int> s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second)
{
cout << "Set inserted successfully for the first time" << endl;
}
else
{
cout << "Set failed to insert for the first time" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << "Set inserted successfully for the second time" << endl;
}
else
{
cout << "Set failed to insert second time" << endl;
}
multiset<int> ms;
ms.insert(10);
ms.insert(10);
cout << "multiset:";
for (multiset<int>::iterator it = ms.begin(a); it ! = ms.end(a); it++) { cout << *it <<"";
}
cout << endl;
}
int main(a)
{
test01(a);return 0;
}
Copy the code
Pair group creation
Pairs of data, using pair groups can return two dataCopy the code
Function prototype:
- pair(type,type) p (value1,value2);
- pair(type,type) p = make_pair(value1,value2);
Copy the code
“Demo” :
#include <iostream>
#include <string>
using namespace std;
// Create a group
void test01(a)
{
pair<string, int> p(string("Zhang"), 20);
cout << "Name:" << p.first << ", age: << p.second << endl;
pair<string, int> p2 = make_pair("Bill".22);
cout << "Name:" << p2.first << ", age: << p2.second << endl;
}
int main(a)
{
test01(a);return 0;
}
Copy the code
Set sort
The default collation rule of set is from small to large. You can change the collation rule by using the parafter functionCopy the code
“Demo” :
#include <iostream>
#include <set>
using namespace std;
class MyCompare
{
public:
bool operator(a)(int v1, int v2)
{
returnv1 > v2; }};void test01(a)
{
set<int> s1;
s1.insert(19);
s1.insert(5);
s1.insert(10);
s1.insert(3);
cout << "Default order from smallest to largest:";
for (set<int>::iterator it = s1.begin(a); it ! = s1.end(a); it++) { cout << *it <<"";
}
cout << endl;
set<int, MyCompare> s2;
s2.insert(19);
s2.insert(5);
s2.insert(10);
s2.insert(3);
cout << "Specify sort from large size:";
for (set<int, MyCompare>::iterator it = s2.begin(a); it ! = s2.end(a); it++) { cout << *it <<"";
}
cout << endl;
}
int main(a)
{
test01(a);return 0;
}
Copy the code
Set stores custom data types
For custom data types, a set must specify a collation before data can be insertedCopy the code
“Demo” :
#include <iostream>
#include <string>
#include <set>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
public:
string m_name;
int m_age;
};
class PersonCompare
{
public:
bool operator(a)(const Person &p1, const Person &p2)
{
// In descending order of age
returnp1.m_age > p2.m_age; }};void test01(a)
{
Person p1("Zhang".20);
Person p2("Bill".23);
Person p3("Fifty".22);
Person p4("Daisy".21);
set<Person, PersonCompare> s;
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, PersonCompare>::iterator it = s.begin(a); it ! = s.end(a); it++) { cout << it->m_name <<","<< it->m_age << endl; }}int main(a)
{
test01(a);return 0;
}
Copy the code