0

マップのマップの内部マップの最初の要素を削除するにはどうすればよいですか?

私は何かをやってみました

my_map[here].erase(my_map[here].begin()) 

しかし、私は予期しない結果を得ています。どんな提案やリソースも素晴らしいでしょう。

4

1 に答える 1

1

my_map[here] は実際に存在しますか?

あなたはそれを見つけたいかもしれません、すなわち

if((auto it = my_map.find(here)) != my_map.end()) {
  it->erase(it->begin());

}

アクセスしようとしたときに my_map[here] が存在しなかった場合、そこに新しい要素が作成されます。

http://www.cplusplus.com/reference/map/map/operator%5B%5D/

If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).

これを防ぐために、find上で示した機能を使用できます。 find指定されたキーを持つ要素を検索します。何かが見つかった場合は、その要素への反復子を返します。my_map.end()それ以外の場合は、最後の要素ではなく、構造体の終わりを示す特別な反復子であるを返します。

于 2013-03-31T02:49:39.277 に答える