// STL_map.cpp: 定义控制台应用程序的入口点。 // /* map为标准关联式容器,一个map是一个键值对序列,即(key,value)对,提供基于key的快速检索能力 map的键值唯一,集合中元素按一定顺序排列,元素插入过程按顺序规则插入,不能指定插入位置 map可以直接取key所对应的value,支持[]操作符 multimap与map的区别,map支持唯一键值,每个键只能出现一次;而multimap中相同键可以出现多次。multimap不支持[]操作符 wangsl */ #include "stdafx.h" #include <map> #include <functional> #include<iostream> #include<string> using namespace std; /* 增删改查 wangsl */ void map_test() { map<int, string> map1; /* 插入 wangsl */ pair<map<int, string>::iterator,bool> mypair1 = map1.insert(pair<int, string>(1, "teacher01")); pair<map<int, string>::iterator, bool> mypair2 = map1.insert(pair<int, string>(2, "teacher02")); pair<map<int, string>::iterator, bool> mypair3 = map1.insert(make_pair(3, "teacher03")); pair<map<int, string>::iterator, bool> mypair4 = map1.insert(make_pair(4, "teacher04")); pair<map<int, string>::iterator, bool> mypair5 = map1.insert(map<int, string>::value_type(5, "teacher05")); pair<map<int, string>::iterator, bool> mypair6 = map1.insert(map<int, string>::value_type(6, "teacher06")); /* 四种插入方式异同:以上三种插入方式返回pair<iterator,bool> wangsl */ map1[7] = "teacher07"; map1[7] = "teacher08"; /* 这样会覆盖teacher07 其他方式则会报错 wangsl */ /* 遍历 wangsl */ for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++) { cout << it->first << "\t" << it->second << endl; } /* 查找 wangsl */ map<int, string>::iterator it2 = map1.find(100); if (it2 == map1.end()) { cout<< "key 100 的值不存在!" << endl; } else cout<< it2->first <<"\t"<<it2->second << endl; /* 1. >=5的位置 2. >5的位置 wangsl */ pair<map<int,string>::iterator,map<int ,string >::iterator> mypair = map1.equal_range(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; /* 删除 wangsl */ while (!map1.empty()) { map<int, string>::iterator it = map1.begin(); cout << it->first << "\t" << it->second << endl; map1.erase(it); } } ////////////////////////////////////////////////////////////////////////// /* multimap 一个key可以对应多个value wangsl */ /* 员工信息 wangsl */ class Person { public: Person(string name, int age) :name(name),age(age),tel("0414"),salary(6000) { } public: string name; int age; string tel; double salary; }; void multimap_test() { //sales部门 Person p1("王一", 30); Person p2("王二", 25); //development部门 Person p3("张三", 34); Person p4("张四", 35); //financial部门 Person p5("刘五", 44); Person p6("刘六", 24); multimap<string, Person> map1; map1.insert(make_pair("sales", p1)); map1.insert(make_pair("sales", p2)); map1.insert(make_pair("development", p3)); map1.insert(make_pair("development", p4)); map1.insert(make_pair("financial", p5)); map1.insert(make_pair("financial", p6)); /* 遍历 wangsl */ for (multimap<string,Person>::iterator it = map1.begin();it != map1.end(); it++) { cout<< it->first << "\t" << it->second.name << " " << it->second.age << endl; } int num = map1.count("development"); int tag = 0; multimap<string, Person>::iterator it = map1.begin(); //it = map1.find("王一"); /* find()只能找key值 wangsl */ it = map1.find("development"); while ( it!= map1.end() && tag < num) { cout << it->first << "\t" << it->second.name << endl; tag++; it++; } } int main() { map_test(); multimap_test(); system("pause"); return 0; }~
STL学习之map与multimap操作练习
猜你喜欢
转载自blog.csdn.net/lasuerte/article/details/78731183
今日推荐
周排行