C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- 容器库
- std::array
- std::vector
- std::map
- std::unordered_map
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::operator[]
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::get_allocator
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::begin, std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::cbegin
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::empty
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::end, std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::cend
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::unordered_map
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::~unordered_map
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::operator=
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::size
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::max_size
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::clear
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::insert
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::insert_or_assign
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::emplace
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::emplace_hint
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::try_emplace
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::erase
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::swap
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::extract
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::merge
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::at
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::count
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::find
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::contains
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::equal_range
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::begin(size_type), std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::cbegin(size_type)
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::end(size_type), std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::cend(size_type)
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::bucket_count
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::max_bucket_count
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::bucket_size
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::bucket
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::load_factor
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::max_load_factor
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::rehash
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::reserve
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::hash_function
- std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::key_eq
- operator==,!=(std::unordered_map)
- std::swap(std::unordered_map)
- std::erase_if (std::unordered_map)
- std::unordered_map 的推导指引
- std::priority_queue
- std::span
- std::forward_list
- std::deque
- std::list
- std::set
- std::multiset
- std::multimap
- std::unordered_set
- std::unordered_multiset
- std::unordered_multimap
- std::stack
- std::queue
- std::vector<bool>
- 结点把柄 (C++17)
- 注释
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >容器库 >std::unordered_map > std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::begin, std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::cbegin
std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::begin, std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::cbegin
iterator begin() noexcept; |
(C++11 起) | |
const_iterator begin() const noexcept; |
(C++11 起) | |
const_iterator cbegin() const noexcept; |
(C++11 起) | |
返回指向 unordered_map
首元素的迭代器。
若 unordered_map
为空,则返回的迭代器将等于 end() 。
参数
(无)
返回值
指向首元素的迭代器。
复杂度
常数。
示例
运行此代码
#include <cmath> #include <iostream> #include <unordered_map> struct Node { double x, y; }; int main() { Node nodes[3] = { {1, 0}, {2, 0}, {3, 0} }; // mag 是将 Node 的地址映射到其在平面中的模的映射 std::unordered_map<Node *, double> mag = { { nodes, 1 }, { nodes + 1, 2 }, { nodes + 2, 3 } }; // 将每个 y 坐标从 0 更改到模 for(auto iter = mag.begin(); iter != mag.end(); ++iter){ auto cur = iter->first; // 指向 Node 的指针 cur->y = mag[cur]; // 可以也使用 cur->y = iter->second; } // 更新并打印每个结点的模 for(auto iter = mag.begin(); iter != mag.end(); ++iter){ auto cur = iter->first; mag[cur] = std::hypot(cur->x, cur->y); std::cout << "The magnitude of (" << cur->x << ", " << cur->y << ") is "; std::cout << iter->second << '\n'; } // 以基于范围的 for 循环重复上述者 for(auto i : mag) { auto cur = i.first; cur->y = i.second; mag[cur] = std::hypot(cur->x, cur->y); std::cout << "The magnitude of (" << cur->x << ", " << cur->y << ") is "; std::cout << mag[cur] << '\n'; // 注意与 std::cout << iter->second << '\n'; 相反,上述的 // std::cout << i.second << '\n'; 不会打印更新的模 } }
可能的输出:
The magnitude of (1, 1) is 1.41421 The magnitude of (2, 2) is 2.82843 The magnitude of (3, 3) is 4.24264 The magnitude of (1, 1.41421) is 1.73205 The magnitude of (2, 2.82843) is 3.4641 The magnitude of (3, 4.24264) is 5.19615
参阅
返回指向容器尾端的迭代器 (公开成员函数) |