set/multiset container, map container

Table of Contents

set/multiset container

set basic concepts

set size and swapping

setinsertion and deletion

Find and count

The difference between set and multiset

Change set sorting rules

set stores built-in data types

set stores custom data types

pair team

map container

Basic concepts of map containers

map construction and assignment

map size and swap

map insertion and deletion

map search and statistics

Change map sorting rules


set/multiset container

set basic concepts

Introduction:
All elements are automatically sorted when inserted
Nature:
set/multiset are associative containers, and the underlying structure is implemented using a binary tree.
The difference between set and multiset:
set does not allow duplicate elements in the container
multiset allows duplicate elements in the container

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;

void print(const set<int> & amp;L)
{
    for(set<int>::const_iterator it = L.begin();it!=L.end();it + + )
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main()
{
//set container features all elements are automatically assigned when inserted
//The set container does not allow inserting duplicate values
    set<int> s1;
    s1.insert(40);
    s1.insert(10);
    s1.insert(20);
    s1.insert(60);
    print(s1);
\t
set<int> s2(s1);
    print(s2);
\t
set<int> s3;
s3 = s1;
    print(s3);
\t
    return 0;
}

Compile and run

set size and exchange

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

set insertion and deletion

insert(elem);//Insert elements in the container
clear();//Clear all elements
erase(pos);//Delete the element pointed to by the pos iterator and return the iterator of the next element
erase(beg,end);//Delete all elements in the interval [beg,end) and return the selector of the next element
erase(elem);//Delete the element whose value is elem in the container

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

The difference between set and multiset

set does not allow duplicate elements in the container
multiset allows duplicate elements in the container

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;

int main()
{

    set<int> s1;
    s1.insert(40);
    s1.insert(10);
    s1.insert(20);
    s1.insert(60);
    s1.insert(60);
    for(set<int>::const_iterator it = s1.begin();it!=s1.end();it + + )
    {
        cout << *it << " ";
    }
    cout << endl;
\t
    multiset<int> s2;
    s2.insert(40);
    s2.insert(10);
    s2.insert(20);
    s2.insert(60);
    s2.insert(60);
\t
     for(multiset<int>::const_iterator it = s2.begin();it!=s2.end();it + + )
    {
        cout << *it << " ";
    }
    cout << endl;

\t
    return 0;
}

Compile and run

Change set sorting rules

set stores built-in data types

The default sorting of set containers is from small to large. You can use functors to change the sorting rules of set containers.

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;

class person
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};

int main()
{
//Default sort from small to large
    set<int> s1;
\t
    s1.insert(40);
    s1.insert(10);
    s1.insert(20);
    s1.insert(60);
    for(set<int>::const_iterator it = s1.begin();it!=s1.end();it + + )
    {
        cout << *it << " ";
    }
    cout << endl;


//Specify sorting rules
    set<int,person> s2;
\t
    s2.insert(40);
    s2.insert(10);
    s2.insert(20);
    s2.insert(60);
    for(set<int,person>::const_iterator it = s2.begin();it!=s2.end();it + + )
    {
        cout << *it << " ";
    }
    cout << endl;

\t
    return 0;
}

Compile and run

set stores custom data types

For custom types, set must specify a collation before inserting elements

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;


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

class compare
{
public:
bool operator()(const person & amp;p1,const person & amp;p2)
{
//Age descending order
return p1.age > p2.age;
}
};

void test01()
{
person p1("Zhang San",20);
person p2("李思",28);
person p3("王五",17);

set<person,compare> s;
s.insert(p1);
s.insert(p2);
s.insert(p3);

for(set<person,compare>::const_iterator it = s.begin();it!=s.end();it + + )
    {
        cout <<"Name: " << it->name<< "Age: "<<it->age;
    }
cout << endl;
}

int main()
{
    test01();
    return 0;
}

pair team

pairp (value1,value2);
pairp = make pair( value1,value2);

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;

