C++中map的常用方法
/************************************************************************/
/*
Map是STL的一個關聯容器,它提供一對一(其中第一個可以稱為關鍵字,每個關鍵字只能在map中出現一次,
第二個可能稱為該關鍵字的值)的資料處理能力,由於這個特性,它完成有可能在我們處理一對一資料的時候,
在程式設計上提供快速通道。這裡說下map內部資料的組織,map內部自建一顆紅黑樹(一種非嚴格意義上的平衡二叉樹),
這顆樹具有對資料自動排序的功能,所以在map內部所有的資料都是有序的,後邊我們會見識到有序的好處。
1. map的建構函式
map<int, string> maphai;
map<char,int> maphai;
map<string,char> mapstring;
map<string,int> mapstring;
map<int ,char>mapint;
map<char,string>mapchar;
*/
/************************************************************************/
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main(){
// 第一種:用insert函式插入pair資料
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
mapStudent.insert(pair<int,string>(3,"student_four"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
cout<<iter->first<<""<<iter->second<<endl;
}
// 用insert函式插入value_type資料,下面舉例說明
map<int, string> mapStudent1;
mapStudent1.insert(map<int, string>::value_type (1, "student_one"));
mapStudent1.insert(map<int, string>::value_type (2, "student_two"));
mapStudent1.insert(map<int, string>::value_type (3, "student_three"));
mapStudent1.insert(map<int,string>::value_type (3,"student_four"));
map<int, string>::iterator iter1;
for(iter1 = mapStudent.begin(); iter1 != mapStudent.end(); iter1++){
cout<<iter1->first<<""<<iter1->second<<endl;
}
// 用陣列方式插入資料,下面舉例說明
map<int, string> mapStudent2;
mapStudent2[1] = "student_one";
mapStudent2[2] = "student_two";
mapStudent2[3] = "student_three";
mapStudent2[3] = "student_four";
map<int, string>::iterator iter2;
for(iter2 = mapStudent2.begin(); iter2 != mapStudent2.end(); iter2++){
cout<<iter2->first<<""<<iter2->second<<endl;
}
// 演示插入成功與否問題
map<int, string> mapStudent3;
pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent3.insert(pair<int, string>(1, "student_one"));
if(Insert_Pair.second == true){
cout<<"Insert Successfully"<<endl;
}else{
cout<<"Insert Failure"<<endl;
}
Insert_Pair = mapStudent3.insert(pair<int, string>(1, "student_two"));
if(Insert_Pair.second == true){
cout<<"Insert Successfully"<<endl;
}else{
cout<<"Insert Failure"<<endl;
}
map<int, string>::iterator iter3;
for(iter3 = mapStudent3.begin(); iter3!=mapStudent3.end(); iter3++)
{
cout<<iter3->first<<""<<iter3->second<<endl;
}
// 陣列插入在資料覆蓋上的效果
map<int, string> mapStudent4;
mapStudent4[1] = "student_one";
mapStudent4[1] = "student_two";
mapStudent4[2] = "student_three";
map<int, string>::iterator iter4;
for(iter4 = mapStudent4.begin(); iter4 != mapStudent4.end(); iter4++){
cout<<iter4->first<<""<<iter4->second<<endl;
}
// 輸出map的長度
int nSize = mapStudent.size();
cout << nSize<<endl;
//應用反相迭代器,下面舉例說明,要體會效果,請自個動手執行程式
map<int, string> mapStudent5;
mapStudent5.insert(pair<int, string>(1, "student_one"));
mapStudent5.insert(pair<int, string>(2, "student_two"));
mapStudent5.insert(pair<int, string>(3, "student_three"));
map<int, string>::reverse_iterator iter5;
for(iter5 = mapStudent5.rbegin(); iter5 != mapStudent5.rend(); iter5++){
cout<<iter5->first<<""<<iter5->second<<endl;
}
//
map<int, string> mapStudent6;
mapStudent6.insert(pair<int, string>(1, "student_one"));
mapStudent6.insert(pair<int, string>(2, "student_two"));
mapStudent6.insert(pair<int, string>(3, "student_three"));
int nSize1 = mapStudent6.size();
//此處有誤,應該是 for(int nIndex = 1; nIndex <= nSize; nIndex++)
//by rainfish
for(int nIndex = 0; nIndex < nSize1; nIndex++){
cout<<mapStudent6[nIndex]<<endl;
}
/************************************************************************/
/* 用find函式來定位資料出現位置,它返回的一個迭代器,當資料出現時,
它返回資料所在位置的迭代器,如果map中沒有要查詢的資料,它返回的迭代器等於end函式返回的迭代器*/
/************************************************************************/
map<int, string> mapStudent7;
mapStudent7.insert(pair<int, string>(1, "student_one"));
mapStudent7.insert(pair<int, string>(2, "student_two"));
mapStudent7.insert(pair<int, string>(3, "student_three"));
map<int, string>::iterator iter7;
iter7 = mapStudent7.find(1);
if(iter7 != mapStudent7.end()){
cout<<"Find, the value is "<<iter7->second<<endl;
}else{
cout<<"Do not Find"<<endl;
}
// 判斷map 是否為空
if(mapStudent7.empty()){
cout << "map is empty" <<endl;
} else {
cout <<"map is not empty" << endl;
}
// 清除map
mapStudent7.clear();
if(mapStudent7.empty()){
cout << "map is empty" <<endl;
} else {
cout <<"map is not empty" << endl;
}
map<int, string> mapStudent8;
mapStudent8.insert(pair<int, string>(1, "student_one"));
mapStudent8.insert(pair<int, string>(2, "student_two"));
mapStudent8.insert(pair<int, string>(3, "student_three"));
map<int, string>::iterator iter8;
// 通過迭代器刪除
iter8 = mapStudent8.find(1);
mapStudent8.erase(iter8);
for(iter8 = mapStudent8.begin(); iter8 != mapStudent8.end(); iter8++){
cout<<iter8->first<<""<<iter8->second<<endl;
}
//如果刪除了會返回1,否則返回0
int retValue = mapStudent8.erase(1);
cout << retValue <<endl;
retValue = mapStudent8.erase(2);
cout << retValue <<endl;
return 1;
}
相關文章
- C++ 中各種map的使用C++
- jquery 中 $.map 的使用方法jQuery
- Java中Map的遍歷方法Java
- C++中map的使用詳解說明C++
- C++中list的使用方法及常用list操作總結C++
- 遍歷陣列的常用方法forEach,filter,map等陣列Filter
- 【JavaSE】Map集合,HashMap的常用方法put、get的原始碼解析JavaHashMap原始碼
- java中遍歷map的集中方法Java
- java中遍歷Map的4種方法Java
- js中的常用方法JS
- 【Java中遍歷Map物件的4種方法】Java物件
- c++ map和unordered_map比較C++
- Java中Array的常用方法Java
- Java 中的map - The Map Interface.Java
- C++常用字串分割方法C++字串
- Java中如何遍歷Map物件的4種方法Java物件
- node中的url常用方法解析
- Python中列表常用的方法Python
- Java中Object類的常用方法JavaObject
- Java中Scanner類的常用方法Java
- Go中的MapGo
- c++ map刪除元素C++
- C++中建立持久物件的方法C++物件
- Map get() 方法
- Map delete() 方法delete
- JavaScript map()方法JavaScript
- Linux與Windows中map類erase方法的差異LinuxWindows
- 在Watir中進行物件對映(Object Map)的方法物件Object
- hash_map中string為key的解決方法
- javascript中Date常用方法JavaScript
- Python中常用的幾個內建方法(max()/min()、filter()、map()、sorted、reduce())PythonFilter
- 【轉】C++ map的基本操作和使用C++
- js中Object那些不常用的方法JSObject
- python中file物件的常用方法Python物件
- Java中String類的常用方法Java
- Java 中 String 的常用方法(一)Java
- Java 中 String 的常用方法(二)Java
- 關於c++ STL map 和 unordered_map 的效率的對比測試C++