map problem

This is a discussion on map problem within the C++ Programming forums, part of the General Programming Boards category; Hello.. Whats wrong with that code: Code: map <string, vector <string> > smp; string n = "somestr"; map <string, vector ...

  1. #1
    l2u
    l2u is offline
    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
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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, 05:37 AM
  4. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 08:11 AM
  5. my map is showing up 90 degrees turned!
    By frenchfry164 in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2002, 01:32 PM

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