C++ map

2597 ワード

mapの使い方まとめ:
1.
第1種
:
Insert関数pairデータ挿入
.
 mapStudent.insert(pair(1, "student_one"));
第2種
:
insert関数挿入value_typeデータ
mapStudent.insert(map::value_type (1, "student_one"));
第3種
:
配列方式データの挿入
 
 mapStudent[1] = 
 "student_one";
mapStudent.insert(map::value_type (1, "student_one"));
mapStudent.insert(map::value_type (1, "student_two"));
上記の2つの文が実行された後、mapの1というキーワードに対応する値は「student_one」であり、2番目の文は有効ではありません.では、insert文が正常に挿入されたかどうかをどのように知るかについて、pairで挿入に成功したかどうかを得ることができます.手順は以下の通りです.
pair::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map::value_type (1, "student_one"));
私たちはpairの2番目の変数を通じて挿入に成功したかどうかを知っています.その1番目の変数はmapの反復器を返しています.挿入に成功すればInsert_Pair.secondはtrueのはずです.そうしないとfalseです.
map要素を削除するには:
mapのエントリを削除するにはerase()
このメンバーメソッドの定義は次のとおりです.

iterator erase(iterator it); // iterator erase(iterator first, iterator last); // size_type erase(const Key& key); //

clear() enumMap.erase(enumMap.begin(), enumMap.end());

map hash_map :

hash_map map ,map hash_map, ,hash_map map , , 。
map         。
  hash_map       C++ 
, STL 。 。 hash_map , hash_map 。