STL中std::map用法詳解
std::map<int, string> mapStudent;
1. map的建構函式
map共提供了6個建構函式,這塊涉及到記憶體分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這裡要說下的就是,我們通常用如下方法構造一個map:
map<int, string> mapStudent;
2. 資料的插入
在構造map容器後,我們就可以往裡面插入資料了。這裡講三種插入資料的方法:
第一種:用insert函式插入pair資料,下面舉例說明(以下程式碼雖然是隨手寫的,應該可以在VC和GCC下編譯通過,大家可以執行下看什麼效果,在VC下請加入這條語句,遮蔽4786警告 #pragma warning (disable:4786) )
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
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”));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->second<<end;
}
}
第二種:用insert函式插入value_type資料,下面舉例說明
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->second<<end;
}
}
第三種:用陣列方式插入資料,下面舉例說明
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[2] = “student_two”;
mapStudent[3] = “student_three”;
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->second<<end;
}
}
以上三種用法,雖然都可以實現資料的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用insert函式插入資料,在資料的插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是插入資料不了的,但是用陣列方式就不同了,它可以覆蓋以前該關鍵字對應的值,用程式說明
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
上面這兩條語句執行後,map中1這個關鍵字對應的值是“student_one”,第二條語句並沒有生效,那麼這就涉及到我們怎麼知道insert語句是否插入成功的問題了,可以用pair來獲得是否插入成功,程式如下
Pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
我們通過pair的第二個變數來知道是否插入成功,它的第一個變數返回的是一個map的迭代器,如果插入成功的話Insert_Pair.second應該是true的,否則為false。
下面給出完成程式碼,演示插入成功與否問題
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
Map<int, string> mapStudent;
Pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
If(Insert_Pair.second == true)
{
Cout<<”Insert Successfully”<<endl;
}
Else
{
Cout<<”Insert Failure”<<endl;
}
Insert_Pair = mapStudent.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 iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->second<<end;
}
}
大家可以用如下程式,看下用陣列插入在資料覆蓋上的效果
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
Map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[1] = “student_two”;
mapStudent[2] = “student_three”;
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->second<<end;
}
}
3. map的大小
在往map裡面插入了資料,我們怎麼知道當前已經插入了多少資料呢,可以用size函式,用法如下:
Int nSize = mapStudent.size();
4. 資料的遍歷
這裡也提供三種方法,對map進行遍歷
第一種:應用前向迭代器,上面舉例程式中到處都是了,略過不表
第二種:應用反相迭代器,下面舉例說明,要體會效果,請自個動手執行程式
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
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”));
map<int, string>::reverse_iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
Cout<<iter->first<<” ”<<iter->second<<end;
}
}
第三種:用陣列方式,程式說明如下
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
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”));
int nSize = mapStudent.size()
//此處有誤,應該是 for(int nIndex = 1; nIndex <= nSize; nIndex++)
//by rainfish
for(int nIndex = 0; nIndex < nSize; nIndex++)
{
Cout<<mapStudent[nIndex]<<end;
}
}
5. 資料的查詢(包括判定這個關鍵字是否在map中出現)
在這裡我們將體會,map在資料插入時保證有序的好處。
要判定一個數據(關鍵字)是否在map中出現的方法比較多,這裡標題雖然是資料的查詢,在這裡將穿插著大量的map基本用法。
這裡給出三種資料查詢方法
第一種:用count函式來判定關鍵字是否出現,其缺點是無法定位資料出現位置,由於map的特性,一對一的對映關係,就決定了count函式的返回值只有兩個,要麼是0,要麼是1,出現的情況,當然是返回1了
第二種:用find函式來定位資料出現位置,它返回的一個迭代器,當資料出現時,它返回資料所在位置的迭代器,如果map中沒有要查詢的資料,它返回的迭代器等於end函式返回的迭代器,程式說明
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
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”));
map<int, string>::iterator iter;
iter = mapStudent.find(1);
if(iter != mapStudent.end())
{
Cout<<”Find, the value is ”<<iter->second<<endl;
}
Else
{
Cout<<”Do not Find”<<endl;
}
}
第三種:這個方法用來判定資料是否出現,是顯得笨了點,但是,我打算在這裡講解
Lower_bound函式用法,這個函式用來返回要查詢關鍵字的下界(是一個迭代器)
Upper_bound函式用法,這個函式用來返回要查詢關鍵字的上界(是一個迭代器)
例如:map中已經插入了1,2,3,4的話,如果lower_bound(2)的話,返回的2,而upper-bound(2)的話,返回的就是3
Equal_range函式返回一個pair,pair裡面第一個變數是Lower_bound返回的迭代器,pair裡面第二個迭代器是Upper_bound返回的迭代器,如果這兩個迭代器相等的話,則說明map中不出現這個關鍵字,程式說明
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
Map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[3] = “student_three”;
mapStudent[5] = “student_five”;
map<int, string>::iterator iter;
iter = mapStudent.lower_bound(2);
{
//返回的是下界3的迭代器
Cout<<iter->second<<endl;
}
iter = mapStudent.lower_bound(3);
{
//返回的是下界3的迭代器
Cout<<iter->second<<endl;
}
iter = mapStudent.upper_bound(2);
{
//返回的是上界3的迭代器
Cout<<iter->second<<endl;
}
iter = mapStudent.upper_bound(3);
{
//返回的是上界5的迭代器
Cout<<iter->second<<endl;
}
Pair<map<int, string>::iterator, map<int, string>::iterator> mapPair;
mapPair = mapStudent.equal_range(2);
if(mapPair.first == mapPair.second)
{
cout<<”Do not Find”<<endl;
}
Else
{
Cout<<”Find”<<endl;
}
mapPair = mapStudent.equal_range(3);
if(mapPair.first == mapPair.second)
{
cout<<”Do not Find”<<endl;
}
Else
{
Cout<<”Find”<<endl;
}
}
6. 資料的清空與判空
清空map中的資料可以用clear()函式,判定map中是否有資料可以用empty()函式,它返回true則說明是空map
7. 資料的刪除
這裡要用到erase函式,它有三個過載了的函式,下面在例子中詳細說明它們的用法
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
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”));
//如果你要演示輸出效果,請選擇以下的一種,你看到的效果會比較好
//如果要刪除1,用迭代器刪除
map<int, string>::iterator iter;
iter = mapStudent.find(1);
mapStudent.erase(iter);
//如果要刪除1,用關鍵字刪除
Int n = mapStudent.erase(1);//如果刪除了會返回1,否則返回0
//用迭代器,成片的刪除
//一下程式碼把整個map清空
mapStudent.earse(mapStudent.begin(), mapStudent.end());
//成片刪除要注意的是,也是STL的特性,刪除區間是一個前閉後開的集合
//自個加上遍歷程式碼,列印輸出吧
}
8. 其他一些函式用法
這裡有swap,key_comp,value_comp,get_allocator等函式,感覺到這些函式在程式設計用的不是很多,略過不表,有興趣的話可以自個研究
9. 排序
這裡要講的是一點比較高深的用法了,排序問題,STL中預設是採用小於號來排序的,以上程式碼在排序上是不存在任何問題的,因為上面的關鍵字是int型,它本身支援小於號運算,在一些特殊情況,比如關鍵字是一個結構體,涉及到排序就會出現問題,因為它沒有小於號操作,insert等函式在編譯的時候過不去,下面給出兩個方法解決這個問題
第一種:小於號過載,程式舉例
#include <map>
#include <string>
Using namespace std;
Typedef struct tagStudentInfo
{
Int nID;
String strName;
}StudentInfo, *PStudentInfo; //學生資訊
Int main()
{
int nSize;
//用學生資訊對映分數
map<StudentInfo, int>mapStudent;
map<StudentInfo, int>::iterator iter;
StudentInfo studentInfo;
studentInfo.nID = 1;
studentInfo.strName = “student_one”;
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
studentInfo.nID = 2;
studentInfo.strName = “student_two”;
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;
}
以上程式是無法編譯通過的,只要過載小於號,就OK了,如下:
Typedef struct tagStudentInfo
{
Int nID;
String strName;
Bool operator < (tagStudentInfo const& _A) const
{
//這個函式指定排序策略,按nID排序,如果nID相等的話,按strName排序
If(nID < _A.nID) return true;
If(nID == _A.nID) return strName.compare(_A.strName) < 0;
Return false;
}
}StudentInfo, *PStudentInfo; //學生資訊
第二種:仿函式的應用,這個時候結構體中沒有直接的小於號過載,程式說明
#include <map>
#include <string>
Using namespace std;
Typedef struct tagStudentInfo
{
Int nID;
String strName;
}StudentInfo, *PStudentInfo; //學生資訊
Classs sort
{
Public:
Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
{
If(_A.nID < _B.nID) return true;
If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;
Return false;
}
};
Int main()
{
//用學生資訊對映分數
Map<StudentInfo, int, sort>mapStudent;
StudentInfo studentInfo;
studentInfo.nID = 1;
studentInfo.strName = “student_one”;
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
studentInfo.nID = 2;
studentInfo.strName = “student_two”;
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
}
相關推薦
STL中std::map用法詳解
std::map<int, string> mapStudent;1. map的建構函式map共提供了6個建構函式,這塊涉及到記憶體分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這裡要說下的就是,我們通常用如下方法構造一個map:map&l
C++中的map用法詳解
時間 占用 sort函數 數組 例程 通道 組織 sso 查找 Map是 STL的一個關聯容器,它提供一對一(其中第一個可以稱為關鍵字,每個關鍵字只能在map中出現一次,第二個可能稱為該關鍵字的值)的數據 處理能力,由 於這個特性,它完成有可能在我們處理一對一數據的
STL中map用法詳解
Map是STL的一個關聯容器,它提供一對一(其中第一個可以稱為關鍵字,每個關鍵字只能在map中出現一次,第二個可能稱為該關鍵字的值)的資料處理能力,由於這個特性,它完成有可能在我們處理一對一資料的時候,在程式設計上提供快速通道。這裡說下map內部資料的組織,map內部自建一顆
C++中的STL中map用法詳解
Map是STL的一個關聯容器,它提供一對一(其中第一個可以稱為關鍵字,每個關鍵字只能在map中出現一次,第二個可能稱為該關鍵字的值)的資料 處理能力,由於這個特性,它完成有可能在我們處理一對一資料的時候,在程式設計上提供快速通道。這裡說下map內部資料的組織,map內部自建一顆紅黑樹(一 種非嚴格意義上的平衡
C++中map用法詳解
Map是c++的一個標準容器,她提供了很好一對一的關係,在一些程式中建立一個map可以起到事半功倍的效果,總結了一些map基本簡單實用的操作! 1. map最基本的建構函式; map<string , int >mapstring; map<int ,string
JavaScript中return的用法詳解
style 返回 www log tle blog 意思 charset fun 1、定義:return 從字面上的看就是返回,官方定義return語句將終止當前函數並返回當前函數的值,可以看下下面的示例代碼: <!DOCTYPE html><html l
java中的instanceof用法詳解
定義 xtend print 繼承 interface 參數 保留 如果 ack instanceof是Java的一個二元操作符(運算符),也是Java的保留關鍵字。它的作用是判斷其左邊對象是否為其右邊類的實例,返回的是boolean類型的數據。用它來判斷某個對象是否是
C#中HttpWebRequest的用法詳解
網站 default 編碼方式 對數 c# toarray collect acc like 本文實例講述了C#中HttpWebRequest的用法。分享給大家供大家參考。具體如下: HttpWebRequest類主要利用HTTP 協議和服務器交互,通常是通過 GET 和
【轉載】 c++中static的用法詳解
ostream 並不會 style 轉載 程序員 都是 note 每次 reference 出處: http://blog.csdn.net/majianfei1023/article/details/45290467 C 語言的 static 關鍵字有三種(具體來說是
mysql中 where in 用法詳解
MySQL這裏分兩種情況來介紹 1、in 後面是記錄集,如: select * from table where uname in(select uname from user); 2、in 後面是字符串,如: select * from table where uname
linux 中特殊符號用法詳解
引號 zabbix 目錄 sele cond 特殊符號 zab set speed 星號(*) 當用於shell變量時, [root@zabbix_nuc ~]# sql=‘select * from mysql.user‘ [root@zabbix_nuc ~]# ech
C#中string.format用法詳解
個數 date 其中 位置 tr1 bsp 位數 數值 日期格式化 tring.Format 方法的幾種定義: String.Format (String, Object) 將指定的 String 中的格式項替換為指定的 Object 實例的值的文本等效項。String.F
linux中mariadb基本用法詳解(企業級)
資料庫 表的每一個列名字的頭 叫做欄位 是高階的exel表格軟體 資料庫種類 sqlserver sqllite db2 oracle > mysql 比較多 其中my
STL-stack的常用用法詳解
stack是資料結構裡面的棧,存入其中的資料具有“先入後出”的特點,適合處理具有該輸入特點的資料。常用的用法是以下5個。 push() push(x)將元素入棧,時間複雜度為O(1); top() 獲得棧頂的
php 中的closure用法詳解
php 中的closure用法詳解 Closure,匿名函式,是php5.3的時候引入的,又稱為Anonymous functions。字面意思也就是沒有定義名字的函式。比如以
linux 中sed的用法詳解
sed 是操作,過濾和轉換文字內容的強大工具,sed可以從檔案和管道中讀取輸入。 sed 命令語法 sed [ option ] {sed -commands} {input -file} -e :執行多個sed命令 -n :只顯示操作行 -i
c++中static的用法詳解
C 語言的 static 關鍵字有三種(具體來說是兩種)用途: 1. 靜態區域性變數:用於函式體內部修飾變數,這種變數的生存期長於該函式。 int foo(){ static int i = 1; // note:1 //int i = 1; // not
python中MethodType的用法詳解
MethodType可以把外部函式(方法)繫結到類或類的例項中 而python2跟python3中MethodType的用法不盡相同,下面是它們的區別: python2: 公共部分: class Student(object): pass def set_
c++中c_str()的用法詳解
//標準庫的string類提供了三個成員函式來從一個string得到c型別的字元陣列 //主要介紹c_str //c_str():生成一個const char*指標,指向以空字元終止的陣列。 //這個陣列應該是string類內部的陣列 #include <iostre
a標籤中href=""的用法詳解
眾所周知,a標籤的最重要功能是實現超連結和錨點。而且,大多數人認為a標籤最重要的作用是實現超連結,今天我剛好碰到a標籤的一種寫法<a href="JavaScript:;">