Public Group

# Searching through Maps

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

## 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;
}
}
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 on other sites
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 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 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 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.

1. 1
2. 2
3. 3
4. 4
frob
20
5. 5
JoeJ
20

• 11
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632214
• Total Posts
3004858

×