Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with search using maps..HELP!

Posted on 2004-11-02
4
Medium Priority
?
204 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
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12473955
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
ID: 12474240
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
ID: 12474286
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:
jkr earned 2000 total points
ID: 12474382
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

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

604 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