Jump to content
  • Advertisement
Sign in to follow this  
Dark_Glitch

Searching through Maps

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm trying to find a clever way to search through the map I created and I tried to use the debugger, to no avail.
#include <iostream>
#include <map>
#include <fstream>
#include <string>

using namespace std;

void AddMapData(map<string, string> &itemMap, string name, string filename)
{
	itemMap[name] = filename;
}

void GetItemInfo(map<string,string> &ItemMap)
{
	ifstream fin("C:/Game Folder/Items/MasterItemList.lis");

	if(fin.fail())
	{
		cout << "Somethings wrong with the Master Item List."
			 << endl
			 << "Please solve this problem by recreating the file or something...";
	}

	string ignoredWord, itemName, filename;

	while(!fin.eof())
	{
		fin >> ignoredWord >> itemName;
		fin >> ignoredWord >> filename;
		AddMapData(ItemMap, itemName, filename);
	}	
}
map<string, string> items;
int main()
{
	cout << "This program will search for a specific item that the end-user types in." << endl;
	cout << "Please choose a created item and enter the name, no spaces: ";

	string selection;
	cin >> selection;


	for(map<string,string>::iterator iter = items.begin(); iter != items.end(); iter++)
	{
		if(selection == (*iter).first)
		{
			cout << "Filename: " << (*iter).second << endl;
		}

		else
		{
			cout << "Searching..." << endl;
		}
	}

	system("PAUSE");
	return 0;
}


I assumed that each time the for loop goes through each iteration, that if block will be tested. If selection is equivalent to the iteration, then it should print the filename to the screen, right? I've never used maps before so I'm attempting to get familiar with them and what I can do with them and whatnot. On program run, everything up until the for statement runs properly, but when I enter a name of an item, i.e. TestSword, nothing happens, it just skips to the pause and ends the program. The map works, as I have already tested it in a similar program without the end-user input. I need to know about that for statement. Should I be doing this another way? EDIT: Ok, I must have a virus or something on this damn pc because text keeps vanishing in my compiler window. I know that sounds lame but the call to GetItemInfo(items) VANISHED in the window and it was JUST THERE. I solved the problem, this works, don't waste your time. :D Thanks for reading.

Share this post


Link to post
Share on other sites
Advertisement
The clever way would be to use the function provided by the class instead of your loop:
map<string,string>::iterator iter = items.find( selection );

if (iter != items.end())
{
cout << "Filename: " << (*iter).second << endl;
}
Also you might want to add to the map using:
itemMap.insert( std::make_pair( name, filename ) );

Share this post


Link to post
Share on other sites
See...I didn't even know I could do that.

Is there a link or a book someone could point me to that will show me EVERYTHING you can use with maps? I'd really appreciate this, cause so far I'm not finding stuff that gives me all the info I need.

I'd stop posting questions so much and maybe one day start answering some real problems.

Share this post


Link to post
Share on other sites
Well, most C++ references give you a complete overview of std::map's members, such as MSDN. If you want a dead tree recommendation, then try "The C++ Standard Library" by Josuttis. And of course, there's always the C++ standard itself.

Share this post


Link to post
Share on other sites
The whole point of using a std::map is that it knows how to look things up more quickly than by just looking through each element in a loop. Another name for a map (the general computer science concept) is "associative array". That is, it associates the key to the value; the key is all the information needed to find the value, and the finding is the class's job.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!