map函数,java和C++的常见用法

来源:互联网 发布:接线示意图制作软件 编辑:程序博客网 时间:2024/05/27 02:30

map函数是一个很常用到的映射函数,他在算法分析的时候有着举足轻重的作用,他可以结果超大空间的问题,比如说需要开一个很大的数组来表示一组关系,比如说要记录下一个学生的学号和姓名,总共有10^9之多的学生,如果使用一维数组是承受不了的,这个时候只能使用map函数,来记录这么庞大的数组。

下边先来看下c++的map函数的用法:

#include<map>
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n = 3;
/*map<int, string> a;


for (int i = 0; i < n; i++){//可以直接使用[]运算符来表示这一个关系,这个关系已经被重载过了的,不用担心会用错
a[i] ="123";
}

a[20]="456";
map<int, string>::iterator it;
for (it = a.begin(); it != a.end(); it++){
cout << it->first << ":" << it->second << endl;
}
0 123

1 123

2 123

3 123

20 456




*/

/////////////////////////////////////////////////////////////////////////////////////
/*map<char, int> b;


for (int i = 0; i < n; i++){
b[i+'0'] = i;
}

b[20+'0']=20;
map<char,int>::iterator it2;
for (it2 = b.begin(); it2 != b.end(); it2++){
cout << it2->first << ":" << it2->second << endl;
}

0 0

1 1

2 2

3 3

20 20




*/

/////////////////////////////////////////////////////////////////////////////////////




/*map<char, string>c;
for (int i = 0; i < n; i++){
c[i + '0'] = "wang";
}

c[20+'0']="wang";
map<char,string>::iterator it3;
for (it3 = c.begin(); it3 != c.end(); it3++){
cout << it3->first << ":" << it3->second << endl;
}

0 wang

1 wang

2 wang

3 wang

20 wang




*/

/////////////////////////////////////////////////////////////////////////////////////


return 0;
}





下边来看java的map函数:

package Main;
import java.util.*;


import org.omg.PortableInterceptor.Interceptor;


import java.io.*;


class Main{
public static void cout(Object s) {
if(s instanceof Map){
//方法1,只输出value一值,key并没有,要得到key,需要用到迭代器,如方法二
// for(Object o:((Map) s).keySet()) {
// cout(((Map) s).get(o));
// }cout("");
//方法2
Iterator it=((Map) s).entrySet().iterator();
while(it.hasNext()) {
Map.Entry entry=(Map.Entry)(it.next());
cout(entry.getKey().toString()+":"+entry.getValue().toString());
}
return;
}
System.out.println(s);
}
public static void main(String args[]) {
Map map=new HashMap();
map.put(2, "123");
map.put(1, 4);
map.put('3', "456");//put函数是自动帮你排好序的,对应于key从小到大自动排序;
cout(map.get(2));cout("");
cout("----------------------");
cout(map);
cout("++++++++++++++++++++++");


map.clear();
cout(map);

cout("\\\\\\\\\\\\\\\\\\\\\\");
HashMap map1=new HashMap(); 
  map1.put("1", "A"); 
  HashMap map2 = new HashMap(); 
  map2.put("2", "B"); 
  map2.put("3", "C"); 
  map1.putAll(map2); 
  System.out.println(map1);
  //直接使用这样的输出就会是{1=A, 2=B, 3=C}
  //两个map具有重复的key
  HashMap map3=new HashMap(); 
  map3.put("1", "A"); 
  HashMap map4 = new HashMap(); 
  map4.put("1", "B"); 
  map4.put("3", "C"); 
  map3.putAll(map4); //如果发现有相同的key就会覆盖前边的value;
  //当然这里的put都会有自动从小到大排序;
  System.out.println(map3);
  
  cout("-----------------------------");
  Map<String, String > map5=new TreeMap<String,String>(
  new Comparator<String>() {
  public int compare(String a,String b) {
  return b.compareTo(a);//降序排序;
  }
  });
  for(int i=0;i<5;i++) {
  map5.put("2", "555");
  map5.put("5", "7777777");
  map5.put("4", "1");
  }//这里的key是重复的,所以后来的都会被忽略掉的。
  cout(map5);cout("");
}
}



123


----------------------
1:4
2:123
3:456
++++++++++++++++++++++
\\\\\\\\\\\
{1=A, 2=B, 3=C}
{1=B, 3=C}
-----------------------------
5:7777777
4:1
2:555


0 0
原创粉丝点击