STL知识点(3)

set相关操作:

#include <iostream>
#include <queue>
#include <cstring>
#include <set>

using namespace std;

class Student
{
	friend class Cmp;
	int id;
	char name[20];
public:
	Student(int i,char *n)
	{
		strcpy(name,n);
		id = i;
	}

	void print() const
	{
		cout << "id: " << id  << " name: " << name << endl;
	}

	bool operator < (const Student &s) const
	{
		return (this->id < s.id);
		//return (strcmp(s1.name,s2.name) <0);
	}

	bool operator > (const Student &s) const
	{
		return (this->id > s.id);
		//return (strcmp(s1.name,s2.name) <0);
	}
};

class Cmp
{
public:
	bool operator ()(const Student &s1,const Student &s2)
	{
		return (s1.id < s2.id);
		//return (strcmp(s1.name,s2.name) <0);
	}
};

int main()
{
	srand(time(NULL));
	set < Student,Cmp > s;
	
	Student s1(24,"aa");
	Student s2(25,"bb");
	Student s3(19,"cc");
	Student s4(18,"dd");
	Student s5(45,"ee");
	
	s.insert(s1);
	s.insert(s2);
	s.insert(s3);
	s.insert(s4);
	s.insert(s5);

	for(set<Student,Cmp>:: iterator it = s.begin(); it != s.end(); it++)
	{
		it->print();
	}
	cout << endl;

	if(s.empty())
	{
		cout << "set is empty" << endl;
	}
	else
	{
		cout << "size = " << s.size() << endl;
		cout << "set is not empty" << endl;
	}

	cout <<  "**************erase***************" << endl;

	set<Student,Cmp>:: iterator it = s.begin();
	it++;
	s.erase(it);
	for(it = s.begin(); it != s.end(); it++)
	{
		it->print();
	}
	cout << endl;

	Student cc(18,"cc");
	s.erase(cc);

	for(it = s.begin(); it != s.end(); it++)
	{
		it->print();
	}
	cout << endl;

	if(s.empty())
	{
		cout << "set is empty" << endl;
	}
	else
	{
		cout << "size = " << s.size() << endl;
		cout << "set is not empty" << endl;
	}
	
	cout << "***********find***************" << endl;
	
	Student aa(24,"bb");
	it = s.find(aa);
	if(it != s.end())
	{
		it->print();
	}
	else
	{
		cout << "don't find" << endl;
	}

	cout << "***********lower_bound***************" << endl;
	it = s.lower_bound(aa);
	if(it != s.end())
	{
		it->print();
	}
	else
	{
		cout << "don't find" << endl;
	}

	cout << "***********equal_range***************" << endl;
	pair< set<Student,Cmp>::iterator,set<Student,Cmp>::iterator > p;
	p = s.equal_range(aa);
	p.first->print();
	p.second->print();

	return 0;	
}

map相关操作:

#include <iostream>
#include <cstring>
#include <map>

using namespace std;

class Student
{
	
	int id;
	char name[20];
public:
	Student()
	{
	
	}
	Student(int i,char *n)
	{
		strcpy(name,n);
		id = i;
	}

	void print() const
	{
		cout << "id: " << id  << " name: " << name << endl;
	}
};


int main()
{		
	map<int,Student > m;
	
	Student s1(24,"aa");
	Student s2(25,"bb");
	Student s3(19,"cc");
	Student s4(18,"dd");
	Student s5(45,"ee");
	Student s6(34,"ff");
	Student s7(35,"gg");
	
	
	m.insert(pair<int ,Student>(1,s1));
	m.insert(pair<int ,Student>(2,s2));


	m.insert(make_pair(3,s3));
	m.insert(make_pair(4,s4));

	m.insert(map<int, Student>::value_type(5,s5));
	m.insert(map<int, Student>::value_type(6,s6));

	m[7] = s7;

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

	pair<map<int, Student>::iterator,bool> p = m.insert(make_pair(3,s3));
	if(!p.second)
	{
		cout << "Insert Failure" << endl;
	}
	else
	{
		cout << "Insert Success" << endl;
	}

	cout << endl;

	
		return 0;	
}

multimap相关操作:

#include <iostream>
#include <cstring>
#include <map>

using namespace std;

class Empleyee
{
	
	int id;
	char name[20];
public:
	Empleyee()
	{
	
	}
	Empleyee(int i,char *n)
	{
		strcpy(name,n);
		id = i;
	}

	void print() const
	{
		cout << "id: " << id  << " name: " << name << endl;
	}
};


int main()
{		
	multimap<string,Empleyee > m;
	
	Empleyee e1(24,"aa");
	Empleyee e2(25,"bb");
	Empleyee e3(19,"cc");
	Empleyee e4(18,"dd");
	Empleyee e5(45,"ee");
	Empleyee e6(34,"ff");
	Empleyee e7(35,"gg");

	m.insert(make_pair("sale",e1));
	m.insert(make_pair("sale",e2));
	m.insert(make_pair("sale",e3));

	m.insert(make_pair("develop",e4));
	m.insert(make_pair("develop",e5));
	m.insert(make_pair("develop",e6));

	m.insert(make_pair("financial",e7));
	m.insert(make_pair("financial",e4));

	int num = m.count("develop");
	cout << "develop:" << num << endl;
	

	multimap<string,Empleyee>::iterator it = m.find("develop");
	for(int i = 0;i < num;i++)
	{
		it->second.print();
		it++;
	}


	cout << endl;

	
		return 0;	
}

猜你喜欢

转载自blog.csdn.net/david_lzn/article/details/81506847