STL-map
map的构造
// 1. 默认构造
explicit map (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
// 2. 迭代器构造
template <class InputIterator>
map (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
// 3. 拷贝构造
map (const map& x);
map的迭代器
iterator begin();
const_iterator begin() const;
反向迭代器
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
C++11 cbegin() cend()
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
返回一个 const 迭代器, 不能用于修改元素
#include <iostream>
#include <map>
int main()
{
std::map<char, int> mymap;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
std::cout << "mymap contains:" << std::endl;
for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
std::cout << " [" << (*it).first << ':' << (*it).second << ']' << std::endl;
std::cout << '\n';
system("pause");
return 0;
}
判空 empty 获取大小 size
operator[] : 根据 key 获得相应的 val
mapped_type& operator[] (const key_type& k);
如果 key 不存在, 返回空, 不会检查 key 的合法性
at
mapped_type& at (const key_type& k);
const mapped_type& at (const key_type& k) const;
作用和 operator[] 相似, 检查 key 的合法性, 如果 key 不存在会抛异常
举例:
// map 打印函数
template<class container>
void printContainer(container& s)
{
typename container::const_iterator it = s.begin();
// typename container::reverse_iterator it = s.rbegin();
// while(it != s.end())
while(it != s.end())
{
cout << it->first << " => " << it->second << endl;
++it;
}
cout << endl;
}
void TestMap01()
{
// map 的构造
// 1. 默认构造
map<int, char> first;
first[0] = 'a';
first[1] = 'b';
first[2] = 'c';
first[3] = 'd';
// 2, 拷贝构造
map<int, char> second(first);
// 3. 迭代器构造
map<int, char> third(first.begin(), first.end());
// 打印 map
printContainer(first);
// print(second);
if(!first.empty())
{
cout << first.size() << endl;
}
cout << first[0] << endl;
// cout << first.at(5) << endl; // 会检查 key 的合法性, 抛异常
}
insert
pair<iterator,bool> insert (const value_type& val);
iterator insert (iterator position, const value_type& val);
template <class InputIterator>
void insert (InputIterator first, InputIterator last);
erase
void erase (iterator position);
size_type erase (const key_type& k);
void erase (iterator first, iterator last);
clear
void clear();
清空 map 中的元素
举例:
void TestMap02()
{
// 插入
map<int, char> mymap;
mymap.insert(pair<int, char>(1, 'x'));
mymap.insert(pair<int, char>(2, 'y'));
mymap.insert(pair<int, char>(3, 'z'));
printContainer(mymap);
pair<map<int, char>::iterator, bool> ret;
ret = mymap.insert(pair<int, char>(3, 'v'));
if(ret.second == false)
{
cout << "3 已经存在, 对应的 val => " << ret.first->second << endl;
}
map<int, char>::iterator it = mymap.begin();
mymap.insert(it, pair<int, char>(4, 'b'));
mymap.insert(it, pair<int, char>(5, 'n'));
printContainer(mymap);
map<int, char> othermap;
othermap.insert(mymap.begin(), mymap.find(4)); // 从mymap的第一个到 4 的前一个
printContainer(othermap);
// 删除
mymap.erase(2); // 指定 key 删除
printContainer(mymap);
it = mymap.find(5);
mymap.erase(it); // 根据 find 返回的迭代器删除
printContainer(mymap);
it = mymap.find(1);
mymap.erase(it, mymap.end()); // 删除一个区间
printContainer(mymap);
cout << "mymap size: " << mymap.size() << endl;
cout << "othermap size: " << othermap.size() << endl;
othermap.clear();
cout << "othermap size: " << othermap.size() << endl;
}
find
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
返回元素的迭代器
count
size_type count (const key_type& k) const;
判断 key 是否存在, 存在返回 1, 不存在返回 0
插入
template <class T1,class T2>
pair<T1,T2> make_pair (T1 x, T2 y)
{
return ( pair<T1,T2>(x,y) );
}
void TestMap_WaterMargin()
{
map <string, string> mymap;
mymap.insert(pair<string, string>("宋江", "及时雨"));
mymap.insert(pair<string, string>("吴用", "智多星"));
mymap.insert(make_pair("李逵", "黑旋风"));
mymap.insert(make_pair("武松", "行者"));
mymap.insert(make_pair("鲁智深", "花和尚"));
cout << "Map size: " << mymap.size() << endl;
// 插入 key 存在, val 不同的元素
mymap.insert(make_pair("李逵", "铁牛"));
cout << "Map size: " << mymap.size() << endl;
printContainer(mymap);
// 通过 key 查找 val
cout << "武松: " << mymap["武松"] << endl;
// 修改 key 对应的 val
cout << "李逵: " << mymap["李逵"] << endl;
mymap["李逵"] = "铁牛";
cout << "李逵: " << mymap["李逵"] << endl;
// 通过 [] 访问 key 对应的 val 时, 如果 key 不存在, 会创建一个默认 val 的键值对
cout << "武大郎: " << mymap["武大郎"] << endl;
cout << "Map size: " << mymap.size() << endl;
cout << "武大郎: " << mymap["武大郎"] << endl; // val 默认为空
// 删除
mymap.erase(mymap.find("武大郎"));
cout << "Map size: " << mymap.size() << endl;
// 清空
mymap.clear();
cout << "Map size: " << mymap.size() << endl;
}
multimap
与 map 唯一的区别就是 key 可以重复
void TestMultiMap()
{
multimap <string, string> mymap;
mymap.insert(pair<string, string>("宋江", "及时雨"));
mymap.insert(pair<string, string>("吴用", "智多星"));
mymap.insert(make_pair("李逵", "黑旋风"));
mymap.insert(make_pair("武松", "行者"));
mymap.insert(make_pair("鲁智深", "花和尚"));
printContainer(mymap);
cout << "Map size: " << mymap.size() << endl;
// 插入 key 存在, val 不同的元素
mymap.insert(make_pair("李逵", "铁牛"));
cout << "Map size: " << mymap.size() << endl;
printContainer(mymap);
multimap <int, int> m;
for(int i=0; i<10; i++)
{
m.insert(make_pair(i, i));
}
m.insert(make_pair(5, 11));
m.insert(make_pair(5, 12));
m.insert(make_pair(5, 13));
m.insert(make_pair(5, 14));
printContainer(m);
cout << "key 为 5 的元素个数: " << m.count(5) << endl;
multimap<int, int>::iterator it;
it = m.lower_bound(5);
cout << it->first << " -> " << it->second << endl;
it = m.upper_bound(5);
cout << it->first << " -> " << it->second << endl;
cout << endl;
typedef multimap<int, int>::iterator Iter;
pair<Iter, Iter> ret = m.equal_range(5);
cout << ret.first->first << " -> " << ret.first->second << endl;
cout << ret.second->first << " -> " << ret.second->second << endl;
}