关联容器 和 顺序容器 的本质差异在于: html
关联容器经过键(key)存储和读取元素,而顺序容器则经过元素在容器中的位置顺序存储和访问元素。ios
Reference: http://www.cnblogs.com/kingcat/archive/2012/05/11/2496135.htmlc++
关联式容器依据特定的排序准则,自动为其元素排序。排序准则以函数形式呈现,用来比较元素值(value)或元素键(key)。缺省状况下以
operator<
进行 比较,不过你也能够提供本身的比较函数,定义出不一样的排序准则。数组一般关联式容器由二叉树(binary tree)实现。在二叉树中,每一个元素(节 点)都有一个父节点和两个子节点;左子树的全部元素都比本身小,右子树的全部元素都比本身大。关联式容器的差异主要在于元素的类型以及处理重复元素时的方式。数据结构
STL预先定义好的关联式容器有:集合(set)、多重集合(multiset)、映射(map)和多重映射(multimap)。less
map函数 |
关联数组:元素经过键来存储和读取this |
setspa |
大小可变的集合,支持经过键实现的快速读取.net |
multimap |
支持同一个键屡次出现的 map 类型 |
multiset |
支持同一个键屡次出现的 set 类型 |
int main() { cout << "Hello World!" << endl; pair<string, int> author; author.first = "lolo"; author.second = 18; pair<string, int> author2; author2 = make_pair("lolo", 18); map<string, int>myMap; myMap["jeff"] = 29; cout << myMap["jeff"] << endl; cout << myMap["lolo"] << endl; //Actually, new element added.
return 0; }
查找Key不存在的内容,会默认自动添加。
key
. So it will return the corresponding key value
.key
. In this case, it will automatically add a key
to the map with null value
.
找到第一个就返回了。
if ( m.find("f") == m.end() ) { // not found } else { // found }
须要整个遍历一遍。
if (m.count("f")>0) cout << " is an element of m.\n"; else cout << " is not an element of m.\n";
#include<map> #include<string> #include<iostream> using namespace std; typedef pair<string, int> PAIR; ostream& operator<<(ostream& out, const PAIR& p) { return out << p.first << "\t" << p.second; } int main() { map<string, int, greater<string>> name_score_map;
name_score_map["LiMin"] = 90; name_score_map["ZiLinMi"] = 79; name_score_map["BoB"] = 92; name_score_map.insert(make_pair("Bing",99)); name_score_map.insert(make_pair("Albert",86));
for (map<string, int>::iterator iter = name_score_map.begin(); iter != name_score_map.end(); ++iter) { cout << *iter << endl; }
return 0; }
structCmpByKeyLength { bool operator()(const string& k1, const string& k2) { return k1.length() < k2.length(); } }; // 使用了函数类 int main() {
map<string, int, CmpByKeyLength> name_score_map; name_score_map["LiMin"] = 90; name_score_map["ZiLinMi"] = 79; name_score_map["BoB"] = 92; name_score_map.insert(make_pair("Bing",99)); name_score_map.insert(make_pair("Albert",86));
for (map<string, int>::iterator iter = name_score_map.begin(); iter != name_score_map.end(); ++iter) { cout << *iter << endl; }
return 0; }
void func_set(void) { std::set<int> myset; std::set<int>::iterator it; std::pair<std::set<int>::iterator, bool> ret; // set some initial values: for (int i=1; i<=5; ++i) {
myset.insert(i*10); // set: 10 20 30 40 50
}
// Jeff --> ret.second 插入成功? 原来没有;插入失败?本来就有。 ret = myset.insert(20); if (ret.second==false) {
it=ret.first; // "it" now points to element 20 }
myset.insert (it, 25); // max efficiency inserting myset.insert (it, 24); // max efficiency inserting myset.insert (it, 26); // no max efficiency inserting int myints[]= {5,10,15}; // 10 already in set, not inserted myset.insert (myints,myints+3); std::cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; }
set由二叉搜索树实现,而且对树进行了平衡处理,使得元素在树中分部较为均匀,所以能保持搜索、插入、删除的复杂度在O(logn)。
How is it useful: Multimaps are one of those things that you need rarely, but when you need them, you really need them.
1 /*
2 * 3 ******************************************** 4 * multimap多重映照容器的基础说明: 5 ******************************************** 6 * 7 * multimap多重映照容器:容器的数据结构采用红黑树进行管理 8 * multimap的全部元素都是pair:第一元素为键值(key),不能修改;第二元素为实值(value),可被修改 9 * 10 * multimap特性以及用法与map彻底相同,惟一的差异在于: 11 * 容许重复键值的元素插入容器(使用了RB-Tree的insert_equal函数) 12 * 所以: 13 * 键值key与元素value的映照关系是多对多的关系 14 * 没有定义[]操做运算 15 * 16 * Sorted Associative Container Pair Associative Container Unique Associative Container 17 * 18 * 使用multimap必须使用宏语句#include <map> 19 * 20 ************************************************************************************** 21 * 22 * 建立multimap对象: 23 * 1.multimap<char,int,greater<char> > a; //元素键值类型为char,映照数据类型为int,键值的比较函数对象为greater<char> 24 * 2.multimap(const key_compare& comp) //指定一个比较函数对象comp来建立map对象 25 * 3.multimap(const multisetr&); //multimap<int,char*> b(a); //此时使用默认的键值比较函数less<int> 26 * 4.multimap(first,last); 27 * 5.multimap(first,last,const key_compare& comp); 28 * 29 * //Example: 30 * pair<const int ,char> p1(1,'a'); 31 * pair<const int ,char> p2(2,'b'); 32 * pair<const int ,char> p3(3,'c'); 33 * pair<const int ,char> p4(4,'d'); 34 * pair<const int ,char> pairArray[]={p1,p2,p3,p4}; 35 * multimap<const int,char> m4(pairArray,pairArray+5); 36 * multimap<const int,char> m3(m4); 37 * multimap<const int,char,greater<const int> > m5(pairArray,pairArray+5,greater<const int>()); 38 * 39 ************************************************************************************** 40 * 41 * 元素的插入 42 * //typedef pair<const key,T> value_type; 43 * pair<iterator,bool> insert(const value_type& v); 44 * iterator insert(iterator pos,const value_type& v); 45 * void insert(first,last); 46 * 47 ************************************************************************************** 48 * 49 * 元素的删除 50 * void erase(iterator pos); 51 * size_type erase(const key_type& k); //删除等于键值k的元素 52 * void erase(first,last); //删除[first,last)区间的元素 53 * void clear(); 54 * 55 ************************************************************************************** 56 * 57 * 访问与搜索 58 * 59 * iterator begin();iterator end(); //企图经过迭代器改变元素是不被容许的 60 * reverse_iterator rbegin();reverse_iterator rend(); 61 * 62 * iterator find(const key_type& k) const; 63 * pair<iterator,iterator> equal_range(const key_type& k) const;//返回的pair对象, 64 * //first为lower_bound(k);大于等于k的第一个元素位置 65 * //second为upper_bound();大于k的第一个元素位置 66 * 67 * 其它经常使用函数 68 * bool empty() const; 69 * size_type size() const; 70 * size_type count(const key_type& k) const; //返回键值等于k的元素个数 71 * void swap(); 72 * 73 * iterator lower_bound();iterator upper_bound();pair<iterator,iterator> equal_range();//上界、下届、肯定区间 74 * 75 * 76 * 77 ******************************************** 78 ** cumirror ** tongjinooo@163.com ** ** 79 ******************************************** 80 * 81 */
82
83 #include <map>
84 #include <string>
85 #include <iostream>
86
87 // 基本操做与set类型,牢记map中全部元素都是pair 88 // 对于自定义类,初学者会以为比较函数如何构造很麻烦,这个能够参照前面的书写示例 89 // 但若设置键值为int或char类型,无须构造比较函数
90
91 struct student{ 92 char* name; 93 int age; 94 char* city; 95 char* phone; 96 }; 97
98 int main() 99 { 100 using namespace std; 101
102 student s[]={ 103 {"童进",23,"武汉","XXX"}, 104 {"老大",23,"武汉","XXX"}, 105 {"饺子",23,"武汉","XXX"}, 106 {"王老虎",23,"武汉","XXX"}, 107 {"周润发",23,"武汉","XXX"}, 108 {"周星星",23,"武汉","XXX"} 109 }; 110 pair<int,student> p1(4,s[0]); 111 pair<int,student> p2(2,s[1]); 112 pair<int,student> p3(3,s[2]); 113 pair<int,student> p4(4,s[3]); //键值key与p1相同
114 pair<int,student> p5(5,s[4]); 115 pair<int,student> p6(6,s[5]); 116 multimap<int, student> a; 117 a.insert(p1); 118 a.insert(p2); 119 a.insert(p3); 120 a.insert(p4); 121 a.insert(p5); 122 a.insert(p6); // 会有重复的key 123 typedef multimap<int, student>::iterator int_multimap; 124 pair<int_multimap,int_multimap> p = a.equal_range(4); 125 int_multimap i = a.find(4); 126 cout<<"班上key值为"<< i->first<<"的学生有:"<<a.count(4)<<"名,"<<" 他们是:"<<endl; 127 for(int_multimap k = p.first; k != p.second; k++) 128 { 129 cout<<k->second.name<<endl; 130 } 131 cout<<"删除重复键值的同窗"<<endl; 132 a.erase(i); 133 cout<<"如今班上总人数为:"<<a.size()<<". 人员以下:"<<endl; 134 for(multimap<int,student>::iterator j=a.begin(); j != a.end(); j++) 135 { 136 cout<<"The name: "<<j->second.name<<" "<<"age: "<<j->second.age<<" "
137 <<"city: "<<j->second.city<<" "<<"phone: "<<j->second.phone<<endl; 138 } 139
140 return 0; 141 }