Thread: A Simple Question about STL's map container

  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,897
    >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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,366
    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, 09: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