日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ map是有序还是无序的_c++ unorder_map的用法

發布時間:2023/12/15 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ map是有序还是无序的_c++ unorder_map的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、unorder_map與map不同:map的KEY值是有序的,而unorder_map則是無序的;

2、unorder_map自定義的KEY值時需要注意思下面兩點:

· KEY為一個類時,需要重載==符號;

· 需要自定義一個HASH類,至于為什么,自己百度找原因

下面貼上unorder_map的兩類使用方法

第一種:

#include

#include

using namespace std;

class Node

{

public:

Node();

~Node();

bool operator==(const Node &n) const;

public:

std::string m_strName;

int m_iAge;

};

Node::Node() :m_iAge(0)

{

}

Node::~Node()

{

}

bool Node::operator==(const Node & n) const

{

if (n.m_iAge==m_iAge && m_strName==n.m_strName)

{

return true;

}

return false;

}

template <>

struct hash

{

std::size_t operator()(const Node& k) const

{

using std::size_t;

using std::hash;

using std::string;

// Compute individual hash values for first,

// second and third and combine them using XOR

// and bit shifting:

return ((hash()(k.m_strName))^(hash()(k.m_iAge) << 1));

}

};

int _tmain(int argc, _TCHAR* argv[])

{

std::unordered_map info;

};

第二種:

#include

#include

using namespace std;

class Node

{

public:

Node();

~Node();

bool operator==(const Node &n) const;

public:

std::string m_strName;

int m_iAge;

};

Node::Node() :m_iAge(0)

{

}

Node::~Node()

{

}

bool Node::operator==(const Node & n) const

{

if (n.m_iAge==m_iAge && m_strName==n.m_strName)

{

return true;

}

return false;

}

struct KeyHasher

{

std::size_t operator()(const Node& k) const

{

using std::size_t;

using std::hash;

using std::string;

return ((hash()(k.m_strName)) ^ (hash()(k.m_iAge) << 1));

}

};

int _tmain(int argc, _TCHAR* argv[])

{

std::unordered_map info;

}

總結

以上是生活随笔為你收集整理的c++ map是有序还是无序的_c++ unorder_map的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。