Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

unordered_map::at

公众成员函数  <unordered_map>
      mapped_type& at (const key_type& k);
const mapped_type& at (const key_type& k) const;

访问元素

返回对键为k的元素映射值的引用。

如果k不匹配容器中任何元素的键,则函数抛出out_of_range异常。

☲  参数


K
访问其映射值的元素的键值。
成员类型key_type是存储在容器中的元素的键类型,在unordered_map中定义为其第一个模板参数(Key)的别名。

☉  返回值



对键值等于k的元素映射值的引用。
成员类型unordered_mapped_type是容器中映射值的类型,在unordered_map中定义为它的第二个模板参数(T)的别名。

☣  示例



// unordered_map::at
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_map<std::string,int> mymap = {
                { "Mars", 3000},
                { "Saturn", 60000},
                { "Jupiter", 70000 } };

  mymap.at("Mars") = 3396;
  mymap.at("Saturn") += 272;
  mymap.at("Jupiter") = mymap.at("Saturn") + 9638;

  for (auto& x: mymap) {
    std::cout << x.first << ": " << x.second << std::endl;
  }

  return 0;
}

输出:
Saturn: 60272
Mars: 3396
Jupiter: 69910

✥ 复杂度



容器大小:对数。最坏情况:容器大小呈线性。

☣ 迭代器的有效性



不变

🍄  另请参阅



unordered_map::find 获取元素迭代器(公众成员函数)
unordered_map::operator[] 访问容器(公众成员函数)

联系我们 免责声明 关于CandCplus 网站地图