Solved

Problem with search using maps..HELP!

Posted on 2004-11-02
193 Views
Last Modified: 2010-04-01
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
Question by:jewee
    4 Comments
     
    LVL 86

    Expert Comment

    by:jkr
    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
     

    Author Comment

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

    Author Comment

    by:jewee
    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
     
    LVL 86

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now