Map will insert a new value when i request the value of a non existing key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bartdeliefde
    New Member
    • Feb 2008
    • 3

    Map will insert a new value when i request the value of a non existing key

    Hey,

    I run into the following problem:

    I have a map:
    map<string,*ofs tream> FILEHANDLES
    map<string,ofst ream*>::iterato r FILEHANDLES_it;

    I fill the map in a loop with a certain number of values, let's say 3, with the key the filename and the value the pointer to a file in memory.
    Let's say that after my loop the map contains the following values:
    key | value
    "afilename. dat" | 8x1645497
    "bfilename. dat" | 8x4651318
    "cfilename. dat" | 8x1978465


    When I want to use an item I do the following:
    ofstream *myFilePointer;

    myFilePointer = FILEHANDLES["afilename. dat"];
    myFilePointer will contain the value: 8x1645497
    ..
    myFilePointer = FILEHANDLES["bfilename. dat"];
    myFilePointer will contain the value: 8x4651318
    ..
    myFilePointer = FILEHANDLES["cfilename. dat"];
    myFilePointer will contain the value: 8x1978465

    which works fine.

    Except when I request the filehandle of a filename which isn't in the map, like
    myFilePointer = FILEHANDLES["notinthemap.da t"];

    When I do this, suddenly my map will contain 4 items instead of three.
    The key of this item is an empty string "" and the value is a null pointer.

    Why?
  • bartdeliefde
    New Member
    • Feb 2008
    • 3

    #2
    Originally posted by bartdeliefde
    When I do this, suddenly my map will contain 4 items instead of three.
    The key of this item is an empty string "" and the value is a null pointer.
    By the way, the key is not an empty string but contains the filename "notinthemap.da t".

    Comment

    • bartdeliefde
      New Member
      • Feb 2008
      • 3

      #3
      Okay,

      so the operator [] will also insert the value if it isn't in the map.

      Problem solved.

      This post can be closed.

      Comment

      Working...