定义于头文件
template< class Key, |
(1) | (C++11 起) |
namespace pmr { template |
(2) | (C++17 起) |
unordered_map 是关联容器,含有带唯一键的键-值 pair 。搜索、插入和元素移除拥有平均常数时间复杂度。
元素在内部不以任何特定顺序排序,而是组织进桶中。元素放进哪个桶完全依赖于其键的哈希。这允许对单独元素的快速访问,因为一旦计算哈希,则它准确指代元素所放进的桶。
std::unordered_map::empty
bool empty() const noexcept; |
(C++11 起) (C++20 前) |
|
[[nodiscard]] bool empty() const noexcept; |
(C++20 起) |
检查容器是否无元素,即是否 begin() == end() 。
(无)
若容器为空则为 true ,否则为 false
常数。
std::unordered_map::size
size_type size() const noexcept; |
(C++11 起) |
(无)
容器中的元素数量。
常数。
std::unordered_map::max_size
size_type max_size() const noexcept; |
(C++11 起) |
返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。
(无)
元素数量的最大值。
常数。
此值通常反映容器大小上的理论极限,至多为 std::numeric_limitsmax_size()
的值。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
struct myCompare
{
bool operator()(const int &a, const int &b)
{
return a < b;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
std::ostream &operator<<(std::ostream &os, const std::pair &pCell)
{
os << pCell.first << "-" << pCell.second;
return os;
}
struct CHash
{
size_t operator()(const Cell& cell) const
{
size_t thash = std::hash()(cell.x) | std::hash()(cell.y);
// std::cout << "CHash: " << thash << std::endl;
return thash;
}
};
struct CEqual
{
bool operator()(const Cell &a, const Cell &b) const
{
return a.x == b.x && a.y == b.y;
}
};
int main()
{
std::cout << std::boolalpha;
std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return std::pair(cell, std::to_string(n));
};
std::vector> vector1(6);
std::generate(vector1.begin(), vector1.end(), generate);
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator>(std::cout, " "));
std::cout << std::endl;
//检查容器是否无元素,即是否 begin() == end() 。
std::unordered_map unordered_map1;
std::cout << "unordered_map1 is empty " << unordered_map1.empty() << std::endl;
unordered_map1 = {generate(), generate(), generate(), generate(), generate(), generate()};;
std::cout << "unordered_map1: ";
std::copy(unordered_map1.begin(), unordered_map1.end(), std::ostream_iterator>(std::cout, " "));
std::cout << std::endl;
std::cout << "unordered_map1 is empty " << unordered_map1.empty() << std::endl;
std::cout << std::endl;
std::unordered_map unordered_map2;
for (size_t index = 0; index < 6; index++)
{
unordered_map2.insert(generate());
//返回容器中的元素数,即 std::distance(begin(), end()) 。
std::cout << "unordered_map2 size: " << unordered_map2.size() << std::endl;
std::copy(unordered_map2.begin(), unordered_map2.end(), std::ostream_iterator>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
//返回根据系统或库实现限制的容器可保有的元素最大数量,
//即对于最大容器的 std::distance(begin(), end()) 。
std::unordered_map unordered_map_c;
std::cout << "unordered_map max_size: " << unordered_map_c.max_size() << std::endl;
std::unordered_map unordered_map_i;
std::cout << "unordered_map max_size: " << unordered_map_i.max_size() << std::endl;
std::unordered_map unordered_map_ui8;
std::cout << "unordered_map max_size: " << unordered_map_ui8.max_size() << std::endl;
std::unordered_map unordered_map_ui16;
std::cout << "unordered_map max_size: " << unordered_map_ui16.max_size() << std::endl;
std::unordered_map unordered_map_ui32;
std::cout << "unordered_map max_size: " << unordered_map_ui32.max_size() << std::endl;
std::unordered_map unordered_map_ui64;
std::cout << "unordered_map max_size: " << unordered_map_ui64.max_size() << std::endl;
std::unordered_map unordered_map_s;
std::cout << "unordered_map max_size: " << unordered_map_s.max_size() << std::endl;
std::unordered_map unordered_map_d;
std::cout << "unordered_map max_size: " << unordered_map_d.max_size() << std::endl;
std::unordered_map unordered_map_f;
std::cout << "unordered_map max_size: " << unordered_map_f.max_size() << std::endl;
std::unordered_map unordered_map_l;
std::cout << "unordered_map max_size: " << unordered_map_l.max_size() << std::endl;
std::unordered_map unordered_map_ll;
std::cout << "unordered_map max_size: " << unordered_map_ll.max_size() << std::endl;
std::unordered_map unordered_map_str;
std::cout << "unordered_map max_size: " << unordered_map_str.max_size() << std::endl;
std::unordered_map unordered_map_Cell;
std::cout << "unordered_map max_size: " << unordered_map_Cell.max_size() << std::endl;
return 0;
} | | | | | |