HelpSearching through a map

Posts 1–7 of 7 · Page 1 of 1
Searching through a map
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;
	}
}
I was talking to my boss today about this program, he saw the part about me extracting the TID and site from the .txt file and recommended using a data table.

Does anyone know anything about this? I'll talk to him some more when he actually has the time, too.
Damn this forum is so inactive.....

Oh and sorry to be so useless since i dont have much experience with the full library. I'm reading C++ Primer (5th edition) and i'd recommend you to do the same to get a good knowledge of the library and shitload of it's functions.
Quote Originally Posted by Knochove View Post
Damn this forum is so inactive.....

Oh and sorry to be so useless since i dont have much experience with the full library. I'm reading C++ Primer (5th edition) and i'd recommend you to do the same to get a good knowledge of the library and shitload of it's functions.
This is chapter 11 bro lol

I know how to create map with a key value of numerical value, I think I can re-use my struct to do a find(iterator, iterator, map.first/second) but I don't want to get too deep into this project until my boss actually approves of us using the program... If he says no I can't do anything with it anyway.

Anyway, when I get into it I'll post the solution to my original problem.

And since we're reading the same book I'll post the exercise solutions I got for the exercises I've done so far. I didn't do them all, some I skipped, some (at the time) I couldn't solve. I just could do every single one because some exercises can take 30+ minutes to do.

So if a mod could approve it, or we could even start a new thread for exercise solutions for "BookName" to help other newbies

And one more thing, look forward to Chapter 9 and Chapter 10, that's when you can really start doing some real stuff
https://www.virustotal.com/en/file/b...is/1418268541/
http://virusscan.jotti.org/en/scanre...f26c6410f1a1f7
C++ Primer Exercises_mpgh.net.rar53 KB · 7 downloads Clean
He is reading it. XD
/Approved.
Next time remember to add the virusscans please.
Working on it tonight and I figured it out, it's pretty easy in C++11

(I made it a function in my class)
The Map:
Code:
std::map<std::string, std::vector<std::pair<std::string, std::string>>> database;
The Search Function:
Code:
void Command::search()
{
	std::string oper;
	std::vector<std::string> sites_found;
	bool nfound = true;
	for (auto Operator : database){
		oper = Operator.first;
		for (auto info : database[oper]){
			if (parameter != "exact" && (caseInsensitive(info.first).find(caseInsensitive(identifier)) != std::string::npos || caseInsensitive(info.second).find(caseInsensitive(identifier)) != std::string::npos)) {
				sites_found.push_back('\t' + info.first + " ... " + info.second);
				if (nfound)
					nfound = false;
			}
			else if (info.first == identifier || info.second == identifier) {
				sites_found.push_back('\t' + info.first + " ... " + info.second);
				if (nfound)
					nfound = false;
			}
		}
		if (sites_found.size() != 0){
			auto it = sites_found.cbegin();
			std::cout << "[Operator] " + oper << std::endl;
			for (auto it : sites_found){
				std::cout << it << std::endl;
			}
			std::cout << '\n';
			sites_found.clear();
		}
	}
	if (nfound)
		std::cout << "Unable to find \'" << identifier << '\'' << ((parameter == "exact") ? " with \'" + parameter + "\' parameter" : "") << std::endl;
	parameter.clear();
}

std::string Command::caseInsensitive(std::string input)
{
	std::string output;
	std::transform(input.begin(), input.end(), std::back_inserter(output), tolower);
	return output;
}
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us