Link to home
Start Free TrialLog in
Avatar of jewee
jewee

asked on

Problem with search using maps..HELP!

In the following code, I have a map with a string as the key and a corresponding string.  I also have a vector of strings.  What I am trying to do is to search through the map for the vector string, if found, then store the corresponding string to a buffer.

There is a problem with how I implemented the searching capability since it is true every time.
Also, outside of this for loop, I need to check if a particular file exists within the vector.

td::string ViewForm::createConfigFile(std::map <std::string, std::string>& groupFiles,
                                         std::vector <std::string>& signalList)
{
  bool groupFileMatch = false;
  std::string fileScript;
  std::string configFile = "";
  std::vector <std::string>::const_iterator signalIt;
  std::map <std::string, std::string>::const_iterator mapIt;

  fileScript = "cat ";
  if(!signalList.empty())
  {
     groupFileMatch = true;
     for (signalIt = signalList.begin(); signalIt != signalList.end(); signalIt++)
     {
           mapIt = groupFiles.find(signalIt->c_str());  
           if (mapIt != groupFiles.end())
           {
               fileScript += mapIt->second + " ";
           }
      }
  }
 
   //Special case.  
   //For button7, the NA1 config file needs to be concatenated for all cases.
   //If a NA1 file exists, this needs to be concatenated to the config file
  printf("IN CONFIG...\n");
  mapIt = groupFiles.begin();
  mapIt = groupFiles.find("NA1");
  if (mapIt != groupFiles.end())
  {
     groupFileMatch = true;
     fileScript += mapIt->second + " ";
  }
Avatar of jkr
jkr
Flag of Germany image

You can always use

mapIt = groupFiles.find(*signalIt);  

instead of

mapIt = groupFiles.find(signalIt->c_str());  

Apart from that, the code looks OK. Try calling

void dump_map std::map <std::string, std::string>& theMap){

  std::map <std::string, std::string>::const_iterator mapIt;

  for ( mapIt = theMap.begin(); mapIt != theMap.end(); ++mapIt) {

   std::cout << mapIt->first << "\t" <<  mapIt->second() << std::endl;
  }
}

to see what the map actually contains before looking up values.
Avatar of jewee
jewee

ASKER

Just checked it...The map does contain all values and is returning the first value even though it does not match.
Avatar of jewee

ASKER

I do not necessarily need to check and see if it is a match...The string values in the vector will match up with the keys in the map.  Therefore, there will always be a match.  
 I guess I am just trying to understand the find function.  The iterator should return the position of the key entered.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial