定义于头文件
template< class Key, | (1) | (C++11 起) |
namespace pmr { template | (2) | (C++17 起) |
std::unordered_map::at
T& at( const Key& key ); | (1) | (C++11 起) |
const T& at( const Key& key ) const; | (2) | (C++11 起) |
返回到拥有等于 key
的关键的元素被映射值的引用。若无这种元素,则抛出 std::out_of_range 类型异常。
key | - | 要找到的元素的关键 |
到请求元素的被映射值的引用
若容器无拥有指定 key
的元素则为 std::out_of_range
平均情况:常数,最坏情况:与大小成线性。
std::unordered_map::operator[]
T& operator[]( const Key& key ); | (1) | (C++11 起) |
T& operator[]( Key&& key ); | (2) | (C++11 起) |
返回到映射到等于 key
的关键的值的引用,若这种关键不存在则进行插入。
1) 若关键不存在,则插入从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 原位构造的 value_type
对象。此函数等价于 return this->try_emplace(key).first->second; 。 (C++17 起)
使用默认分配器时,这导致从 key
复制构造关键,并值初始化被映射值。
- value_type 必须从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 可就位构造 (EmplaceConstructible) 。使用默认分配器时,这表明 key_type 必须可复制构造 (CopyConstructible) 而 mapped_type 必须可默认构造 (DefaultConstructible) 。 |
2) 若关键不存在,则插入从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 原位构造的 value_type
对象。此函数等价于 return this->try_emplace(std::move(key)).first->second; 。 (C++17 起)
使用默认分配器时,这导致从 key
移动构造关键,并值初始化被映射值。
- value_type 必须从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 可就位构造 (EmplaceConstructible) 。使用默认分配器时,这表明 key_type 必须可移动构造 (MoveConstructible) mapped_type 必须可默认构造 (DefaultConstructible) 。 |
若插入发生且导致容器的重哈希,则所有迭代器被非法化。否则迭代器不受影响。重哈希仅若新元素数量大于 max_load_factor()*bucket_count() 才发生。
key | - | 要寻找的元素关键 |
若不存在拥有关键 key
的元素,则为到新元素被映射值的引用。否则为到既存的关键等价于 key
的元素的被映射值的引用。
若任何操作抛出异常,则插入无效果。
平均情况:常数,最坏情况:与大小成线性。
出版的 C++11 和 C++14 标准中,指定此函数要求 mapped_type
为可默认插入 (DefaultInsertable) 且 key_type
为可复制插入 (CopyInsertable) 或可移动插入 (MoveInsertable) 到 *this 。此规定有缺陷并为 LWG 问题 2469 所修复,而上面的描述合并了该问题的解决方案。
然而,已知一个实现( libc++ )通过二个分离的分配器 construct()
调用构造 key_type
和 mapped_type
对象,可认为如发布时的标准所要求,而非原位构造 value_type
对象。
operator[]
非 const ,因为若不关键不存在则它插入关键。若此行为非所欲或容器为 const
,则可用 at()
。
| (C++17 起) |
#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() < 6){//若容器尚未含有带等价关键的元素,则插入元素到容器中。1-2) 插入 value 。unordered_map1.insert(generate());}std::cout << "unordered_map1 const at: " << std::endl;for (std::unordered_map::const_iterator cit = unordered_map1.cbegin();cit != unordered_map1.end(); cit++){std::cout << "key: " << cit->first << " ";//返回到拥有等于 key 的关键的元素被映射值的引用。若无这种元素,则抛出 std::out_of_range 类型异常。std::cout << "value: " << unordered_map1.at(cit->first);std::cout << std::endl;}std::cout << std::endl;std::cout << "unordered_map1 at: " << std::endl;for (std::unordered_map::const_iterator cit = unordered_map1.cbegin();cit != unordered_map1.end(); cit++){std::cout << "key: " << cit->first << " ";//返回到拥有等于 key 的关键的元素被映射值的引用。若无这种元素,则抛出 std::out_of_range 类型异常。string value = unordered_map1.at(cit->first);unordered_map1.at(cit->first) = value + value;std::cout << "value: " << unordered_map1.at(cit->first);std::cout << std::endl;}std::cout << std::endl;std::cout << std::endl;std::unordered_map unordered_map2;std::cout << "unordered_map2 const Key& key : " << std::endl;for (std::unordered_map::const_iterator cit = unordered_map1.cbegin();cit != unordered_map1.end(); cit++){std::cout << "key: " << cit->first << " ";//返回到映射到等于 key 的关键的值的引用,若这种关键不存在则进行插入。std::cout << "value: " << unordered_map2[cit->first] << " ";unordered_map2[cit->first] = cit->second;std::cout << "value: " << unordered_map2[cit->first];std::cout << std::endl;}std::cout << std::endl;std::unordered_map unordered_map3;//移动语义std::cout << "unordered_map3 Key&& key : " << std::endl;for (std::unordered_map::const_iterator cit = unordered_map1.cbegin();cit != unordered_map1.end(); cit++){std::cout << "key: " << cit->first << " ";//返回到映射到等于 key 的关键的值的引用,若这种关键不存在则进行插入。std::cout << "value: " << unordered_map3[cit->first] << " ";unordered_map3[cit->first] = cit->second;std::cout << "value: " << unordered_map3[std::move(cit->first)];std::cout << std::endl;}std::cout << std::endl;return 0;
}
| | | | | | | | |