• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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 + " ";
  }
0
jewee
Asked:
jewee
  • 2
  • 2
1 Solution
 
jkrCommented:
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.
0
 
jeweeAuthor Commented:
Just checked it...The map does contain all values and is returning the first value even though it does not match.
0
 
jeweeAuthor Commented:
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.
0
 
jkrCommented:
No, it actually seems to work - checked with

#include <string>
#include <map>
#include <vector>
#include <iostream>
using namespace std;

void 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 + " ";
 }

  cout << fileScript;
}

int main () {

map <std::string, std::string> groupFiles;
std::vector <std::string> signalList;

groupFiles.insert(map<std::string, std::string>::value_type("test0", "mapped0"));
groupFiles.insert(map<std::string, std::string>::value_type("test1", "mapped1"));
groupFiles.insert(map<std::string, std::string>::value_type("test2", "mapped2"));
groupFiles.insert(map<std::string, std::string>::value_type("test3", "mapped3"));

signalList.push_back("test1");
signalList.push_back("test3");

createConfigFile(groupFiles, signalList);

return 0;
}

and got

IN CONFIG...
cat mapped1 mapped3

which is correct.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now