int main()
{

    pair<string,int>p1("tom",20);

    cout << "Name: "<<p1.first<<"Age: " <<p1.second << endl;


    pair<string,int>p2 = make_pair("tom",20);
    cout << "Name: "<<p2.first<<"Age: " <<p2.second << endl;
    return 0;
}

map container

Basic concepts of map containers

Introduction

All elements in map are pairs
The first element in the pair is key (key value), which serves as an index, and the second element is value (real value)
All elements are automatically sorted based on the element’s key value
Nature:

Map/multimap is an associative container, and the underlying structure is implemented using a binary tree.
advantage:

You can quickly find the value based on the key value
The difference between map and multimap:

map does not allow duplicate kev value elements in the container
Multimap allows duplicate key value elements in the container

map construction and assignment

map mp;//map default constructor

map(const map & amp;mp);//copy constructor

map & amp; operator=(const map & amp;mp); //Overload the equal sign operator

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;

void print(map<int,int> & amp;m)
{
for(map<int,int>::iterator it = m.begin();it!= m.end();it + + )
{
cout << "key: "<<it->first << "value: "<<it->second<<endl;
}
cout << endl;
}

void test01()
{
map<int,int> m;
\t
m.insert(pair<int,int>(1,10));
m.insert(pair<int,int>(3,20));
m.insert(pair<int,int>(2,30));
m.insert(pair<int,int>(4,40));
    //Automatically sort according to key value
print(m);
}

int main()
{
    test01();
    return 0;
}

map size and swap

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

map insertion and deletion

insert(elem);//Insert elements in the container
clear();//Clear all elements
erase(pos);//Delete the element pointed to by the pos iterator and return the iterator of the next element
erase(beg,end);//Delete all elements in the interval [beg,end) and return the iterator of the next element
erase(key);//Delete the element whose value is key in the container

void test01()
{
map m;

//insert element

m.insert(pair(1,10));

m.insert(make_pair(2,20));

m.insert(map::value_type(3,30);

m[4] = 40;//It is not recommended to use interpolation. You can use the key to access the value.

m.erase(m.begin());//Delete the element pointed to by the iterator and return the iterator of the next element
m.erase(m.begin(),m.end());//Delete all elements in the interval [beg, end) and return the iterator of the next element
m.erase();//Delete the element whose value is key in the container
m.clear();//Clear all elements
}

find(key); Find whether the key exists. If it exists, return the iterator of the element of the key: if it does not exist, return set.end();

count(key);//Count the number of elements of key

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;

void test01()
{
    map<int,int> m;

    m.insert(pair<int,int>(1,10));
    m.insert(pair<int,int>(2,20));
    m.insert(pair<int,int>(3,30));

    map<int,int>::iterator pos = m.find(3);//Find the element with key 3 and return the iterator of the element

    if(pos != m.end())
    {
        cout <<"The element was found key = "<< (*pos).first << "value = "<< pos->second << endl;
    }
}
int main()
{
    test01();
    return 0;
}

Change map sorting rules

The default sorting of map containers is from small to large. You can use functors to change the sorting rules of map containers.

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;

class person
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};

int main()
{
//Default sort from small to large
map<int,int> m;

m.insert(pair<int,int>(1,10));
m.insert(pair<int,int>(3,30));
m.insert(pair<int,int>(2,20));

    for(map<int,int>::const_iterator it = m.begin();it!=m.end();it + + )
    {
        cout << "key: "<<it->first << " ";
    }
    cout << endl;


//Specify sorting rules
    map<int,int,person> m1;
\t
m1.insert(pair<int,int>(1,10));
m1.insert(pair<int,int>(3,30));
m1.insert(pair<int,int>(2,20));

    for(map<int,int,person>::const_iterator it = m1.begin();it!=m1.end();it + + )
    {
        cout << "key: "<<it->first << " ";
    }
    cout << endl;

\t
    return 0;
}