您的位置:首页 > 编程语言 > C语言/C++

map这个小妖精(*/ω\*)

2016-01-09 14:51 363 查看
map这个小妖精(*/ω\*)

1. map是虾米咩
redis晓得咩,就是那个key-value结构的数据库,map这家伙其实就是key-value结构的容器。容器是虾米咩,还好之前看过有关的一丁点,晓得C++里有这个一种类存在,它的名字就叫做容~器~类~~~网上说,容器就是用来存储数据的咩。map是标准STL关联容器,反正有概念就好得了咩,不必太纠结了o( ̄ヘ ̄o#)

2. 它是谁家小妖精咩

来来来,让俺们看看,这小妖精的头文件是哪个咩

#include <map>
既然是map,当然是map家的,有木有猜到咩哈哈哈哈

3. 小妖精长什么样子咩
map<类型1, 类型2> 变量名称

比如酱紫咩
map<string, string> key_value;


4. 小妖精有虾米本领咩
4.1 赋值
map变量有了,俺要给它赋值咩,肿么办呢
听说map有个叫做insert的方法阔以插入数据,插插插
噫,有种污了的错觉绝不是俺的错(*/ω\*),除了使用insert外当然可以它还可以像数组那样通过下标赋值,比如酱紫:map[key] = value
下面是例子咩
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main(int argc, char **argv)
{
map<string, string> key_value;

key_value.insert(pair<string, string>("one", "yes_one")); //插插插\(≧▽≦)/
key_value["two"] = "yes_two"; //下标赋值咩O(∩_∩)O~~

return 0;
}


4.2 iteratorcplusplus说map家的成员变量有酱酱酿酿的,但俺不认识,俺只认识iterator,所以就说说iterator咩~iterator这家伙有两个成员变量,一个叫first,存储map的key,一个叫second,存储map的value。而且使用iterator就阔以遍历map晓得咩,就像酱紫滴
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main(int argc, char **argv)
{
map<string, string> key_value;
map<string, string>::iterator iter;

key_value.insert(pair<string, string>("one", "yes_one"));
key_value["two"] = "yes_two";

for(iter=key_value.begin(); iter!=key_value.end(); iter++) { //遍历看这里咩\(≧▽≦)/
cout<<iter->first<<"->"<<iter->second<<endl;  //firsh和second诶
}

return 0;
}


4.3 查找俺啾一下扔了两个数据进map里了,可俺想找到这两个数据肿么办哦QAQ,因为map是key-value结构,当然是通过key来寻找了,map有个小触手叫做find~find~哟调用find方法,会返回iterator。如果find木有找到,则iterator等于map.end(),不要在这个时候试图打印iterator的key和value,因为会发生段错误,虽然俺知道会是这个结果,但俺还是闲得去尝试了%>_<%
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main(int argc, char **argv)
{
map<string, string> key_value;
map<string, string>::iterator iter;

key_value.insert(pair<string, string>("one", "yes_one"));
key_value["two"] = "yes_two";
iter = key_value.find("one");  //看这里find小触手

if (iter == key_value.end()) {
cout<<"iter == key_value.end()\n";
return -1;
}

cout<<"iter->one = "<<iter->first<<endl;
cout<<"iter->two = "<<iter->second<<endl;

return 0;
}


4.4 删除
因为作死去打印不存在的key和value,俺如愿以偿(才木有哼哼)地dump了,所以俺很生气后果很严重,俺要把插进去的数据揪出来扔掉T_T

删除数据阔以用erase,清空数据阔以用clear
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main(int argc, char **argv)
{
map<string, string> key_value;
map<string, string>::iterator iter;

key_value.insert(pair<string, string>("one", "yes_one"));
key_value["two"] = "yes_two";
key_value["three"] = "yes_three";
for (iter=key_value.begin(); iter!=key_value.end(); iter++) {
cout<<iter->first<<"->"<<iter->second<<endl;
}
cout<<endl;

key_value.erase("two"); //看这里看着里咩
for (iter=key_value.begin(); iter!=key_value.end(); iter++) {
cout<<iter->first<<"->"<<iter->second<<endl;
}
cout<<endl;

key_value.clear(); //清空清空清空
for (iter=key_value.begin(); iter!=key_value.end(); iter++) {
cout<<iter->first<<"->"<<iter->second<<endl;
}
cout<<endl;

return 0;
}

/*
运行结果:
one->yes_one
three->yes_three
two->yes_two

one->yes_one
three->yes_three

*/


噫,又写了一堆废话,不晓得未来的俺看到后会不会有种想掐死俺的冲动咩哈哈哈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++