»
(15)C++14 map
(18)C++14 智能指针及引用计数*
(19)C++14 多线程pthread*
(20)C++14 同步锁mutex*
(24)C++中的计时与等待*
(25)C++中的高精度计算*
(27)C++14 std::string*
(31)C++高级技能*
C++中的map,默认是TreeMap,所以,对于放入C++中的map需要实现operator <操作符重载。
map的例子代码:
#include <iostream>
#include <map>
typedef enum{
UNCONFIRMED=0,
MALE=1,
FEMALE=2
}Gender;
using namespace std;
class RowItem{
private:
int id;
string name;
Gender gender;
public:
RowItem(int id, string name, Gender gender){
this->id=id;
this->name=std::move(name);
this->gender=gender;
}
bool operator < (const RowItem& right) const{
return this->name<right.name;
}
void print() const{
cout<<this->id<<" "<<this->name<<" "<<this->gender<<endl;
}
};
int main(){
RowItem rowItem1(1,"NotSortName1",MALE);
RowItem rowItem2(2,"001SortName2",FEMALE);
RowItem rowItem3(3,"Name3",MALE);
map<int,RowItem>idToRowItemMap;
idToRowItemMap.insert(make_pair(1,rowItem1));
idToRowItemMap.insert(make_pair(2,rowItem2));
idToRowItemMap.insert(make_pair(3,rowItem3));
map<RowItem,int>rowItemToIdMap;
rowItemToIdMap.insert(make_pair(rowItem1,1));
rowItemToIdMap.insert(make_pair(rowItem2,1));
rowItemToIdMap.insert(make_pair(rowItem3,1));
cout<<"Id To RowItem Map:"<<endl;
for(auto& pair:idToRowItemMap){
pair.second.print();
}
cout<<"RowItem(sortByName) To Id Map:"<<endl;
for(auto& pair:rowItemToIdMap){
pair.first.print();
}
return 0;
}
打印结果:
Id To RowItem Map:
1 NotSortName1 1
2 001SortName2 2
3 Name3 1
RowItem(sortByName) To Id Map:
2 001SortName2 2
3 Name3 1
1 NotSortName1 1