STL知識点(3)
5412 ワード
set関連操作:
map関連操作:
Multimap関連アクション:
#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
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;
}