18. Use the array strList of strings along with a map<string, int> object mLengt
ID: 663262 • Letter: 1
Question
18. Use the array strList of strings along with a map<string, int> object mLength to write code
segments that perform designated tasks:
string strList[ ] = { "store", "map", "array", "set"
"multimap", "string" };
int strListSize = sizeof(strList)/ sizeof(string);
map<string, int> mLength;
int i;
a) Write a loop that enters each string from the array into the map as the key-value pair (string, string length).
b)Declare an iterator that locates elements in the map mLength. Use the iterator to output all strings that have a lenght of 5.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int main() {
string strList[] = { "store", "map", "array", "set","multimap", "string" };
int strListSize = sizeof(strList)/ sizeof(string);
map<string,int> mLength;
int i;
for (i = 0; i < strListSize; i++){
mLength[strList[i]] = strList[i].length();
}
for (std::map<string,int>::iterator it=mLength.begin(); it!=mLength.end(); ++it){
if (it->second == 5)
cout << it->first << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.