Map<int, string> mapStudent;
1.map的構造函數
map共提供了6個構造函數,這塊涉及到內存分配器這些東西,略過不表,在下面我們將接觸到一些map的構造方法,這裏要說下的就是,我們通常用如下方法構造一個map:
Map<int, string> mapStudent;
2.數據的插入
在構造map容器後,我們就可以往裏面插入數據了。這裏講三種插入數據的方法:
第一種:用insert函數插入pair數據
舉個栗子:
#include<string>
#include<map>
#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 << endl;
}
return 0;
}
make_pair()//返回類型爲對應的pair類型
無需寫出類別,就可以生成一個pair對象
例:
make_pair(1,'@')
而不必費力的寫成
pair<int ,char>(1,'@')
第二種:用insert函數插入value_type數據
舉個栗子:
#include<string>
#include<map>
#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 << endl;
}
return 0;
}
第三種:用數組方式插入數據
舉個栗子:
#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<<endl;
}
}
以上三種用法,雖然都可以實現數據的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用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 << endl;
}
}
大家可以用如下程序,看下用數組插入在數據覆蓋上的效果
#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 << endl;
}
}
4.數據的遍歷
#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<<endl;
}
}
第三種:用數組方式,程序說明如下
#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++){
cout << mapStudent[nIndex] << endl;
}
}
5.數據的查找(包括判定這個關鍵字是否在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>::iterator iter;
iter = mapStudent.find(1);
if (iter != mapStudent.end()){
cout << "Find, the value is " << iter->second << endl;
}
else{
cout << "Do not Find" << endl;
}
}
#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;
}
}
7. 數據的刪除
#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.erase(mapStudent.begin(), mapStudent.end());
//成片刪除要注意的是,也是STL的特性,刪除區間是一個前閉後開的集合
//自個加上遍歷代碼,打印輸出吧
}
9.排序
#include<map>
#include<string>
#include<iostream>
using namespace std;
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; //學生信息
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;
return 0;
}
第二種:仿函數的應用,這個時候結構體中沒有直接的小於號重載,程序說明
#include <map>
#include <string>
using namespace std;
typedef struct tagStudentInfo
{
int nID;
string strName;
}StudentInfo, *PStudentInfo; //學生信息
class 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));
}
0.另外