定义于头文件
template< class Key, | (1) | (C++11 起) |
namespace pmr { template | (2) | (C++17 起) |
std::unordered_map::begin,
std::unordered_map::cbegin
iterator begin() noexcept; | (C++11 起) | |
const_iterator begin() const noexcept; | (C++11 起) | |
const_iterator cbegin() const noexcept; | (C++11 起) |
返回指向容器首元素的迭代器。
若容器为空,则返回的迭代器将等于 end() 。
(无)
指向首元素的迭代器。
常数。
std::unordered_map::end,
std::unordered_map::cend
iterator end() noexcept; | (C++11 起) | |
const_iterator end() const noexcept; | (C++11 起) | |
const_iterator cend() const noexcept; | (C++11 起) |
返回指向容器末元素后一元素的迭代器。
此元素表现为占位符;试图访问它导致未定义行为。
(无)
指向后随最后元素的迭代器。
常数。
#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::unordered_map unordered_map1;while (unordered_map1.size() < 5){unordered_map1.insert(generate());}std::cout << "unordered_map1: ";std::copy(unordered_map1.begin(), unordered_map1.end(), std::ostream_iterator>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//返回指向容器首元素的迭代器。若容器为空,则返回的迭代器将等于 end() 。//返回指向容器末元素后一元素的迭代器。此元素表现为占位符;试图访问它导致未定义行为。std::cout << "unordered_map1 const_iterator: ";for (std::unordered_map::const_iterator cit =unordered_map1.cbegin(); cit != unordered_map1.cend(); cit++){std::cout << *cit << " ";}std::cout << std::endl;std::cout << std::endl;for (std::unordered_map::iterator it =unordered_map1.begin(); it != unordered_map1.end(); it++){it->second = std::to_string(std::rand() % 10 + 110);}std::cout << "unordered_map1: ";std::copy(unordered_map1.begin(), unordered_map1.end(), std::ostream_iterator>(std::cout, " "));std::cout << std::endl;return 0;
}
| | | | |
下一篇:【Git】git常用命令集合