I'm wanting to create a map with two string values and be able to search through each value for a string
Code:
int createMap(std::map<std::string, std::string> &inMap, std::string tid, std::string site)
{
size_t size = inMap.size();
inMap.insert(std::pair<std::string, std::string>(tid, site));
if (inMap.size() > size){
return 1;
}
else{
std::cerr << "Failed to insert TID and site into map" << std::endl;
return 0;
}
}
This will add a pair to it no problem but how do I go about searching for each element?
If I had a map called example with 5 names
Code:
map<string, string> example {{"Jack", "Miller"}, {"Anthony", "Anderson"}, {"Jimmy", "John"}, {"Hello", "Operator"}, {"Michael", "Jackson"}};
How could I search for John and return Jimmy?
This class will pritn out each element in a multimap I already use. Am I close to being able to do what I want and just don't realize it?
Code:
typedef std::map<std::string, std::string> CustomMap;
template <class T>
struct PrintMyMap : public std::unary_function<T, void>
{
std::ostream& os;
PrintMyMap(std::ostream& strm) : os(strm) {}
void operator()(const T& elem) const
{
os << elem.first << elem.second << "\n";
}
};
I can already do it with a .txt file but I want to do this in maps (I'm creating a the map using a 1400 line text file)
Code:
int searchFor(const std::string str)
{
std::ifstream operatorsFile("Databases.txt");
std::string currentLine;
bool flag = false;
if (operatorsFile.good()){
while (operatorsFile.good()){
getline(operatorsFile, currentLine);
if (currentLine.find(str) != std::string::npos){
flag = true;
std::cout << '\t' << currentLine << std::endl;
}
currentLine.clear();
}
if (!flag){
std::cout << "\tUnable to find \'" << str << '\'' << std::endl;
return 0;
}
else
return 1;
}
else{
std::cout << "Unable to open Databases file\n" << std::endl;
return 0;
}
}