12.3 multiset of custom rules

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

struct rule{
	bool operator()(const &a1, const &a2)
	{
		return a1%10 < a2%10;
	}
};

int main()
{
	int a[10] = {2,6,210,8,5,9,12,8,9,122};
	int n = sizeof(a)/sizeof(int);
	
	multiset<int,greater<int> > st1;
	for(int j = 0; j < n; ++j)
		st1.insert(a[j]);
	multiset<int,greater<int> >::iterator i;
	for(i = st1.begin(); i != st1.end(); ++i)//210 122 12 9 9 8 8 6 5 2 
		cout << *i << " "; 
	cout << endl;
	i = st1.find(8);
	cout << *i << endl;
	
	multiset<int,rule> st2;
	for(int j = 0; j < n; ++j)
		st2.insert(a[j]);
	multiset<int,rule>::iterator p;
	for(p = st2.begin(); p != st2.end(); ++p)// 210 2 12 122 5 6 8 8 9 9  
		cout << *p << " "; 
	cout << endl;
	p = st2.find(1222);
	cout << *p << endl;//2
	return 0;
}

 

[note] here, the order of 12 122 is not fixed. Whoever is in front of the others can do it. Because the rule only defines the arrangement of single digits from small to large, and doesn't say how to arrange when the single digits are equal, so the final output of the computer is not necessarily in strict accordance with the order of 12 122.

Note that both p and i are iterators. To access the elements in multiset, you must use an iterator. The function of an iterator is similar to that of a pointer, pointing to the location of a number.

p = st2.find(1222) here, because rule only compares the size of single digits, the number of lookups does not have to be = =, as long as a number is found before or after 1222, even if it is found, it is not a strict equality relationship.

When multiset < int, greater < int > > st; is used, there are two spaces between the following two ">".

In p=st.find(s), because in the defined collation, s can be found before or after "85 Mary 102", so it can be said that s can be found, and the data is equal.

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

struct Student{
	char name[20];
	int id;
	int score;
};

Student a[] = {{"Jack",112,78},{"Mary",102,85},{"Ala",333,92},{"Zero",101,70},{"Cindy",102,78}};
struct rule{
	bool operator()(const Student &a1, const Student &a2)
	{
		if(a1.score != a2.score)
			return a1.score > a2.score;
		else
			return strcmp(a1.name,a2.name)<0;
	}
}; 

int main()
{
	int n = sizeof(a)/sizeof(Student);
	multiset<Student,rule> st;
	for(int i = 0; i < n; ++ i)//The insertion process is automatically sequenced 
		st.insert(a[i]);
	multiset<Student,rule>::iterator p;
	for(p = st.begin(); p != st.end(); ++p)
		cout << p->score << " " << p->name << " " << p->id << endl; 
	cout << endl;
	Student s1 = {"Mary",134,85};
	Student s2 = {"Mary",134,100};
	p = st.find(s1);
	if(p != st.end())
		cout << p->score << " " << p->name << " " << p->id << endl;
	else
		cout << "not found" << endl;
	p = st.find(s2);
	if(p != st.end())
		cout << p->score << " " << p->name << " " << p->id << endl;
	else
		cout << "not found" << endl;
	return 0;	
}

Posted by nevillejones on Sun, 05 Jan 2020 10:29:29 -0800