map - search it

This is a discussion on map - search it within the C++ Programming forums, part of the General Programming Boards category; I want to search my map. If I search for a string that's not listed, it bombs for obvious reason ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    map - search it

    I want to search my map. If I search for a string that's not listed, it bombs for obvious reason (see the code).
    How do I phrase this to do the following:
    x = map.find(string)
    if x != map.end
    -spit it out
    else
    -spit out spaces.

    Code:
    	it = freq.find ( "ofx" );
    	cout << it->first;

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    this works but is it the best way?
    Code:
    	it = freq.find ( "ofx" );
    	if (it != freq.end())
    	{
    		cout << it->first;
    	}

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    That is the way I usually do it.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Your solution is simple, clear, and effective; stick with it.

    Additionally, there is really no other way that is fundamentally different.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

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. 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
  3. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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