Is the default allocator resetting the int value?

When using STL containers, I'm not sure if the default destination assigned to the dispenser has been reset. The following code indicates yes to the question:

#include <map>
#include <iostream>

int main() {
  using namespace std;
  map<int, int> m;
  cout << m[1234] << endl;
}

Since no document has confirmed this, I dare not take it for granted.

+5
source share
4 answers

In the implementation, std::map::operator[]you will see that if the element is not found in the index, a new one is inserted and returned:

ReturnValue = this->insert(where, make_pair(key_value, mapped_type()));

where mapped_typeis the second type, in your case int. So yes, it is initialized by default 0since it is inserted as mapped_type().

+6
source

, , , . , , . , "".

, !... , tb , , , - ( , , , , ).... - , (, 0xdeadbeef ).

+6

, : 8.5.5 ++ - ANSI ISO IEC 14882 2003

T : T - (3.9), 0 (), T;

+2

, - new int() default-ininitialize int 0, ISO ++ ISO 14882: 2003. 8.5, -5,7:

zero-initialize T :

  • T - (3.9), 0 (), T;

value-initialize T :

  • T - ( 9) (12.1), T ( , T );

  • T - , , T ;

  • T - , ;

default-initialize T :

  • T - -POD ( 9), T ( , T );

  • T - , ;

  • .

, , ..(), .

++ 11 ISO Standard.

+1

All Articles