[C + +] learning notes -- creation and use of set/multiset container and pair group

Posted by Anim9or on Wed, 03 Nov 2021 11:01:52 +0100

set/multiset container


Container: set/multiset

Header file: #include

Basic concepts of set

Introduction:

  • All elements are automatically sorted (ascending from small to large) upon insertion

Essence:

  • set/multiset is an associative container, and its underlying structure is implemented by binary tree.

Difference between set and multiset:

  • set does not allow duplicate elements in a container
  • multiset allows duplicate elements in a container

set construction and assignment

Construction:

  • set<T> st; // Default constructor
  • set(const set &st); // copy constructor

Assignment:

  • set& operator=(const set &st); // Overloaded equal sign operator

set size and swap

Function prototype:

  • size(); // Returns the number of elements in the container
  • empty(); // Determine whether the container is empty
  • swap(st); // Swap two collection containers

set insert and delete

Function prototype:

  • insert(elem); // Insert element in container
  • clear(); // Clear all elements
  • erase(pos); // Delete the element referred to by the POS iterator and return the iterator of the next element
  • erase(beg, end); // Delete all elements of the interval [beg, end], and return the iterator of the next element
  • erase(elem); // Delete the element with element in the container

set lookup and statistics

Function prototype:

  • find(key); // Find out whether the key exists. If so, return the element iterator of the key; If it does not exist, return set.end();
  • count(key); // Count the number of key elements

Difference between set and multiset

difference:

  • set cannot insert duplicate data, while multiset can
  • set will return the insertion result when inserting data, indicating whether the insertion is successful
  • multiset does not detect data, so duplicate data can be inserted

set container sort

Learning objectives:

  • The default sorting rule of the set container is from small to large. Master how to change the sorting rule

Main technical points:

  • Using the imitation function, the sorting rules can be changed

Demonstration of built-in data type code stored in set:

#include<iostream>
#include<set>
using namespace std;

class MyConpare     //functor 
{
public:
	bool operator()(int v1, int v2)const     //Here, const should be added to the function to indicate that the function does not modify class member variables. Instead, adding const before int v1 and int v2 means that v1 and v2 are not modified
	{
		return v1 > v2;    //The specified collation is from large to small
	}
};
int main()
{
	set<int>s1;

	s1.insert(10);
	s1.insert(40);
	s1.insert(20);
	s1.insert(50);
	s1.insert(30);

	//The default collation is from small to large
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	set<int,MyConpare>s2;    //Use the functor to modify the collation from large to small

	s2.insert(10);
	s2.insert(40);
	s2.insert(20);
	s2.insert(50);
	s2.insert(30);

	//Modify the sorting rule to sort from large to small
	for (set<int,MyConpare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}

Demo of user-defined data type code stored in set

#include<iostream>
#include<set>
using namespace std;

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

class MyConpare1     //Imitation function from small to large
{
public:
	bool operator()(const Person v1, const Person v2)const
	{
		return v1.m_Age < v2.m_Age;    //The specified collation is from large to small
	}
};

class MyConpare2     //Large to small affine functions
{
public:
	bool operator()(const Person v1, const Person v2)const
	{
		return v1.m_Age > v2.m_Age;    //The specified collation is from large to small
	}
};
int main()
{
	//Create Person object
	Person p1("Liu Bei", 35);
	Person p2("Guan Yu", 30);
	Person p3("Fei Zhang", 29);
	Person p4("Zhuge Liang", 27);

	set<Person, MyConpare1>s1;

	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	s1.insert(p4);

	//The default collation is from small to large
	cout << "Sort from small to large:" << endl;
	for (set<Person, MyConpare1>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << "full name:" << it->m_Name << " " << "Age:" << it->m_Age << endl;
	}

	set<Person,MyConpare2>s2;    //Use the functor to modify the collation from large to small

	s2.insert(p1);
	s2.insert(p2);
	s2.insert(p3);
	s2.insert(p4);

	//Modify the sorting rule to sort from large to small
	cout << "Sort from small to large:" << endl;
	for (set<Person, MyConpare2>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << "full name:" << it->m_Name << " " << "Age:" << it->m_Age << endl;
	}
	cout << endl;

	system("pause");
	return 0;
}

pair group creation and use

Function Description:

  • For paired data, two data can be returned by using the pair group

There are two creation methods:

  • pair<type, type> p (value1, value2);
  • pair<type, type> p = make_pair( value1, value2);

Code demonstration:

#include<iostream>
using namespace std;

int main()
{
	//The first way to create
	pair<string, int>p1("Tom", 20);
	cout << "full name:" << p1.first << " " << "Age:" << p1.second << endl;
	//The second way to create
	pair<string, int>p2 = make_pair("Jerry", 30);
	cout << "full name:" << p2.first << " " << "Age:" << p2.second << endl;
	system("pause");
	return 0;
}

Topics: C++ Operation & Maintenance Container