首页 技术 正文
技术 2022年11月18日
0 收藏 834 点赞 4,624 浏览 1072 个字

map  :关联式容器,使用时是以属性值对的方式进行使用,例如:<key,value>。map key值唯一,相同的key值插入时只会保留一个。除此之外,map的特点还包括:
1、map底层是一个红黑树,插入记录时根据对记录进行排序。
2、使用find方法进行查找时,时间复杂度为LOG(N)
3、修改记录时根据key值进行修改。
5、可以对key值进行修改,具体的方法是…….不表,一般没必要。

#include <iostream>
#include <map>
using namespace std;
struct classcomp {
bool operator() (const char& lhs, const char& rhs) const{
return lhs>=rhs;
}
};
int main(int argc, char *argv[]) {
std::map<char,int> foo,bar;
// map 初始化、交换和遍历
foo['x']=100;
foo['y']=200;bar['a']=11;
bar['b']=22;
bar['c']=33;foo.swap(bar);
std::cout << "foo contains:\n";
for (std::map<char,int>::iterator it=foo.begin(); it!=foo.end(); ++it){
std::cout << it->first << " => " << it->second << '\n';
}std::cout << "bar contains:\n";
for (std::map<char,int>::iterator it=bar.begin(); it!=bar.end(); ++it){
std::cout << it->first << " => " << it->second << '\n';
}
//map key使用函数
std::cout<<"use comp funcation to initialize the map:...."<<std::endl;
std::map<char,int,classcomp> first;
first.insert(std::make_pair('a',100));
first.insert(std::make_pair('y',200));
for (std::map<char,int>::iterator it=first.begin(); it!=first.end(); ++it){
std::cout << it->first << " => " << it->second << '\n';
}
return 0;
}

结果如下:

温故而知新——map

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,122
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,594
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,439
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,210
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,846
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,931