Question: C++ We looked at the Dictionary implementation in our textbook. There is an equivalent in the Standard Template Library Map. STL Maps are known as
C++
We looked at the Dictionary implementation in our textbook. There is an equivalent in
the Standard Template Library Map. STL Maps are known as Associative Containers.
Other associative containers are: sets, multiset, and multimap.
Set is only one item. Map and set have unique keys. The multi versions can have
duplicates.
Like other containers weve studied, we have to specify the type.
Were going to try a thesaurus application.
Well have a word and a list of words. I chose to implement my map with a string and
an STL list.
To get a bit of experience, I used just a word to word association so my map was string
and string. Then I worked on creating a map instance that took a string and a list. The
display for string and string was tricky, the display for string and list even trickier.
Online sources include:
http://www.cplusplus.com/reference/map/map/
http://www.cprogramming.com/tutorial/stl/stlmap.html
http://en.cppreference.com/w/cpp/container/map
Youre going to have to do some research. Theres a couple of challenges here. Using
maps, learning some methods, using an iterator with it, then with the STL List and
iterator within an iterator. This is an interactive assignment so you can work together.
Heres my sample output:
Making a word to word association
hint or inkling
perplex or puzzle
Making a word to list of words association
hint or inkling whisper cue
perplex or puzzle confound bewilder
This is the code I have: Was wondering if anyone could make any suggestions for a different way of writing the code but still following the above instructions
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
int main()
{
map wordMap;
wordMap["perplex"] = "puzzle";
wordMap["hint"] = "inkling";
cout << "Making a word to word association ";
// this displays the string and string
for (auto it = wordMap.begin(); it != wordMap.end(); it++)
{
cout << it->first << " or " << it->second;
cout << endl;
}
cout << endl;
list perplexList{ "puzzle", "confound", "bewilder" };
list hintList{ "inkling ", "whisper", "cue" };
map> mapList;
mapList["hint"] = hintList;
mapList["perplex"] = perplexList;
cout << "Making a word to list of words association ";
cout << "hint or ";
for (auto j = hintList.begin(); j != hintList.end(); j++)
{
cout << *j << " ";
}
cout << endl;
cout << "perplex or ";
for (auto k = perplexList.begin(); k != perplexList.end(); k++)
{
cout << *k << " ";
}
cout << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
