ヘッダー ファイル <map> で定義
テンプレート< クラス Key、 |
(1) | |
名前空間 pmr { template <class Key, class T, class Compare = std::less<Key>> |
(2) | (C++17以降) |
std::map
順序付けられたキーと値のペアのコンテナーであり、その要素のキーは一意です。比較機能でキーCompare
をソートします。検索、削除、挿入の操作は対数的な複雑さがあります。マップは通常、赤黒ツリーとして実装されます。
標準ライブラリが比較の概念を使用するすべての場所で、一意性が等価関係に対してチェックされます。不正確には、2 つのオブジェクト および は、相互に比較したときに互いに小さくないa
場合、同等 (非固有) とみなされます。b
!comp(a, b) && !comp(b, a)
std::map
Container、AllocatorAwareContainer、AssociativeContainer 、およびReversibleContainerの要件を満たします。
修飾子
現場建設要素
std::map<Key,T,Compare,Allocator>::emplace
template< class... Args > |
(C++11以降) |
key を所有する要素がコンテナ内にない場合は、指定されたインargs
プレース構造。
慎重に使用すると、不必要なコピーまたは移動操作を回避しながら、新しい要素を構築emplace
できます。Forward は、 にemplace
指定されたものとまったく同じ引数を使用して コンテナ内にキーを持つ要素がすでに存在する場合でも要素を構築できます。その場合、新しく構築された要素はすぐに破棄されます。
イテレータや参照は無効になりません。
パラメータ
引数 | - | 要素のコンストラクターに転送される引数 |
戻り値
挿入された要素を指すイテレータ、または挿入が行われなかった場合は既存の要素を指すイテレータ、および挿入が行われたかどうかを示すブール値 (挿入が行われた場合は true、そうでない場合は false) を返します。
異常な
いずれかの操作で例外がスローされた場合、この関数は効果がありません。
複雑さ
コンテナサイズの対数。
ヒントを使用して要素をその場で構築する
std::map<Key,T,Compare,Allocator>::emplace_hint
テンプレート <クラス... Args> |
(C++11以降) |
hint
の直前の 位置にできるだけ近い要素を挿入します。要素をその場で構築します。つまり、コピーまたは移動操作は実行されません。
value_type
関数に指定された引数とまったく同じ std::forward<Args>(args)... を使用して、要素型 (すなわち std:: pair<const Key, T> ) のコンストラクターを呼び出します。
イテレータや参照は無効になりません。
パラメータ
ヒント | - | 新しい要素が挿入される位置の前の反復子 |
引数 | - | 要素のコンストラクターに転送されるパラメーター |
戻り値
新しく挿入された要素を指すイテレータを返します。
要素がすでに存在するために失敗した場合は、同等のキーを持つ既存の要素を指すイテレータを返します。
異常な
いずれかの操作で例外がスローされた場合、この関数は効果がありません (強力な例外保証)。
複雑さ
通常はコンテナ サイズの対数ですが、新しい要素がhint
の直前に。
明確な内容
std::map<Key,T,Compare,Allocator>::clear
ボイドクリア(); |
(C++11まで) | |
void clear() no例外; |
(C++11以降) |
コンテナからすべての要素を消去します。この呼び出しの後、size() はゼロを返します。
含まれる要素を参照する参照、ポインタ、またはイテレータを不正にします。ポストエンド反復子は有効なままです。
パラメータ
(なし)
戻り値
(なし)
複雑さ
コンテナのサイズ、つまり要素の数に比例します。
要素を消去する
std::map<Key,T,Compare,Allocator>::erase
void Erase( iterator pos ); |
(1) | (C++11まで) |
イテレータ消去( const_iterator pos ); |
(C++11以降) | |
イテレータ消去( イテレータ pos ); |
(C++17以降) | |
void Erase( iterator first, iterator last ); |
(2) | (C++11まで) |
iterator Erase( const_iterator first, const_iterator last ); |
(C++11以降) | |
size_type Erase( const key_type& key ); |
(3) |
指定された要素をコンテナから削除します。
1)pos
の。
2) *this の有効な範囲である必要がある範囲[first; last)
内の。
3)key
に。
消去された要素への参照と反復子は無効になります。他の参照とイテレータは影響を受けません。
イテレータは有効で逆参照可能pos
である必要があります。したがって、 end() イテレータ (正当ではあるが逆参照されない) は、pos
で。
パラメータ
位置 | - | 削除する要素へのイテレータ |
最初の最後 | - | 削除する要素の範囲 |
鍵 | - | 削除する要素キー |
戻り値
1-2) 削除された最後の要素に続く反復子。
3) 削除された要素の数。
異常な
1,2) (なし)
3)Compare
オブジェクト
複雑さ
map
のインスタンスがあるとしますc
。
1) 償却定数
2) log(c.size()) + std:: distance(最初、最後)
3) log(c.size()) + c.count(k)
コンテンツを交換する
std::map<Key,T,Compare,Allocator>::swap
void swap( マップ&その他 ); |
(C++17以前) | |
void swap( マップ& その他 ) noExcept(/* 以下を参照 */); |
(C++17以降) |
とother
コンテンツ。個々の要素に対して移動、コピー、または交換操作を呼び出しません。
すべてのイテレータと参照は有効なままです。終了後の反復子は無効になります。
Pred
オブジェクトはスワップ可能(Swappable) である必要があり、非メンバーswap
へのオブジェクトをスワップします。
std::allocator_traits<allocator_type>::propagate_on_container_swap::value が true の場合、アロケーターは非 |
(C++11以降) |
パラメータ
他の | - | コンテンツを交換するコンテナ |
戻り値
(なし)
異常な
|
(C++17以前) |
noexc は以下を指定します。 noexcel(std::allocator_traits<アロケータ>::is_always_equal::value |
(C++17以降) |
複雑さ
絶え間ない。
電話の例
#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <map>
#include <time.h>
using namespace std;
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
struct myCompare
{
bool operator()(const int &a, const int &b)
{
return a < b;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
std::ostream &operator<<(std::ostream &os, const std::pair<const int, Cell> &pCell)
{
os << pCell.first << "-" << pCell.second;
return os;
}
int main()
{
auto genKey = []()
{
return std::rand() % 10 + 100;
};
auto generate = []()
{
int n = std::rand() % 10 + 100;
Cell cell{n, n};
return cell;
};
std::map<int, Cell> map1;
for (size_t index = 0; index < 5; index++)
{
//插入以给定的 args 原位构造的新元素到容器。
map1.emplace(genKey(), generate());
std::cout << "map1: ";
std::copy(map1.begin(), map1.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::map<int, Cell> map2;
for (size_t index = 0; index < 5; index++)
{
//插入新元素到尽可能接近恰在 hint 前的呆滞。原位构造元素,即不进行复制或移动操作。
map2.emplace_hint(map2.begin(), genKey(), generate());
std::cout << "map2: ";
std::copy(map2.begin(), map2.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "after swap" << std::endl;
//将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。
map1.swap(map2);
std::cout << "map1: ";
std::copy(map1.begin(), map1.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
std::cout << "map2: ";
std::copy(map2.begin(), map2.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
//从容器擦除所有元素。此调用后 size() 返回零。
map1.clear();
std::cout << "map1 is empty: " << map1.empty() << std::endl;
map2.clear();
std::cout << "map2 is empty: " << map2.empty() << std::endl;
std::cout << std::endl;
std::map<int, Cell> map3({
{genKey(), generate()}, {genKey(), generate()},
{genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
//从容器移除指定的元素。1) 移除位于 pos 的元素。
std::cout << "map3: ";
std::copy(map3.begin(), map3.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
map3.erase(map3.begin());
std::cout << "map3: ";
std::copy(map3.begin(), map3.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
//从容器移除指定的元素。2) 移除范围 [first; last) 中的元素,它必须是 *this 中的合法范围。
std::map<int, Cell>::iterator bit = map3.begin();
std::advance(bit, 1);
std::map<int, Cell>::iterator ait = bit;
std::advance(ait, 2);
map3.erase(bit, ait);
std::cout << "map3: ";
std::copy(map3.begin(), map3.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
map3.insert({
{genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
//从容器移除指定的元素。3) 移除关键等于 key 的所有元素。
std::cout << "map3: ";
std::copy(map3.begin(), map3.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
bit = map3.begin();
std::advance(bit, 3);
map3.erase(bit->first);
std::cout << "map3: ";
std::copy(map3.begin(), map3.end(),
std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
return 0;
}