STL知識点(3)

5412 ワード

set関連操作:
#include 
#include 
#include 
#include 

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:: 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:: 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::iterator,set::iterator > p;
	p = s.equal_range(aa);
	p.first->print();
	p.second->print();

	return 0;	
}

map関連操作:
#include 
#include 
#include 

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 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(1,s1));
	m.insert(pair(2,s2));


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

	m.insert(map::value_type(5,s5));
	m.insert(map::value_type(6,s6));

	m[7] = s7;

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

	pair::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 
#include 
#include 

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 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::iterator it = m.find("develop");
	for(int i = 0;i < num;i++)
	{
		it->second.print();
		it++;
	}


	cout << endl;

	
		return 0;	
}