Industrial Training

VISUAL C++



/****************************************************************************
1..Second version of the first assignment(using map function), this runc only with VISUAL C++.
*****************************************************************************/

#include
#include
#include
#include
#include
using namespace std;
char buffer[500];
void main()
{ cout<<"Enter a string and press # to complete";
cin.getline(buffer,499,'#');//geting the line into the twister string
//we use typedef as because its not advantageous to type out
//map::const_iterator very often typedef map Collocation;//map variable initialized with
//string type for key and int type for the associated value

typedef Collocation::const_iterator WordIter;//we use WordIter to display
//the contents of Collocation
Collocation M;
istringstream text(buffer);//the istringstream operator is used for
//input operations to string objects, that is the buffer object is read
//through the text stream just like any other stream
istream_iterator begin(text);//istream_iterator objects are
//pretending like pointers, the begin object reads from text
istream_iterator end;//the variable end is another object that
//corresponds to end of file, the istream iterators reads sets of
//characters ending with spaces
for(;begin!=end;++begin)
M[*begin]++;//checks if string is present, if yes it increments
//the corresponding int field else it inserts the string with
//its int field set to 0, the[] operator is overloaded, the
//expression M[*begin] returns a pointer with the associated
//string *begin, we then increment the field
for(WordIter m=M.begin() ; m!=M.end();++m)//we then use the map iterator
//m to print the result
cout<second<<" "<first< }

Hi I am Pluto.