[C++] set/multiset container

1.set basic concept



#include <iostream>
using namespace std;

//set container construction and assignment
#include<set>

//Traverse
void printSet(const set<int> & st)
{<!-- -->
for (set<int>::const_iterator it = st. begin(); it != st. end(); it ++ )
{<!-- -->
cout << *it << " ";
}
//Newline
cout << endl;
}
//set container construction and assignment
void test01()
{<!-- -->
set<int>st1; // create set container
//Insert data only insert mode
st1.insert(10);
st1.insert(20);
st1.insert(50);
st1.insert(30);
st1.insert(40);
st1.insert(30);
//Print output
printSet(st1);
//set container features: all elements are automatically sorted when inserted
//set container does not allow inserting duplicate values

//operator= assignment
set<int>st2;
st2 = st1;
printSet(st1);

//copy construction
set<int>st3(st2);
printSet(st3);
}

int main()
{<!-- -->
test01();

//**************************************
system("pause");
return 0;
}

2.set size and exchange

#include <iostream>
using namespace std;

//set container size and swap
#include<set>
//Traverse the set container
void printSet(const set<int> & s)
{<!-- -->
for (set<int>::const_iterator it = s. begin(); it != s. end(); it ++ )
{<!-- -->
cout << *it << " ";
}
cout << endl;
}
//size
void test01()
{<!-- -->
set<int>s1;
//set container can only use insert to insert data
s1.insert(10);
s1.insert(50);
s1.insert(30);
s1.insert(40);
s1. insert(20);
//Print container
printSet(s1);

// Check if the container is empty
if (s1.empty())
{<!-- -->
cout << "s1 is empty" << endl;
}
else
{<!-- -->
cout << "s1 is not empty" << endl;
cout << "s1 size is " << s1.size() << endl; //output the number of s1 elements
}

}

//exchange
void test02()
{<!-- -->
//Create set container 1
set<int>s1;
s1.insert(10);
s1.insert(50);
s1.insert(30);
s1. insert(40);
s1. insert(20);

//Create set container 2
set<int>s2;
s2. insert(100);
s2. insert(400);
s2. insert(300);
s2. insert(500);
s2. insert(200);

cout << "before exchange: " << endl;
printSet(s1);
printSet(s2);

// swap s1 and s2 containers
cout << "After exchange: " << endl;
printSet(s1);
printSet(s2);
}

int main()
{<!-- -->
test01();
cout << "-------------" << endl << endl;
test02();

//**************************************
system("pause");
return 0;
}

3.set insert and delete


#include <iostream>
using namespace std;

//set container insert and delete
#include<set>
void printSet(const set<int> & s)
{<!-- -->
for (set<int>::const_iterator it = s. begin(); it != s. end(); it ++ )
{<!-- -->
cout << *it << " ";
}
cout << endl;
}

void test01()
{<!-- -->
//Create set container
set<int>s1;
//Insert
s1.insert(50);
s1.insert(30);
s1.insert(10);
s1.insert(20);
s1.insert(40);
\t//Print
printSet(s1);

\t//delete
s1.erase(s1.begin());
printSet(s1);

//delete overloaded version
s1. erase(30);
printSet(s1);

//clear
//s1.erase(s1.begin(), s1.end()); //The method of using the interval
s1.clear(); // use the clear() member function
printSet(s1);
}

int main()
{<!-- -->
test01();
//cout << "-------------" << endl << endl;
//test02();

//**************************************
system("pause");
return 0;
}

4.set search and statistics


#include <iostream>
using namespace std;

//set container search and statistics
#include <set>
void test01()
{<!-- -->
set<int>s1;
s1. insert(30);
s1. insert(20);
s1. insert(50);
s1. insert(10);
s1. insert(40);

//Find
//Find element 30 and return set iterator
set<int>::iterator pos = s1. find(30);
if (pos != s1.end()) // If not found, return s1.end() iterator
{<!-- -->
cout << "Find element: " << *pos << endl;
}
else
{<!-- -->
cout << "Element not found!" << endl;
}

//Statistics
// count the number of 30 elements
int num = s1.count(30);
//For set, the statistical result is either 0 or 1
cout << "num = " << num << endl;
}

int main()
{<!-- -->
test01();
//cout << "-------------" << endl << endl;
//test02();

//******************************************
system("pause");
return 0;
}

5.The difference between set and multiset


#include <iostream>
using namespace std;

//The difference between set container and multiset container
#include <set>
void test01()
{<!-- -->
//Create set container
set<int>s;

//pair<iterator, bool> use insert return value type
pair<set<int>::iterator, bool> ret = s.insert(10);

if (ret.second)
{<!-- -->
cout << "The first insertion is successful " << "The inserted data is: " << *ret.first << endl;
}
else
{<!-- -->
cout << "First insertion failed" << endl;
}
\t
//Insert the same number again
ret = s.insert(10);
if (ret.second)
{<!-- -->
cout << "First insertion successful" << *ret.first << endl;
}
else
{<!-- -->
cout << "First insertion failed" << endl;
}

//Create multiset
multiset<int>ms;
//Allow duplicate values to be inserted
ms.insert(20);
ms.insert(20);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it + + )
{<!-- -->
cout << *it << " ";
}
cout << endl;
}

int main()
{<!-- -->
test01();
//cout << "-------------" << endl << endl;
//test02();

//******************************************
system("pause");
return 0;
}

6.Pair creation


#include <iostream>
using namespace std;

//pair pair creation
void test01()
{<!-- -->
//First way
pair<string, int>p("Tom", 20);
cout << "Name:" << p.first << "\tAge:" << p.second << endl;

//Second way
pair<string, int>p2 = make_pair("Jerry", 30);
cout << "Name:" << p2.first << "\tAge:" << p2.second << endl;
}

int main()
{<!-- -->
test01();
//cout << "-------------" << endl << endl;
//test02();

//******************************************
system("pause");
return 0;
}

7.set container sorting



#include <iostream>
using namespace std;

//set container sorting
#include <set>

classMyCompare
{<!-- -->
public:
bool operator()(int v1, int v2)
{<!-- -->
return v1 > v2;
}
};

void test01()
{<!-- -->
set<int, MyCompare>s1;
s1.insert(30);
s1.insert(20);
s1.insert(40);
s1.insert(10);
//Sort from large to small
for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); it + + )
{<!-- -->
cout << *it << " ";
}
cout << endl;
}

int main()
{<!-- -->
test01();
//cout << "-------------" << endl << endl;
//test02();

//******************************************
system("pause");
return 0;
}


#include <iostream>
using namespace std;

// Set container sorting, store custom data types
#include <set>

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

string m_Name;
int m_Age;
};

classComparePerson
{<!-- -->
public:
bool operator()(const Person & amp;p1,const Person & amp;p2)
{<!-- -->
//Sort by age in descending order
return p1.m_Age > p2.m_Age;
}
};

void test01()
{<!-- -->
//Custom data types will specify sorting rules
set<Person, ComparePerson>s;

//Create Person object
Person p1("Liu Bei", 24);
Person p2("Guan Yu", 28);
Person p3("Zhang Fei", 25);
Person p4("Zhao Yun", 21);

//Insert data into the container
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);

for (set<Person, ComparePerson>::iterator it = s.begin(); it != s.end(); it + + )
{<!-- -->
cout << "Name:" << it->m_Name << "\tAge:" << it->m_Age << endl;
}
}

int main()
{<!-- -->
test01();
//cout << "-------------" << endl << endl;
//test02();

//******************************************
system("pause");
return 0;
}