???Java????C++???????????
???????????? ???????[ 2013/4/17 9:53:25 ] ????????
???????????????????map vs TreeMap????
????????????Java???б????????????Map?????????????????????????????????????TreeMap??HashMap????????????????????????????Ч????????????????????????Java??C++??STL?????map???????書??????Java?е?TreeMap??????HashMap????????н???C++????????????????????е?????C++???????????????????????????????????????????????hash_map??????????????STL?е?????????????
?????????????????????о?TreeMap??Java????map??C++?????????????????????????????????????????????????????????????????и?????????Щ???????????????????
????1????????????????????????????????Java??TreeMap???????????????????????????????????????C++?е?map????????????????????иü?????????????2???????????????????????????У???????Java????????????
public class MyTest {
private static void testGet() {
TreeMap<String??String> tm = new TreeMap<String??String>();
tm.put("Hello"?? "World");
if (tm.get("Hello1") == null)
System.out.println("Hello1 is NOT found.");
}
public static void main(String[] args) {
TreeMap<Integer??Integer> tm = new TreeMap<Integer??Integer>();
tm.put(5?? 10);
tm.put(5?? 20);
Set<Entry<Integer??Integer>> entries = tm.entrySet();
Iterator<Entry<Integer??Integer>> it = entries.iterator();
while (it.hasNext()) {
Entry<Integer??Integer> e = it.next();
System.out.printf("key = %d?? value = %d
"??e.getKey().intValue()??e.getValue().intValue());
}
}
}
//The count of the TreeMap is 1
//key = 5?? value = 20
??????????C++????????????
using namespace std;
int main()
{
map<int??int> m;
m.insert(make_pair(5??10));
m.insert(make_pair(5??20));
printf("The count of the map is %d.
"??m.size());
map<int??int>::iterator it = m.begin();
for (; it != m.end(); ++it) {
printf("key = %d?? value = %d
"??(*it).first??(*it).second);
}
return 0;
}
//The count of the map is 1
//key = 5?? value = 10
????????????????????????????????????????????????????C++?????????????е?Java????????????????????????????????????????????????????ж?ü???????????????????????м???????????????????????????????У???????????м????????????ü????????????????????????????????????????????????????????????????????????????????г?????????Ч???????????????????Щ?????????????C++????
using namespace std;
int main()
{
map<int??int> m;
m.insert(make_pair(5??10));
map<int??int>::iterator it = m.find(5);
if (it != m.end())
m.erase(it);
m.insert(make_pair(5??20));
//?????????·??
//if (it != m.end())
// it->second = 20;
//else
// m.insert(make_pair(5??20));
printf("The count of the map is %d.
"??m.size());
it = m.begin();
for (; it != m.end(); ++it)
printf("key = %d?? value = %d
"??(*it).first??(*it).second);
return 0;
}
//The count of the map is 1
//key = 5?? value = 10
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11