A Simple Question about STL's map container

This is a discussion on A Simple Question about STL's map container within the C++ Programming forums, part of the General Programming Boards category; Code: map<string,int> dict; dict["we"]++; cout<< dict["vermeer"]; I was told that "indexing a map 'inserts' the key into the map if ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A Simple Question about STL's map container

    Code:
    	map<string,int> dict;
    	dict["we"]++;
    	cout<< dict["vermeer"];
    I was told that "indexing a map 'inserts' the key into the map if it is not already present, if 'vermeer' is not present, this form of search enters it into the map with an occurence count of zero". <Essencial C++> stanley lippman, pp91.

    What does this mean? I think before cout<< dict["vermeer"]; the "vermeer" entry's value is already 0.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What does this mean?
    It means that when you say dict["vermeer"], a record is added to the map with the key "vermeer" and the value 0.

    >I think before cout<< dict["vermeer"]; the "vermeer" entry's value is already 0.
    No, "vermeer" isn't an entry in the map before that line. "we" is, but not "vermeer".
    My best code is written with the delete key.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    http://www.sgi.com/tech/stl/Map.html
    Use find() to test whether an entry exists, without creating it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    which solution takes longer time: indexing vs find()?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    They should take the same time (essentially the same algorithm), except when one adds a new element to the map by accessing a previously nonexistent index.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by meili100 View Post
    which solution takes longer time: indexing vs find()?
    An indexing operation on a map is just a find() but if the element doesn't exist, it is allocated. So for items which already exist in the map, the performance should be the same.

    Indexing creates the element because it has to. The indexing operator returns a reference to an element. In order to do this, the element has to exist.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you want a new record to be created if one doesn't exist, or if you know that it must exist, then use operator[]. If you want to check first, then use find. If you already have an iterator t the element, don't use either, use the iterator you already have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM
  3. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  4. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21