Skip to content

Latest commit

 

History

History
81 lines (61 loc) · 1.68 KB

File metadata and controls

81 lines (61 loc) · 1.68 KB

at

  • map[meta header]
  • std[meta namespace]
  • map[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
T& at(const key_type& x);
const T& at(const key_type & x) const;

概要

指定したキーを持つ要素を取得する。
要素を取り出す際にキーの存在チェックをする。

戻り値

キーxに対応する値を返す。対応する要素が存在しないときは、out_of_range例外を投げる。

計算量

要素数に対して対数時間

#include <iostream>
#include <map>
#include <stdexcept>

template<class Container, class T>
void at_wrap(Container& c, T v)
{
  try {
    std::cout << c.at(v) << std::endl;
  }
  catch(std::out_of_range&) {
    std::cout << "exception std::out_of_range" << std::endl;
  }
}

int main()
{
  std::map<int,char> m;
  m.insert(std::make_pair(1, 'a'));

  at_wrap(m, 1);
  at_wrap(m, 2);

  return 0;
}
  • c.at[color ff0000]
  • m.insert[link insert.md]
  • std::out_of_range[link /reference/stdexcept.md]

出力

a
exception std::out_of_range

処理系

関連項目

名前 説明
operator= 代入演算子
insert 要素を挿入する

参照