Thread: map problem

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    map problem

    Hello..

    Whats wrong with that code:

    Code:
    map <string, vector <string> > smp;
    
    string n = "somestr";
    
    map <string, vector <string> >::const_iterator v_it;
    if ( (v_it = smp.find(n) ) != smp.end() ) {
    	vector <string> cm = v_it->second; //the critic line
    }
    Thanks a lot for help
    Kind regards

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Well, in that code, cm no longer exists as soon as the closing brace of the if statement is reached.

    A local variable is only in-scope within the braces in which it's declared. You need to pull the declaration outside the if() statement if you want to use cm later.

    Your code should compile, but you can't use cm later on.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  2. problem with a map iterator
    By anykey in forum C++ Programming
    Replies: 17
    Last Post: 04-29-2005, 11:49 PM
  3. STL MAP Question
    By deadpoet in forum C++ Programming
    Replies: 11
    Last Post: 02-24-2004, 06:37 AM
  4. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM
  5. my map is showing up 90 degrees turned!
    By frenchfry164 in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2002, 02:32 PM