What is a map? A map is a tree type data structure using a key-value pair relationship. I believe it is basically a binary tree that uses keys to get to values stored in it. The map in the code below, uses strings for its key and data of type long for its values. why use this? in the example below, this code will be a word counter. You could use an array, but you run into a problem with it being set in size. resizing arrays are a pain in the butt. you could use a linked list too, but that can end up being unwieldy. Actually maps are the best thing to use because if you think about it, you're counting each individual word (the key) and storing the number of times it's seen in a string or a file (the value). The nice thing about maps is that it's specifications do three things - 1) it is dynamically resizable, 2) it uses array type indexing to get to the values in the map, and 3) it has the speed of a binary tree, ie searching in a binary tree is about a big-O of log 2 n (where the 2 is a subscript). anyway, when programming in c++ i always try to find the appropriate container or data structure to use instead of making my own, since it's already out there free to use. sometimes you need to do things quick and you can't sit there modifying something you wrote to make it work or writing it out from scratch and having to debug it.
Code: Select all
// Programmer: megaspaz
// Project: N/A
// Date: Jan. 20, 2002
// File Name: wordcnt.cpp
// Description:
// This is a small example program using the map container in the C++
// STL. this program will tokenize a string and keep count of individual
// words in the string.
//Microsoft problems with STL
//comment when not using VC++ compiler
//#pragma warning(disable:4786) //Microsoft specific problem with STL
#include <iostream>
#include <string>
#include <map>
#include <exception>
#include <stdexcept>
using namespace std;
//save typing when using STL
typedef map<string,long> a_map;
typedef map<string,long>::iterator MI;
int main ()
{
a_map word_counter;
string input_string = "This is not cool and is not funny, but is very naughty";
char* line;
char* ptr;
try
{
//display the string to parse
cout <<endl;
cout <<"This is the string that will be analyzed: " <<endl <<input_string <<endl <<endl;
//set up string for strtok
line = new char [input_string.length() + 1];
//tokenize string
strcpy (line,input_string.c_str());
ptr = strtok(line," ");
while (NULL != ptr)
{
//assign tmp_str variable the tokenized string
string tmp_str = ptr;
//see if the string is already stored in the map, word_counter
MI a_map_ptr = word_counter.find(tmp_str);
if (a_map_ptr != word_counter.end ()) //this word is already a key in the map
{
//increment the counter of the key and assign the value of the counter
//to the value of the map with the key value of the tmp_str value
word_counter[a_map_ptr->first] = a_map_ptr->second + 1;
}
else
{
//this is the first occurence of the word. insert a new key into the map
//and set it's value to 1
word_counter[tmp_str] = 1;
}
//get the next token
ptr = strtok (NULL," ");
}
//lets display the results
cout <<"Word" <<" " <<"Count" <<endl;
cout <<"----" <<" " <<"-----" <<endl;
for (MI i = word_counter.begin(); i != word_counter.end(); ++i)
{
cout <<endl;
cout <<i->first <<" "<<i->second <<endl;
}
cout <<endl;
return 0;
}
catch (...) //catch anything and exit the program
{
return 1; //error
}
}
Archived topic from Iceteks, old topic ID:210, old post ID:664