1、map 红黑书 ,如果key 是结构体的话,需要重载 < 和 == 操作符 ,时间复杂度为o(n) ,会排序
https://blog.csdn.net/fcku_88/article/details/88135150
https://blog.csdn.net/henu_jizhideqingwa/article/details/80994913
2、unordered_map 基于hash ,如果 key 作为结构体,需要重载 == 操作符,并且重写hash函数,时间复杂度为o(1) ,占用空间多 ,顺序会被打乱
3、OrderedMap ,基于 std::deque ,严格按照读入顺序生成
/// For documentation, see https://tessil.github.io/ordered-map/
struct ST
{
string strClass;
string strNO;
ST(const string& cs, const string&no)
:strClass(cs), strNO(no)
{
}
bool operator < (const ST& st) const
{
if (strClass < st.strClass)
{
return true;
}
else if (strClass == st.strClass)
{
return strNO < st.strNO;
}
else
{
return false;
}
}
};
ostream & operator<<(ostream &out, const ST &st)
{
out << st.strClass << "->" << st.strNO;
return out;
}
int main()
{
std::map<ST, int> s1 ;
s1[ST("A1", "002")] = 100;
s1[ST("A1", "001")] = 50;
s1[ST("A2", "001")] = 100;
for (const auto& st : s1)
{
std::cout << st.first << " :" << st.second << "\r\n";
}
}
运行结果如下:
unordermap 见方法二、三
https://blog.csdn.net/y109y/article/details/82669620