flat_map[meta header]
std[meta namespace]
class template[meta id-type]
cpp23[meta cpp]
namespace std {
template <class Key ,
class T ,
class Compare = less<Key>,
class KeyContainer = vector<Key>,
class MappedContainer = vector<T>>
class flat_map ;
}
less[link ../functional/less.md]
vector[link /reference/vector/vector.md]
名前
説明
対応バージョン
begin
先頭を指すイテレータを取得する
C++23
cbegin
先頭を指す読み取り専用イテレータを取得する
C++23
end
末尾の次を指すイテレータを取得する
C++23
cend
末尾の次を指す読み取り専用イテレータを取得する
C++23
rbegin
末尾を指す逆イテレータを取得する
C++23
crbegin
末尾を指す読み取り専用逆イテレータを取得する
C++23
rend
先頭の前を指す逆イテレータを取得する
C++23
crend
先頭の前を指す読み取り専用逆イテレータを取得する
C++23
名前
説明
対応バージョン
empty
コンテナが空であるかどうかを調べる
C++23
size
要素数を取得する
C++23
max_size
格納可能な最大の要素数を取得する
C++23
名前
説明
対応バージョン
key_type
キーの型。テンプレートパラメータ Key
C++23
mapped_type
値の型。テンプレートパラメータ T
C++23
value_type
要素の型。std::pair
<key_type, mapped_type>
C++23
key_compare
キー値の大小関係を判定する二項述語の型。テンプレートパラメータ Compare
C++23
value_compare
要素値のキー部分で大小関係を判定する二項述語の型。入れ子クラス
C++23
reference
要素への参照型。std::pair
<const key_type&, mapped_type&>
C++23
const_reference
要素へのconst
参照型。std::pair
<const key_type&, const mapped_type&>
C++23
size_type
要素数を表す符号なし整数型 size_t
C++23
difference_type
同一のコンテナを指す iterator
の差を表す符号付き整数型 ptrdiff_t
C++23
iterator
ランダムアクセスイテレータ
C++23
const_iterator
読み取り専用ランダムアクセスイテレータ
C++23
reverse_iterator
逆順ランダムアクセスイテレータ。std::reverse_iterator
<iterator>
C++23
const_reverse_iterator
読み取り専用逆順ランダムアクセスイテレータ。std::reverse_iterator
<const_iterator>
C++23
key_container_type
キーを格納するコンテナ型 KeyContainer
C++23
mapped_container_type
値を格納するコンテナ型 MappedContainer
C++23
containers
キーのコンテナと値のコンテナを保持する型
C++23
key_equiv
要素をとってキーの等価比較を行う説明専用の関数オブジェクト
C++23
名前
説明
対応バージョン
erase_if
指定した条件に合致する要素とその分の領域を、コンテナから削除する
C++23
名前
説明
対応バージョン
operator==
左辺と右辺が等しいかの判定を行う
C++23
bool operator!=(const flat_map& x, const flat_map& y);
左辺と右辺が等しくないかの判定を行う (==
により使用可能)
C++23
operator<=>
三方比較を行う
C++23
bool operator<(const flat_map& x, const flat_map& y);
左辺が右辺より小さいかの判定を行う (<=>
により使用可能)
C++23
bool operator<=(const flat_map& x, const flat_map& y);
左辺が右辺より小さいか等しいかの判定を行う (<=>
により使用可能)
C++23
bool operator>(const flat_map& x, const flat_map& y);
左辺が右辺より大きいかの判定を行う (<=>
により使用可能)
C++23
bool operator>=(const flat_map& x, const flat_map& y);
左辺が右辺より大きいか等しいかの判定を行う (<=>
により使用可能)
C++23
名前
説明
対応バージョン
swap
2つのflat_map
オブジェクトを入れ替える
C++23
#include < iostream>
#include < string>
#include < flat_map>
int main ()
{
// stringをキー、intを値として扱う連想配列
std::flat_map<std::string, int > fm = {
{" Carol" , 4 },
{" Alice" , 3 },
{" Bob" , 1 }
};
// 検索 : キー(string)を指定し、値(int)を得る
int r = fm.at (" Alice" );
std::cout << r << std::endl;
// 全体を出力する
for (const auto & [key, value] : fm) {
std::cout << key << " : " << value << std::endl;
}
}
fm.at[link flat_map/at.md]
3
Alice : 3
Bob : 1
Carol : 4
ユーザー定義型をキーとして使用する (operator<=>
を定義)
#include < iostream>
#include < string>
#include < flat_map>
// 要素がひとつの場合
struct MyInt {
int value;
friend auto operator <=>(const MyInt& a, const MyInt& b) noexcept {
return a.value <=> b.value ;
}
};
// 要素が複数の場合
struct Person {
int id;
int age;
std::string name;
friend auto operator <=>(const Person& a, const Person& b) noexcept {
if (auto comp = a.id <=> b.id ; comp != 0 ) {
return comp;
}
if (auto comp = a.age <=> b.age ; comp != 0 ) {
return comp;
}
return a.name <=> b.name ;
}
};
int main ()
{
std::flat_map<MyInt, int > fm1 {
{MyInt{1 }, 3 },
{MyInt{2 }, 1 },
{MyInt{3 }, 4 },
};
std::cout << fm1[MyInt{2 }] << std::endl;
std::flat_map<Person, int > fm2 {
{Person{1 , 18 , " Alice" }, 3 },
{Person{2 , 30 , " Bob" }, 1 },
{Person{3 , 30 , " Carol" }, 4 },
};
std::cout << fm2[Person{2 , 30 , " Bob" }] << std::endl;
}
ユーザー定義型をキーとして使用する (大小比較の関数オブジェクトを定義)
#include < iostream>
#include < string>
#include < tuple>
#include < flat_map>
// 要素がひとつの場合
struct MyInt {
int value;
};
struct MyIntLess {
bool operator ()(const MyInt& a, const MyInt& b) const noexcept {
return a.value < b.value ;
}
};
// 要素が複数の場合
struct Person {
int id;
int age;
std::string name;
};
struct PersonLess {
bool operator ()(const Person& a, const Person& b) const noexcept {
// キーとして比較したい要素を列挙する
return std::tie (a.id , a.age , a.name ) < std::tie (b.id , b.age , b.name );
}
};
int main ()
{
std::flat_map<MyInt, int , MyIntLess> fm1 {
{MyInt{1 }, 3 },
{MyInt{2 }, 1 },
{MyInt{3 }, 4 },
};
std::cout << fm1[MyInt{2 }] << std::endl;
std::flat_map<Person, int , PersonLess> fm2 {
{Person{1 , 18 , " Alice" }, 3 },
{Person{2 , 30 , " Bob" }, 1 },
{Person{3 , 30 , " Carol" }, 4 },
};
std::cout << fm2[Person{2 , 30 , " Bob" }] << std::endl;
}