std::map,multimap



map,multimap

キーと値を関連付けて格納する連想コンテナ. multimapはmapのキーの重複を許したバージョン.

クラスのテンプレート仕様は,

template < class Key, class T, class Compare = less<Key>,
           class Allocator = allocator<pair<const Key,T> > > class map;
template < class Key, class T, class Compare = less<Key>,
           class Allocator = allocator<pair<const Key,T> > > class multimap;

となっており, 定義時にキーのデータ型,値のデータ型,比較関数,アロケータのタイプを指定することができる. 標準のアロケータにはpair構造体が使われている.

template<class Key, class Value> struct pair
{
	typedef Key first_type;		// キーの型
	typedef Value second_type;	// 値の型

	pair() : first(Key()), second(Value()) {}	// デフォルトコンストラクタ
	pair(const Key& _Val1, const Value& _Val2) : first(_Val1), second(_Val2) {}	// 値指定のコンストラクタ

	template<class _Other1, class _Other2>
		pair(const pair<_Other1, _Other2>& _Right) : first(_Right.first), second(_Right.second) {} // 互換性があるpairを用いたコンストラクタ

	void swap(pair<Key, Value>& _Right)		// スワップ
	{
		if(this != &_Right){
			_STD _Swap_adl(first, _Right.first);
			_STD _Swap_adl(second, _Right.second);
		}
	}

	Key first;		// キー
	Value second;	// 値
};

template<class Key, class Value> 
inline pair<Key, Value> make_pair(Key _Val1, Value _Val2)
{
	return (pair<Key, Value>(_Val1, _Val2));
}

上記の例はVisual Studio 2008の場合. pairのコンストラクタを用いる以外に, make_pair関数を用いてもpairオブジェクトを作成できる.

インクルード

#include <map>

初期化(コンストラクタ)

explicit map(const Compare& comp = Compare(),
               const Allocator& = Allocator());
template <class InputIterator>
 map(InputIterator first, InputIterator last,
       const Compare& comp = Compare(), const Allocator& = Allocator());
map(const map<Key,T,Compare,Allocator>& x);

要素アクセス([],at,begin,end,rbegin,rend)

  • [] : キーを指定することで値を得る.対応する要素がなければ生成して返す.mapの中身を変更してしまう可能性があるのでconstなmapには使えない.新たな要素を追加したくない/constなmapで使いたいという場合はatを使おう. multimapでは使えない.
    T& operator[](const key_type& k);
  • at : キーを指定することで値を得る.オペレータ[]を使ったアクセスと違ってこちらは対応する要素がなければout_of_range例外となる.
    T& at(const key_type& x);
    const T& at(const key_type & x) const;
  • begin(),end() : コンテナの最初のイテレータ,最後の次のイテレータを返す.イテレータを用いた要素アクセスに使用.
    iterator begin ();
    const_iterator begin () const;
  • rbegin(),rend() : コンテナの最初の逆イテレータ,最後の次の逆イテレータを返す.イテレータを用いた要素逆順アクセスに使用
    reverse_iterator rend();
    const_reverse_iterator rend() const;
    使用例
    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    
    int main(void)
    {
    	map<int, string> x;
    
    	// 背番号と名前
    	x.insert(pair<int, string>( 9, "M. Murton"));
    	x.insert(pair<int, string>( 5, "K. Hirano"));
    	x.insert(pair<int, string>( 1, "T. Toritani"));
    	x.insert(pair<int, string>(25, "T. Arai"));
    	x.insert(pair<int, string>(67, "C. Brazell"));
    	x.insert(pair<int, string>( 2, "K. Jojima"));
    	x.insert(pair<int, string>( 7, "S. Fujikawa"));
    	x.insert(pair<int, string>(31, "W. Lin"));
    	x.insert(pair<int, string>(14, "A. Noumi"));
    
    	// マップの内容の表示
    	map<int, string>::iterator iter;
    	for(iter = x.begin(); iter != x.end(); ++iter){
    		cout << iter->first << "  " << iter->second << endl;
    	}
    
    	return 0;
    }
    実行結果
    1  T. Toritani
    2  K. Jojima
    5  K. Hirano
    7  S. Fujikawa
    9  M. Murton
    14  A. Noumi
    25  T. Arai
    31  W. Lin
    67  C. Brazell

サイズ確認(size,max_size,empty)

  • size() : コンテナのサイズを返す関数
    size_type size() const;
  • max_size() : コンテナが確保可能な最大サイズを返す関数.予約領域ではない
    size_type max_size () const;
  • empty() : コンテナが空だったらtrueを返す関数
    bool empty () const;

編集(insert,erase,clear,swap)

  • insert(val) : 要素を挿入する.
    pair<iterator,bool> insert(const value_type& x);
    iterator insert(iterator position, const value_type& x);
    template <class InputIterator>
     void insert(InputIterator first, InputIterator last);
  • erase(iter) : 要素を削除する.
    void erase(iterator position);				// イテレータで示された位置の要素削除
    size_type erase(const key_type& k);			// キーがxである要素を削除
    void erase(iterator first, iterator last);	// イテレータで示された範囲の要素削除
  • clear() : コンテナのクリア.全要素を削除する.
    void clear();
  • swap() : 2つのコンテナのスワップ.
    void swap(map<Key,T,Compare,Allocator>& mp);

マップの走査(find,count,lower_bound,upper_bound,equal_range)

  • find(k) : キーがxである要素を検索してそのイテレータを返す.見つからなければ末尾のイテレータ(end())が返される.
    iterator find(const key_type& k);
    const_iterator find(const key_type& k) const;
  • count(k) : マップ内のキーがxである要素の数を返す(mapでは0か1,multimapでは数)
    size_type count(const key_type& k) const;
  • lower_bound(k) : キーがx以上である最初の要素を示すイテレータを返す
    iterator lower_bound(const key_type& k);
    const_iterator lower_bound(const key_type& k) const;
  • upper_bound(k) : キーがxより大きい最初の要素を示すイテレータを返す
    iterator upper_bound(const key_type& k);
    const_iterator upper_bound(const key_type& k) const;
  • equal_range(k) : キーがxである要素の範囲(イテレータのpair)を返す(mapでは1要素のみ)
    pair<iterator,iterator> equal_range(const key_type& k);
    pair<const_iterator,const_iterator> equal_range(const key_type& k) const;

比較関数(key_comp,value_comp)

  • key_comp() : キーの比較関数オブジェクトを返す
    key_compare key_comp() const;
  • value_comp() : 値の比較関数オブジェクトを返す
    value_compare value_comp() const;

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2024-03-08 (金) 18:06:08