map 学习笔记

1.map 常用功能


/****************************************
* File Name: map.cpp
* Author: sky0917
* Created Time: 2014年06月 4日 15:49:14
****************************************/
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
/*
* map::insert 的使用
*/
map<string, int> word_count;

word_count.insert(map<string, int>::value_type("zhouzhou",1));
cout<<word_count["zhouzhou"]<<endl;

pair<string, int> p("zz",2);
word_count.insert(p);
cout<<word_count["zz"]<<endl;

word_count.insert(make_pair("zz", 1));

cout<<p.first<<" "<<p.second<<endl;
word_count["ww"] = 0;

pair<map<string, int>::iterator, bool> res = word_count.insert(make_pair("zz",8));
cout<<++res.first->second<<endl;

word_count["tt"] = 7;
cout<<"start"<<endl;
for (map<string, int>::iterator it = word_count.begin(); it != word_count.end(); ++it){
cout<<(*it).first<<" "<<(*it).second<<endl;
}
cout<<"end"<<endl;
cout<<"size = " << word_count.size()<<"----"<<endl;
/*
* 查找并读取 map 中的元素
*/

int tmp = word_count["sss"];
tmp = word_count["2sdf"]; // 这种查找方式的缺点在于如果该元素不存在,则会插入一个具有该键值的新元素
word_count["zz"] = 9;
if (word_count.count("zz")){
int cont = word_count["zz"];
}
map<string, int>::iterator iot = word_count.find("zz");

if (iot != word_count.end())
cout<<(*iot).first<<" "<<(*iot).second<<endl;

/*
* 从 map 中删除元素
*/
word_count["bbz"] = 44;
cout<<word_count["bbz"]<<" "<<endl;
cout<<"size = "<< word_count.size()<<endl;
word_count.erase("bbz");
cout<<"size = "<< word_count.size()<<endl;

return 0;
}

2. map 示例:


/****************************************
* File Name: map_test.cpp
* Author: sky0917
* Created Time: 2014年06月 4日 17:27:34
****************************************/
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

ifstream& open_file(ifstream &in, const string &file)
{
in.close(); // colse in case it was already open
in.clear(); // clear any existing errors
// if open fails, the stream will be in an invalid state
in.open(file.c_str()); // open
return in;
}
int main(int argc, char **argv){
map<string, string> trm;
string key , value;
if (argc != 3){
puts("wrong number of arguments");
exit(0);
}
ifstream mfile;
if (!open_file(mfile, argv[1])){
puts("cann‘t open the first file");
exit(0);
}
while (mfile >> key >> value){
cout<<"key = " << key <<"value = "<<value<<endl;
trm.insert(make_pair(key, value));
}

ifstream input;
if (!open_file(input, argv[2])){
puts("cann‘t open the last file");
exit(0);
}
string line;
while (getline(input, line)){
istringstream stream(line); // read the line a word at a time
string word;
bool firstword = true;
while (stream >> word){
map<string, string>::const_iterator it = trm.find(word);
if (it != trm.end())
word = it->second;
if (firstword)
firstword = false;
else
cout<<" ";
cout<<word;
}
cout<<endl;
}
return 0;
}

map 学习笔记

时间: 2024-08-05 06:42:14

map 学习笔记的相关文章

map学习笔记

collection是单列集合,map是双列集合.其中包含<k,v>键值对,注意:键具有唯一性,而值不唯一. 在此列举三个读取方式:keyset,valueset,及entryset. keyset是获取所有键的集合.valueset是获取所有值得集合.entryset是获取所有条目的集合,entry是每一个条目的意思包含其中的(键与值). 具体用法为: 1 package javastudy; 2 3 import java.util.Collection; 4 import java.ut

[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

[原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

sizzle.js学习笔记利用闭包模拟实现数据结构:字典(Map)

sizzle.js学习笔记利用闭包模拟实现数据结构:字典(Map) 这几天学习和查看了jQuery和Property这两个很流行的前端库的御用选择器组件Sizzle.js的源代码,收获还是相对多的!之前一直做使用Java语言开发,其丰富的组件类库使得开发效率那叫一个快呀!突然转来做JavaScript一时间还有点儿不适应(快半年了),不过自从看见那么多漂亮的网站和对JavaScript接触的越来越多,也发现了其中的一些乐趣.正如自己一直坚信的那样,编程语言仅仅是工具,重要的是编程思想!使用Jav

Go语言学习笔记(四) [array、slices、map]

日期:2014年7月22日 一.array[数组] 1.定义:array 由 [n]<type> 定义,n 标示 array 的长度,而 <type> 标示希望存储的内容的类型. 例如: var arr[10] int arr[0] = 1 arr[1] = 2 数组值类型的:将一个数组赋值给 另一个数组,会复制所有的元素.另外,当向函数内传递一个数组的时候,它将获得一个数组的副本,而不是数组的指针. 2.数组的复合声明.a :=[3]int{1,2,3}或简写为a:=[...]i

Go语言学习笔记十三: Map集合

Go语言学习笔记十三: Map集合 Map在每种语言中基本都有,Java中是属于集合类Map,其包括HashMap, TreeMap等.而Python语言直接就属于一种类型,写法上比Java还简单. Go语言中Map的写法比Java简单些,比Python繁琐. 定义Map var x map[string]string x : = make(map[string]string) 写法上有些奇怪,map为关键字,右侧中括号内部为key的类型,中括号外部为value的类型.一般情况下使用逗号或者冒号

Python学习笔记__4.1.1章 map/reduce

# 这是学习廖雪峰老师python教程的学习笔记 1.map函数 map() 函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回 # 比如有一个函数f(x)=x2,要把这个函数作用在一个list [1, 2, 3, 4, 5, 6, 7, 8, 9]上 >>> def f(x): ...     return x * x list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])

java/android 设计模式学习笔记(一)---单例模式

前段时间公司一些同事在讨论单例模式(我是最渣的一个,都插不上嘴 T__T ),这个模式使用的频率很高,也可能是很多人最熟悉的设计模式,当然单例模式也算是最简单的设计模式之一吧,简单归简单,但是在实际使用的时候也会有一些坑. PS:对技术感兴趣的同鞋加群544645972一起交流 设计模式总目录 java/android 设计模式学习笔记目录 特点 确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式的使用很广泛,比如:线程池(threadpool).缓存(cache).对

storm学习笔记完整记录(一)

storm有两种运行模式(本地模式和集群模式) 1. 首先创建一个类似于HelloWorld的简单程序,以便进入storm的大门,包结构如下: 2.从包结构可以知道,这是一个Maven Project,pom.xml的内容如下: <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"