C++關聯容器之map
/*
* @Description:
* @version:
* @Author: sunshine
* @Github: https://subshine.github.io/
* @Email: [email protected]
* @Date: 2019-11-21 20:38:16
* @LastEditTime: 2020-03-13 14:50:43
*/
#include<map>
#include<string>
#include<iostream>
using namespace std;
typedef struct tagStudentinfo
{
int niD;
string strName;
bool operator <(tagStudentinfo const& _A) const
{
if(niD<_A.niD)
return true;
if(niD==_A.niD)
return strName.compare(_A.strName) < 0;
return false;
}
}Studentinfo,*PStudentinfo;
//仿函數
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<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(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"));
//插入數據的第三種方法:
// mapStudent[1] = "student_one";
// mapStudent[2] = "student_two";
// mapStudent[3] = "student_three";
//如何驗證插入成功:使用pair
// 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;
// }
//驗證數組形式插入數據的結果
// mapStudent[1] = "student_one";
// mapStudent[1] = "student_two";
// mapStudent[3] = "student_three";
// map<int, string>::iterator it;
// for (it = mapStudent.begin(); it != mapStudent.end(); it++)
// cout << it->first << " " << it->second << endl;
//map的大小,可以用size()函數
//數據的遍歷
//1、前向迭代器
//2、反向迭代器
// mapStudent[1] = "student_one";
// mapStudent[2] = "student_two";
// mapStudent[3] = "student_three";
// map<int, string>::reverse_iterator it;
// for (it = mapStudent.rbegin(); it != mapStudent.rend(); it++)
// cout << it->first << " " << it->second << endl;
//通過數組的方式遍歷
// 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;
//查找並獲取map中的元素(三種數據查找方法)
// 1.count()函數判定關鍵字是否出現,無法定位數據出現位置
// 2.用find()函數定位數據出現位置,返回迭代器
// 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 it;
// it = mapStudent.find(1);
// //map對象的方法獲取的iterator數據類型是一個std::pair對象,包括iterator->first和iterator->second分別代表關鍵字和存儲的數據
// if(it!=mapStudent.end())
// cout << "Find,the value is" << it->second << endl;
// else
// {
// cout << "Do not find" << endl;
// }
// 3.lower_bound()函數,返回查找關鍵字的下界(是一個迭代器)
// upper_bound()函數,返回查找關鍵字的上界(是一個迭代器)
// Equal_range()函數返回一個pair,pair第一個變量Lower_bound返回的迭代器,第二個變量Upper_bound返回的迭代器,如果兩個迭代器相等的話,說明map總不出現這個關鍵字。
// mapStudent[1] = "student_one";
// mapStudent[3] = "student_three";
// mapStudent[5] = "student_five";
// map<int, string>::iterator it;
// it = mapStudent.lower_bound(1); // student_one
// cout << it->second << endl;
// it = mapStudent.lower_bound(2); // student_three
// cout << it->second << endl;
// it = mapStudent.lower_bound(3); // student_three
// cout << it->second << endl;
// it = mapStudent.upper_bound(2);//student_three
// cout << it->second << endl;
// it = mapStudent.upper_bound(3);
// cout << it->second << endl; // student_five
// pair<map<int, string>::iterator, map<int, string>::iterator> mappair;
// if(mappair.first==mappair.second)
// cout << "Do not find" << endl;
// else
// {
// cout << "Find" << endl;
// }
// mappair = mapStudent.equal_range(4);
// if(mappair.first==mappair.second)
// cout << "Do not Find" << endl;
// else
// {
// cout << "Find" << endl;
// }
// map中刪除元素
// erase()函數刪除某一個條目
// iterator erase(iterator it);//通過一個條目對象刪除
// iterator erase(iterator first,iterator last)//刪除一個範圍
// size_type erase(const Key&key);//通過關鍵字刪除
//clear()相當於enumMap.erase(enumMap.begin(),enumMap.end())
// 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 it;
// //it = mapStudent.find(1);
// //mapStudent.erase(it);
// int n = mapStudent.erase(1);
// cout << "n: " << n << endl; //刪除返回1,否則返回0;
// mapStudent.erase(mapStudent.begin(), mapStudent.end());//全部刪除
// for (it = mapStudent.begin(); it != mapStudent.end(); it++)
// cout << it->first << " " << it->second << endl;
//10.map中swap用法
//map中的swap不是一個容器中的元素交換,而是兩個容器所有元素的交換
//11.(排序)map中的sort問題
//小於號的重載
// int nSize;
// map<Studentinfo, int> mapStudent;
// map<Studentinfo, int>::iterator it;
// 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 (it = mapStudent.begin(); it != mapStudent.end();it++)
// cout << it->first.niD << " " << it->first.strName << " " << it->second
// << endl;
//仿函數的應用,結構體中沒有直接的小於號重載
map<Studentinfo, int, sort> mapStudent;
map<Studentinfo, int>::iterator it;
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 (it = mapStudent.begin(); it != mapStudent.end();it++)
cout << it->first.niD << " " << it->first.strName << " " << it->second
<< endl;
return 0;
}