Need help with vectors and maps assignment please. Please send correct code and
ID: 665655 • Letter: N
Question
Need help with vectors and maps assignment please. Please send correct code and ouput so I do not get confused with the concepts. The files needed are in the dropbox below:
https://www.dropbox.com/sh/36sxpfkktf7rawv/AABiPMaEmnPVSsVlR65Jr7S6a?dl=0
In a previous lab, we wrote a program to find all of the words in a text file, count them, then output the word and count values. This week we will look at using C++’s Standard Template Library (STL) to implement these in similar, but more efficient and simpler ways.
Vectors
vector <type> variable_name;
Vectors are a built-in library that are basically the same as a dynamic array. You can use the push_back function to store something of type into the vector. For example, a vector<int> scores would let you push integers into the vector by calling scores.push_back(1); (where 1 is any integer). You can retrieve elements like you would with any array by using the [] operator (like cout << scores[0]). There are some other functions that have useful features: begin() – retrieves the first element in the vector
end() – retrieves the last element in the vector
size() – retrieves the size of the vector
empty() – a Boolean if the vector is empty or not
Maps
map<type1, type2> variable_name;
Maps are similar to vectors in that you can add elements to them. However, they work in key-value pairs. type1 and type2 correspond to the types of data you want to associate. To store a list of moons, you can use the planet name as a key, and the name of the moon as a value. For example:
map<string, string> moons;
moons[“Mercury”] = “None”;
moons[“Venus”] = “None”;
moons[“Earth”] = “Moon”;
moons[“Mars”] = “Deimos, Phobos”;
When you use iterators (below) with maps, the key is called first and the value is called second.
Iterators
vector<type>::iterator variable_name;
map<type1, type2>::iterator variable_name;
Iterators are ways of traversing through the libraries to go through each element in them. Similar to using a variable to remember a position of an array in a loop, an iterator can remember the position in a library so you can use them easily in loops.
Iterators become pointers to the current element. So as you loop through a vector, an iterator points to the memory location of the first element, then the second, and so on. Since a pointer is a memory address, you can just use the ++ address to increment it to the next element.
For a vector, you can set up a loop like this:
for(vector<type>::iterator iter = vectorname.begin(); iter != vectorname.end(); iter++)
When the loop starts, iter is the iterator name, and it will loop through a vector of type. It starts at vectorname.begin() (which is the first element), and will keep increasing until it gets to vectorname.end(). Each loop it increments itself (iter++).
The iterator must match the type it is iterating through, including the template name. A vector<double>::iterator can’t loop through a vector<int>, for instance.
Since the iterator is a pointer to an element of a vector or map (or really any STL container), to work with the element, you have to dereference it. The way we’ve learned is through the * operator. Other examples:
If it’s a vector<int>, you can do *iter = 5;
If it’s a vector<double>, you can do *iter = 4.85;
If it’s a vector<string>, you can do *iter = “Vectors rock!”;
If the vector is of a class, remember that
(*iter).function() is the same as iter->function()
and you can use either one to access the class information.
Assignment – Part 1
The exercise of counting the number of words by using a dynamic array of Word objects can be rewritten using a vector instead of a dynamic array.
1) Create a solution named Lab11-Vector. Rename the Lab11.cpp file to Lab11-vector.cpp.
2) Remove the resizeArray() declaration and function, and then replace the dynamic array with a vector.
3) Replace the loops using iterators.
4) Remove the output line that outputs the maximum size of the array.
5) Remove any unnecessary variables or functions remaining.
The code should work exactly as it did before and should be shorter too.
Assignment – Part 2
If you haven’t noticed, our Word class is a key-value pair. string word is the key, and int count is the value. The whole class can be removed and a map can be used instead.
1) Make a solution called Lab11-Map. Copy and rename the Lab11-vector.cpp to Lab11-map.cpp.
2) Remove the resizeArray() declaration and function, and remove the Word class references.
3) Replace it with a map to replace the same key-value pair that the Word class used.
4) Rewrite the loops to use iterators.
5) Remove the output line that outputs the maximum size of the array.
6) Remove any unnecessary variables or functions remaining. (Note the Word class is no longer needed, so the files containing it are no longer needed either.)
Your code should not only be shorter than the original assignment, it should be shorter than part 1 too.
Explanation / Answer
using vector
#include "Word.h"
#include <bits/stdc++.h>
using namespace std;
// Starting point
int main()
{
// File I/O
ifstream fin("input.txt");
ofstream fout("output.txt");
vector<Word*> myvec;
int size = 0;
// Read in file
string wordIn;
// While there are more words
while(fin >> wordIn){
// It's not found by default
bool found = false;
// For each element in the array, if it exists, increment its count, and mark it as found
for (int i = 0; i < myvec.size(); i++){
if (myvec[i]->getWord() == wordIn){
myvec[i].increment();
found = true;
break;
}
}
// If not found, add it. If array is full, resize it first.
if (!found){
myvec[size]->setWord(wordIn);
myvec[size]->increment();
size++;
}
}
// Print list of words and counts
fout << "Words found: " << size << endl;
fout << "Array's max size: " << max << endl << endl;
// For each word, print it's word/count pair.
for(int i = 0; i < size; i++)
fout << myvec[i]->getWord() << " - " << myvec[i]->getCount() << endl;
}
using Map
#include <bits/stdc++.h>
using namespace std;
// Starting point
int main()
{
// File I/O
ifstream fin("input.txt");
ofstream fout("output.txt");
map<string,int> mymap;
int size = 0;
// Read in file
string wordIn;
// While there are more words
while(fin >> wordIn){
// It's not found by default
if (mymap.find(wordIn) == mymap.end()){
mymap[wordIn] = 0;
}
else{
mymap[wordIn]++;
}
}
// Print list of words and counts
fout << "Words found: " << size << endl;
fout << "Array's max size: " << max << endl << endl;
// For each word, print it's word/count pair.
for (std::map<string,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
cout << it->first << " - " << it->second << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.