map/multimap/set/multiset 简介
map/multimap都是以key/value对的方式存储数据的,通过key即可查找到对应的value,二者区别,前者的key不可以重复,后者可以重复。
set/multiset每个元素只是以一个实值存在,前者不允许重复,后者允许重复。
map/multimap 构造函数
void MapDefine() {
// 定义空对象
typedef pair<int, char> int_pair;
pair<map<int,char>::iterator, char> m_iter;
map<int, char> mp;
mp.insert(int_pair(1, 'a'));
mp.insert(int_pair(2, 'b'));// insert返回值是一个pair<iterator,bool>类型,iterator是对应map的迭代器,bool为true则插入成功,反之插入失败
m_iter = mp.insert(int_pair(2, 'c'));//返回false,下面输出仍是1 a, 2 b
for_each(mp.begin(), mp.end(), fun);//输出结果会默认排序的
cout << endl;
// 用另一个对象定义
map<int, char> mp1(mp);
for_each(mp1.begin(), mp1.end(), fun);
cout << endl;
// 通过另一个对象的一段数据定义
map<int, char> mp2(mp1.begin(), mp1.end());
for_each(mp2.begin(), mp2.end(),fun);
cout << endl;
}
map/multimap 常用方法
void MapProperty() {
typedef pair<int, char> i_pair;
pair<map<int, char>::iterator, bool> m_iter;
map<int, char> mp1;
mp1.insert(i_pair(111, 'a'));
mp1.insert(i_pair(222, 'b'));
mp1.insert(i_pair(333, 'c'));
mp1.insert(i_pair(111, 'w'));// 没插入进去,返回的pair<iterator,bool> 中的bool为0
for_each(mp1.begin(), mp1.end(), fun);
// size()/max_size()
cout << mp1.size() << endl;// 输出3,元素个数
cout << mp1.max_size() << endl; // 根据内存清空输出,能够存储的最大元素个数
// empty()
cout << mp1.empty() << endl;//输出0, 不为空
// rbegin()/rend()
cout << "-------------" << endl;
map<int, char>::reverse_iterator riter;
riter = mp1.rbegin();
for (riter; riter != mp1.rend(); ++riter) {
cout << riter->second << endl;// 输出c b a
}
// count()/find()
cout << mp1.count(111) << endl;//输出1,返回map中指定key的个数,map和set返回不是1就是0,因为不能重复元素,mutimap和mutiset就不同了
cout << (mp1.find(222))->second << endl;//输出b, find返回的是iterator,没找到则返回.end()
// key_comp()/value_comp()
cout << "-------------" << endl;
map<int, char>::key_compare kc = mp1.key_comp();//返回一个比较key的函数,判断左操作数是否小于右操作数
map<int, char>::value_compare vc = mp1.value_comp();// 这里需注意,vc必须赋值,否则报错,但上面的kc不会
map<int, char>::iterator f_iter = mp1.begin();//f_iter指向mp1首元素
map<int, char>::iterator s_iter = --(--(mp1.end()));//s_iter指向mp1第二个元素
cout << kc(1, 3) << endl;// 输出1
cout << f_iter->second << " " << s_iter->second << endl;// 输出a b
cout << vc(*f_iter,*s_iter) << endl;//输出1,如果f_iter对应的key在s_iter对应的key前面,返回true
// upper_bound()/lower_bound()
cout << "-------------" << endl;
map<int, char>::iterator test_iter1;
map<int, char>::iterator test_iter2;
test_iter1 = mp1.upper_bound(222);
test_iter2 = mp1.lower_bound(222);
cout << test_iter1->second << endl;// 输出c,upper_bound返回一个迭代器,指向map中键值>key的第一个元素
cout << test_iter2->second << endl;// 输出b,lower_bound返回一个迭代器,指向map中键值>=key的第一个元素。
}
void MapMethods() {
typedef pair<int, char> i_pair;
map<int, char> mp1,mp2;
mp1.insert(i_pair(11, 'a'));
mp1.insert(i_pair(22, 'b'));
mp1.insert(i_pair(33, 'c'));
mp1.insert(i_pair(44, 'd'));
for_each(mp1.begin(), mp1.end(), fun);// a b c d
/*
void erase( iterator pos );
void erase( iterator start, iterator end );
size_type erase( const KEY_TYPE &key );
*/
cout << "--------------" << endl;
// erase()
mp1.erase(mp1.begin()++);
for_each(mp1.begin(), mp1.end(), fun);// 输出 b c d
// swap()
cout << "--------------" << endl;
mp2.insert(i_pair(11, 'w'));
mp2.insert(i_pair(22, 'x'));
mp2.insert(i_pair(33, 'y'));
mp2.insert(i_pair(44, 'z'));
mp1.swap(mp2);
for_each(mp1.begin(), mp1.end(), fun);
// clear()
cout << "--------------" << endl;
mp1.clear();
cout << mp1.size() << endl;
}
void MapEqualRange() {
typedef pair<int, char> i_pair;
multimap<int, char> mp1;
mp1.insert(i_pair(11, 'a'));
mp1.insert(i_pair(11, 'b'));
mp1.insert(i_pair(22, 'c'));
mp1.insert(i_pair(11, 'o'));
mp1.insert(i_pair(11, 'p'));
mp1.insert(i_pair(11, 'q'));
mp1.insert(i_pair(33, 'w'));
mp1.insert(i_pair(44, 'y'));
for_each(mp1.begin(), mp1.end(), fun);// 会按照key自动排序输出
cout << "------------" << endl;
// equal_range()
/*
理解equal_range:一个multimap中(存在重复key),所有元素的区间为[first,last],因所有元素会自动按照key排序,所以存在一个区间[i,j],
区间内的所有key必然相同(对应的value可能不同),lower_bound()返回的iterator指向所查找key的第一个元素(i的位置),upper_bound()返回
的是所查找key最后一个元素的下一个位置(j+1),因此如下会输出a c。而equal_range()方法则以pair的方式把两个iterator返回。
*/
pair<map<int, char>::iterator, map<int, char>::iterator> p;
p = mp1.equal_range(11);
cout << (p.first)->second << " " << (p.second)->second << endl;// 输出a c
cout << (mp1.lower_bound(11))->second <<" "<< (mp1.upper_bound(11))->second << endl;// 输出a c
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。