map/multimap
的简介
map 是标准的 关联式 容器, 一个 map 是一个键值对序列, 即 (key,value) 对。 它提供
基于 key 的快速检索能力。
map 中 key 值是唯一的 。 集合中的元素按一定的 顺序 排列。 元素插入过程是按排序
规则插入, 所以不能指定插入位置。
map 的具体实现采用红黑树变体的平衡二叉树的数据结构 。 在插入操作和删除操作
上比 vector 快。
map 可以直接存取 key 所对应的 value , 支持 [] 操作符, 如 map[key]=value 。
multimap 与 map 的区别: map 支持唯一键值, 每个键只能出现一次; 而 multimap
中相同键可以出现多次。 multimap 不支持 [] 操作符。
map 是标准的 关联式 容器, 一个 map 是一个键值对序列, 即 (key,value) 对。 它提供
基于 key 的快速检索能力。
map 中 key 值是唯一的 。 集合中的元素按一定的 顺序 排列。 元素插入过程是按排序
规则插入, 所以不能指定插入位置。
map 的具体实现采用红黑树变体的平衡二叉树的数据结构 。 在插入操作和删除操作
上比 vector 快。
map 可以直接存取 key 所对应的 value , 支持 [] 操作符, 如 map[key]=value 。
multimap 与 map 的区别: map 支持唯一键值, 每个键只能出现一次; 而 multimap
中相同键可以出现多次。 multimap 不支持 [] 操作符。
#include <map>
#include"iostream" using namespace std; #include"map" #include"string" /*遍历*/ void display(map<int, string>&t) { map<int, string>::iterator it1 = t.begin(); while (it1 != t.end()) { cout << (*it1).first << " " << (*it1).second << endl; it1++; } } /*初始化*/ int main01() { map<int, string>map_1; //通过insert函数创建,采用的是 insert()方法, 该方法返回值为 pair<iterator,bool> //当插入的键值已存在时,则插入失败 //pair<map<int,string>::iterator,bool> mypair1=map_1.insert(pair<int, string>(1, "zhang")); //pair<map<int,string>::iterator,bool> mypair2=map_1.insert(make_pair(1,"zhang")); //pair<map<int,string>::iterator,bool> mypair3=map_1.insert(map<int,string>::value_type(1,"zhang")); pair<int, string>s1(1, "zhangsan"); pair<int, string>s2(5, "li si"); pair<int, string>s3(3, "wangwu"); pair<int, string>s4(2, "zhaoliu"); map_1.insert(s1); map_1.insert(s2); map_1.insert(s3); pair<map<int, string>::iterator, bool> mypair4=map_1.insert(s4); if (mypair4.second != true) { cout << "插入失败" << endl; } else { cout << "插入成功" << endl;/////成功 } //pair<map<int, string>::iterator, bool> mypair5=map_1.insert(pair<int, string>(1, "liu")); //if (mypair5.second != true) //{ // cout << "插入失败" << endl;/////失败 //} //else //{ // cout << "插入成功" << endl; //} //通过数组方式实现非常直观, 但存在一个性能的问题。 插入 7 时, 先在 map_1 中查找主 //键为 7 的项, 若没发现, 则将一个键为 7, 值为初始化值的对组插入到 map_1中, //然后再将值修改成“zhang”。 若发现已存在7 这个键, 则修改这个键对应的 value。 map_1[7] = "zhang"; map_1[9] = "li"; display(map_1); //通过赋值构造函数创建map<int, string>map_2(map_1); map<int, string>map_2 = map_1; //display(map_2); while (!map_2.empty ()) { map<int, string>::iterator it0 = map_2.begin(); cout << it0->first << "\t" << it0->second << endl; map_2.erase(it0); } return 0; } /*查找*/ int main02() { map<int, string>map_1; pair<int, string>s1(1, "zhangsan"); pair<int, string>s2(5, "li si"); pair<int, string>s3(3, "wangwu"); pair<int, string>s4(2, "zhaoliu"); map_1.insert(s1); map_1.insert(s2); map_1.insert(s3); map_1.insert(s4); map<int, string>::iterator it = map_1.find(100); if (it == map_1.end()) { cout << "查找失败" << endl; } else { cout << it->first << "\t" << it->second << endl; } //equal_range 返回两个迭代器(pair) //mypair1.first 第一个迭代器指针,mypair1.second第二个迭代器指针 pair<map<int, string>::iterator, map<int, string>::iterator>mypair1 = map_1.equal_range(5); if (mypair1.first == map_1.end()) { cout << "等于5的位置不存在" << endl; } else { cout << mypair1.first->first << "\t" << mypair1.first->second << endl; } if (mypair1.second== map_1.end()) { cout << "大于5的位置不存在" << endl; } else { cout << mypair1.second->first << "\t" << mypair1.second->second << endl; } return 0; } /* Multimap 案例: //1个key值可以对应多个valude =分组 //公司有销售部 sale (员工2名) 、 技术研发部 development(1人)、财务部 Financial (2人) //人员信息有: 姓名, 年龄, 电话、 工资等组成 //通过 multimap进行 信息的插入、 保存、 显示 //分部门显示员工信息 */ class Person { public: Person(string nam, int a, string tel, double w) { name = nam; age = a; telephone = tel; wage = w; } void person_print() { cout << name << " " << age << " " << telephone << " " << wage << " " << endl; } string getname() { return name; } int getage() { return age; } string name; int age; private: string telephone; double wage; }; int main03() { Person p1("zhangsan",20,"111111111",1000); Person p2("lisier", 21, "222222222", 2000); Person p3("wangwu", 22, "333333333", 3000); Person p4("zhaoliu", 23, "444444444", 4000); Person p5("zhanwei", 24, "555555555", 5000); multimap<string, Person>map_3; map_3.insert(make_pair("Sale",p1));//此处是pair类型 map_3.insert(make_pair("Sale",p2)); map_3.insert(make_pair("Development", p3)); map_3.insert(make_pair("Financial",p4)); map_3.insert(make_pair("Financial",p5)); // 遍历 /*multimap<string, Person>::iterator it3 = map_3.begin(); for (; it3 != map_3.end(); it3++) { cout << it3->first << "\t"; it3->second.person_print(); cout<< endl; }*/ // 查找 int num = map_3.count("Sale"); int tag=0; cout << "Sale数量:" << num << endl; multimap<string, Person>::iterator it4=map_3.find("Sale"); while (it4 != map_3.end()&&tag<num) { cout << it4->first << "\t"; it4->second.person_print(); cout<< endl; it4++; tag++; } return 0; } int main() { //main01();//初始化,元素插入,遍历 //main02();//查找 main03();//multimap的案例:分组 system("pause"); return 0; }
//案例
//编一个同义词功能类,每个单词跟着他的同义词// //例如:one unique single//correct true right//near close //单词基础类和同义词管理类(map) #include "iostream" using namespace std; #include"string" #include"map" #include"vector" #include"sstream" //单词基础类 class Word {public: string keyword; vector<string>vec_1; public: Word(string strline) { istringstream in(strline); in >> keyword; //第一个单词是关键词 string mid = " "; while (!in.eof()) { in >> mid; vec_1.push_back(mid); } } void show() { cout << endl; cout << "单词是:" << "\t" << keyword << endl; cout << "同义词是:" << "\t"; for (int i = 0; i < vec_1.size();i++) { cout << vec_1[i] << "\t"; } cout << endl; } string getkeyword() { return keyword; } }; class wmanager { public: multimap<string, Word>mymap; public: bool add(string strline) { Word wo_rd(strline); pair<string, Word>p(wo_rd.getkeyword(), wo_rd); mymap.insert(p); return true; } void show(string strfind) { multimap<string, Word>::iterator it1 = mymap.find(strfind); if (it1 != mymap.end()) { Word &t = (*it1).second; t.show(); } else { cout << "没有近义词" << endl; } } void show() { multimap<string, Word>::iterator te = mymap.begin(); while (te != mymap.end()) { Word &t = (*te).second; t.show(); te++; } } }; int main() { string s[5] = { string("one unique single"), string("correct true right"), string("near close"), string("happy please"), string("strong powerful") }; wmanager ma; for (int i = 0; i < 5; i++) { ma.add(s[i]); ma.show(); } cout << "以上是词库"<<endl; cout << "**********" << endl; cout << "查找同义词:" << endl; ma.show("near"); cout << "*****good*****" << endl; ma.show("good"); system("pause"); return 0; }