- map的插入
map<int, string> map1;
//方法1
map1.insert(pair<int, string>(1,"teacher01") );
map1.insert(pair<int, string>(2,"teacher02") );
//方法2
map1.insert(make_pair(3, "teacher04") );
map1.insert(make_pair(4, "teacher05") );
//方法3
map1.insert(map<int, string>::value_type(5, "teacher05") );
map1.insert(map<int, string>::value_type(6, "teacher06") );
//方法4
map1[7] = "teacher07";
map1[8] = "teacher08";
//map1['z'] = "teacher08";
//容器的遍历
for (map<int, string>::iterator it = map1.begin(); it!=map1.end(); it++ )
{
cout << it->first << "\t" << it->second << endl;
}
cout << "遍历结束" << endl;
//容器元素的删除
while (!map1.empty())
{
map<int, string>::iterator it = map1.begin();
cout << it->first << "\t" << it->second << endl;
map1.erase(it);
}
- map的查找
map<int, string> map1;
//方法1
map1.insert(pair<int, string>(1,"teacher01") );
map1.insert(pair<int, string>(2,"teacher02") );
//方法2
map1.insert(make_pair(3, "teacher04") );
map1.insert(make_pair(4, "teacher05") );
//方法3
map1.insert(map<int, string>::value_type(5, "teacher05") );
map1.insert(map<int, string>::value_type(6, "teacher06") );
//方法4
map1[7] = "teacher07";
map1[8] = "teacher08";
//容器的遍历
for (map<int, string>::iterator it = map1.begin(); it!=map1.end(); it++ )
{
cout << it->first << "\t" << it->second << endl;
}
cout << "遍历结束" << endl;
//map的查找 //异常处理
map<int, string>::iterator it2 = map1.find(100);
if (it2 == map1.end())
{
cout << "key 100 的值 不存在" << endl;
}
else
{
cout << it2->first << "\t" << it2->second << endl;
}
//equal_range //异常处理
pair<map<int, string>::iterator , map<int, string>::iterator> mypair = map1.equal_range(5); //返回两个迭代器 形成一个 pair
//第一个迭代器 >= 5的 位置
//第一个迭代器 > 5的 位置
if (mypair.first == map1.end() )
{
cout << "第一个迭代器 >= 5的 位置 不存在" << endl;
}
else
{
cout << mypair.first->first << "\t" << mypair.first->second << endl;
}
//使用第二个迭代器
if (mypair.second == map1.end() )
{
cout << "第二个迭代器 > 5的 位置 不存在" << endl;
}
else
{
cout << mypair.second->first << "\t" << mypair.second->second << endl;
}
- multimap插入
class Person
{
public:
string name;
int age;
string tel;
double saly;
};
int main()
{
Person p1, p2, p3, p4, p5;
p1.name = "王1";
p1.age = 31;
p2.name = "王2";
p2.age = 32;
p3.name = "张3";
p3.age = 33;
p4.name = "张4";
p4.age = 34;
p5.name = "赵5";
p5.age = 35;
multimap<string, Person> map2;
//sale部门
map2.insert(make_pair("sale", p1) );
map2.insert(make_pair("sale", p2) );
//development 部门
map2.insert(make_pair("development", p3) );
map2.insert(make_pair("development", p4) );
//Financial 部门
map2.insert(make_pair("Financial", p5) );
for( multimap<string, Person>::iterator it=map2.begin(); it!=map2.end(); it++)
{
cout << it->first << "\t" << it->second.name << endl;
}
cout << "遍历结束" << endl;
//
int num2 = map2.count("development");
cout << "development部门人数==>" << num2 << endl;
cout << "development部门员工信息" << endl;
multimap<string, Person>::iterator it2 = map2.find("development");
int tag = 0;
while (it2 != map2.end() && tag < num2)
{
cout << it2->first << "\t" << it2->second.name << endl;
it2 ++;
tag ++;
}
return 0;
}
- multimap的查找
class Person
{
public:
string name;
int age;
string tel;
double saly;
};
int main()
{
Person p1, p2, p3, p4, p5;
p1.name = "王1";
p1.age = 31;
p2.name = "王2";
p2.age = 32;
p3.name = "张3";
p3.age = 33;
p4.name = "张4";
p4.age = 34;
p5.name = "赵5";
p5.age = 35;
multimap<string, Person> map2;
//sale部门
map2.insert(make_pair("sale", p1) );
map2.insert(make_pair("sale", p2) );
//development 部门
map2.insert(make_pair("development", p3) );
map2.insert(make_pair("development", p4) );
//Financial 部门
map2.insert(make_pair("Financial", p5) );
for( multimap<string, Person>::iterator it=map2.begin(); it!=map2.end(); it++)
{
cout << it->first << "\t" << it->second.name << endl;
}
cout << "遍历结束" << endl;
//
int num2 = map2.count("development");
cout << "development部门人数==>" << num2 << endl;
cout << "development部门员工信息" << endl;
multimap<string, Person>::iterator it2 = map2.find("development");
int tag = 0;
while (it2 != map2.end() && tag < num2)
{
cout << it2->first << "\t" << it2->second.name << endl;
it2 ++;
tag ++;
}
pair<multimap<string, Person>::iterator , multimap<string, Person>::iterator> mypair = map2.equal_range("development"); //返回两个迭代器 形成一个 pair
if (mypair.first == map2.end() )
{
cout << "第一个迭代器 >= development的 位置 不存在" << endl;
}
else
{
cout<<"res begin"<<endl;
cout << mypair.first->first << "\t" << mypair.first->second.name << endl;
}
if(mypair.second==map2.end()){
cout<<"第二个迭代器>development的位置不存在"<<endl;
}else{
cout<<"res end"<<endl;
cout<<mypair.second->first<<"\t"<<mypair.second->second.name<<endl;
}
return 0;
}