std::map,multimap
- map,multimap
- インクルード
- 初期化(コンストラクタ)
- 要素アクセス([],at,begin,end,rbegin,rend)
- サイズ確認(size,max_size,empty)
- 編集(insert,erase,clear,swap)
- マップの走査(find,count,lower_bound,upper_bound,equal_range)
- 比較関数(key_comp,value_comp)
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構造体が使われている.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| | 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) {}
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)†
- begin(),end() : コンテナの最初のイテレータ,最後の次のイテレータを返す.イテレータを用いた要素アクセスに使用.
iterator begin ();
const_iterator begin () const;
- rbegin(),rend() : コンテナの最初の逆イテレータ,最後の次の逆イテレータを返す.イテレータを用いた要素逆順アクセスに使用
reverse_iterator rend();
const_reverse_iterator rend() const;
使用例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| | #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)†
編集(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)†
|