map是C++98中引入的二叉树数据结构
STLmap具有以下一些特点:
1、map有四个参数
2、count()和find()函数传入的参数都是key
3、*findIter迭代器返回的是const int
4、在map中插入元素需要用make_pair
5、insert有返回两个参数第一个是迭代器的位置,第二个表示插入是否成功
6、emplace比insert插入效率更高
7、以[]传入的类型需要有默认的构造函数
8、用at定位会抛出异常,一般配合try catch使用
9、如果用以下方法赋值会重新生成pair进行拷贝构造函数赋值由于不是同一个东西,影响效率
std::pair<int,std::string> obj;
obj = *findIter;
应该用引用的方式:
const std::pair<int, std::string>& obj = *findIter;
10、查找元素一般用find比较多
auto findIter = b.find(10);
if(findIter != std::end(b) /* b.end() */) {
auto& v = (*findIter).second;
} else {
}
也可以自己定义模板获取值,更高效一点
template <class Map>
typename Map::mapped_type get_default(
const Map &map, const typename Map::key_type &key,
const typename Map::mapped_type &dflt = typename Map::mapped_type()) {
auto pos = map.find(key);
return (pos != map.end() ? pos->second : dflt);
}
以下关于map完整接口代码:
static void mapPart() {
// map multimap是c++98中引入的二叉树数据结构
// namespace std {
// template<typename Key, typename T, typename Compare = less<Key>, typename Allocator =
// allocator<pair<const Key, T>>
// class map;
// template<typename Key, typename T, typename Compare = less<Key>, typename Allocator =
// allocator<pair<const Key, T>>
// class multimap;
// }
// 特点自动将元素排序
// 插入和删除查找O(logn)
// 必须Key元素必须支持严格的弱顺序
// (1) x < y == true, y < x == false
// (2) x < y == true, y < z == true, x < z == true
// (3) x < x === false
// (4) a == b, b == c, c == a
// 不能改变Key元素的值
//
using Group = std::map<int,std::string>;
Group a;
Group b = a;
Group c(a);
Group d(c.begin(), c.end());
Group g({{1, "a"}, {2,"test"}, {3, "test"}});
d.empty();
d.size();
d.max_size();
// operator == != < > <= >=
// special
auto keycomp = c.key_comp();
auto valuecomp = c.value_comp();
// 赋值
b = g;
// 交换
b.swap(a);
std::swap(a,b);
// 迭代器相关
a.begin();
a.end();
a.cbegin();
a.cend();
a.rbegin();
a.rend();
a.crbegin();
a.crend();
auto iterBegin = a.begin();
//算法函数
// map count 0 1
// multimap count >= 0
auto num = a.count(1);
auto findIter = a.find(1);
if(findIter == a.end()) {
// not finded
} else {
const std::pair<int, std::string>& obj = *findIter;
//*findIter; // std::pair<const int, std::string>&
}
auto lower = a.lower_bound(1);
if(lower != a.end()) {
if(*lower.first == 1) {
// has 1
}
}
auto high = a.upper_bound(1);
auto range = a.equal_range(1); // return std::make_pair(a.lower_bound(1), a.upper_bound(1));
auto eraseIter = b.erase(b.begin());
eraseIter = b.erase(b.begin(), b.end());
auto state = b.insert(std::make_pair(100, "good")); // state is a pair<Iterator, bool>
auto insertIter = b.insert(c.begin(), c.end());
b.emplace(std::make_pair(10, "has it"));
b.emplace(11, std::string("again")); // b.emplace( std::pair<const int,std::string>(10, std::string("again"));
b.emplace(12, "third"); // b.emplace(std::pair<const int, std::string>(10, "third"));
b.emplace_hint(b.end(), 13, "haha");
// []
// auto string
auto& info = b[10];
// [] -> auto iter = b.insert(std::make_pair(13, std::string())).first; return (*iter).second;
b[13];
// at
try {
auto& findInfo = b.at(10); // const
} catch(...) {
}
auto findIter = b.find(10);
if(findIter != std::end(b) /* b.end() */) {
auto& v = (*findIter).second;
} else {
}
auto info = get_default(b, 10);
if(info.empty()) {
} else {
}
}
template <class Map>
typename Map::mapped_type get_default(
const Map &map, const typename Map::key_type &key,
const typename Map::mapped_type &dflt = typename Map::mapped_type()) {
auto pos = map.find(key);
return (pos != map.end() ? pos->second : dflt);
}