C++ multiset集合操练
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <set>
using namespace std;
class Student
{
public:
Student(string name, int age)
{
this->name = name;
this->age = age;
}
public:
string name;
int age;
};
struct FuncStudent
{
bool operator()(const Student& left, const Student& right)
{
return (left.age < right.age);
}
};
int main()
{
multiset<int> set1;
for (int i = 0; i < 5; i++)
{
int tmp = rand();
set1.insert(tmp);
}
set1.insert(5);
set1.insert(100);
set1.insert(100);
for (multiset<int>::iterator it = set1.begin(); it != set1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
while (!set1.empty())
{
multiset<int>::iterator it = set1.begin();
cout << *it << " ";
set1.erase(it);
}
cout << endl << endl;
multiset<int, greater<int>> set2;
for (int i = 0; i < 5; i++)
{
int tmp = rand();
set2.insert(tmp);
}
for (multiset<int, greater<int>>::iterator it = set2.begin(); it != set2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
multiset<int> set3;
for (int i = 0; i < 10; i++)
{
set3.insert(i + 1);
}
multiset<int>::iterator it0 = set3.find(5);
cout << "*it0: " << *it0 << endl;
int num1 = set3.count(5);
cout << "num1: " << num1 << endl;
multiset<int>::iterator it1 = set3.lower_bound(5);
cout << "*it1: " << *it1 << endl;
multiset<int>::iterator it2 = set3.lower_bound(5);
cout << "*it2: " << *it2 << endl;
pair<multiset<int>::iterator, multiset<int>::iterator> myPair = set3.equal_range(5);
multiset<int>::iterator it3 = myPair.first;
cout << "*it3: " << *it3 << endl;
multiset<int>::iterator it4 = myPair.second;
cout << "*it4: " << *it4 << endl << endl;
multiset<int> set4;
int tmp;
cout << "请输入multiset集合的值(输入0结束): ";
cin >> tmp;
while (tmp)
{
set4.insert(tmp);
cout << "请输入multiset集合的值(输入0结束): ";
cin >> tmp;
}
for (multiset<int>::iterator it = set4.begin(); it != set4.end(); it++)
{
cout << *it << " ";
}
cout << endl;
return 0;
}