文章目录
🔥前言
继
set容器后,今天总结一下map容器的功能,从零到一快速掌握基本使用与常用接口。map在STL编程中与vector、list、set具有同等重要的地位,键值对的方式存储元素在查找时很是高效,那么下面正式开始map容器的学习之旅。
特点:
pairkey(键),起到索引作用,第二个元素为value(值)
本质:
二者区别:
map不允许容器中有重复key值元素multimap允许容器中有重复key值元素功能:
函数原型:
map<T1, T2> mp; 默认构造函数:map(const map &mp); 拷贝构造函数map& operator=(const map &mp); 重载等号操作符代码示例:
// 遍历容器
void printInfo(map<int,int>& mp)
{
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
// 构造和赋值
void test01()
{
// 默认构造
map<int, int> mp1;
// 利用二元组显示创建
mp1.insert(make_pair(1,1));
mp1.insert(make_pair(5,2));
mp1.insert(make_pair(4,5));
printInfo(mp1);
// 拷贝构造
map<int, int> mp2(mp1);
printInfo(mp2);
// 赋值
map<int, int> mp3;
mp3 = mp2;
printInfo(mp3);
}
功能:
函数原型:
size(); 返回容器中元素的数目empty(); 判断容器是否为空swap(st); 交换两个集合容器代码示例:
// 大小和交换
void test02()
{
// 大小
map<int, int>mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(pair<int, int>(3, 30));
mp.insert(pair<int, int>(2, 20));
if (mp.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m的大小为: " << mp.size() << endl;
}
// 交换
map<int, int>m2;
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
cout << "交换前" << endl;
printInfo(mp);
printInfo(m2);
cout << "交换后" << endl;
mp.swap(m2);
printInfo(mp);
printInfo(m2);
}
功能:
函数原型:
insert(elem); 在容器中插入元素。clear(); 清除所有元素erase(pos); 删除pos迭代器所指的元素,返回下一个元素的迭代器。erase(beg, end); 删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。erase(key); 删除容器中值为key的元素。代码示例:
// 插入和删除
void test03()
{
//插入操作
map<int, int> mp;
//第一种插入方式
mp.insert(pair<int, int>(1, 10));
//第二种插入方式,推荐
mp.insert(make_pair(2, 20));
//第三种插入方式
mp.insert(map<int, int>::value_type(3, 30));
//第四种插入方式,不推荐:当不存在此值就会自动创建键,值为0
mp[4] = 40;
// 模拟误操作
cout << "下标为6的值为:"<<mp[6] << endl;
printInfo(mp);
//删除,按迭代器
mp.erase(mp.begin());
printInfo(mp);
// 删除,按下标
mp.erase(3);
mp.erase(6);
printInfo(mp);
//清空
mp.erase(mp.begin(), mp.end());
mp.clear();
printInfo(mp);
}
功能:
函数原型:
find(key); 查找key是否存在,若存在,返回该键对应的迭代器;若不存在,返回end();count(key); 统计key的元素个数代码示例:
// 查找和统计
void test04()
{
map<int, int>mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(pair<int, int>(2, 20));
mp.insert(pair<int, int>(4, 40));
mp.insert(pair<int, int>(3, 30));
//查找
map<int, int>::iterator pos = mp.find(3);
if (pos != mp.end())
{
cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
int num = mp.count(2);
cout << "num = " << num << endl;
}
与 set 容器一样,定义排序规则需要在创建容器的时候就指定,同样需要借助仿函数,也就是后面会提到的 谓词
// 内置数据类型排序
class descmp
{
public:
bool operator()(int val1, int val2) const {
// 指定为降序排列
return val1 > val2;
}
};
void test05()
{
cout << "降序排序插入:" << endl;
map<int, int,descmp> mp;
mp.insert(make_pair(3, 2));
mp.insert(make_pair(7, 4));
mp.insert(make_pair(6, 1));
mp.insert(make_pair(8, 5));
mp.insert(make_pair(1, 3));
for (auto it = mp.begin(); it != mp.end(); it++) {
cout << "key = " << it->first << " value = " << it->second << endl;
}
}

仿函数需要指定为
const常函数类型,否则将会出现数据不完整错误
进行自定义数据类型排序的时候要注意,使用仿函数定义排序规则是针对 键 的,因此要把定义的类、结构体放在泛型的第一个参数上(别问,问就是我看了半天源码才发现的…)
接下来看看我设计的小案例:
// 自定义数据类型排序
class Hero
{
public:
// 构造方法
Hero(string name, int age) :name(name), age(age) {}
// 属性
string name;
int age;
};
class Cmp_hero
{
public:
bool operator()(const Hero& h1, const Hero& h2)const {
return h1.age < h2.age;
}
};
void test06()
{
map<Hero, int, Cmp_hero> mh;
Hero h1("赵云", 42);
Hero h2("曹操", 43);
Hero h3("孙策", 39);
Hero h4("刘备", 40);
Hero h5("关羽", 41);
mh.insert(make_pair(h1, 12000));
mh.insert(make_pair(h2, 14000));
mh.insert(make_pair(h3, 10000));
mh.insert(make_pair(h4, 18000));
mh.insert(make_pair(h5, 16000));
for (auto t = mh.begin(); t != mh.end(); t++) {
cout << "姓名:" << t->first.name << " 年龄:" << t->first.age
<< " 赏金:" << t->second << endl;
}
}

其实更合理的设计应该是按照赏金大小来排序,我之所以这样设计是想介绍一下当键是类的时候该怎么处理:仿函数参数列表中的变量若是 引用形式 必须用
const修饰(若不是引用形式则不用加 const),而且函数也应该是常函数。
map 容器键值对存储的使用是非常广泛的,下篇博客来做一个员工分组的具体案例,巩固一些 STL常用容器的使用,大家可以订阅专栏,方便查阅和复习。
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c