Question

a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string> such that the values in the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.

map<int,string> arrayToMap(string arr[],int arrSize)

{

map<int,string> x;

for(int i=0;i<arrSize;i++)

x[i]=arr[i];

return x;

}

b.

void printMap(map<int,string> x)

{

map<int,string>::iterator i;

for(i=x.begin();i!=x.end();i++)

cout << i->first << ":" << i->second << endl;

}

CODE TO VERIFY:

#include <iostream>

#include <map>

#include <iterator>

using namespace std;

//to store array indices as key and corresponding string as value in map and return it

map<int,string> arrayToMap(string arr[],int arrSize)

{

map<int,string> x;

for(int i=0;i<arrSize;i++)

x[i]=arr[i];

return x;

}

//to print the map in the form of key:value using iterator

void printMap(map<int,string> x)

{

map<int,string>::iterator i;

for(i=x.begin();i!=x.end();i++)

cout << i->first << ":" << i->second << endl;

}

int main()

{

//testing the function

string arr[5]={"Debjit","Ganguli","Is","the","best"};

map<int,string> x=arrayToMap(arr,5);

printMap(x);

}

Output:

0:Debjit 1:Ganguli 2:13 3: the 4:best

Add a comment
Know the answer?
Add Answer to:
a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a function named "FriendsWithPets" that takes a const reference to a std::map of my...

    C++ Write a function named "FriendsWithPets" that takes a const reference to a std::map of my friends names (std::string) to the number of pets they own (int) and returns the number of friends (int) that have at least one pet. You should be using the STL algorithms to achieve this, credit is only given if your solution doesn't have any looping constructs (no "while" or "for" keywords anywhere in the solution).

  • Write a function that takes an array of C-strings as a parameter and the number of...

    Write a function that takes an array of C-strings as a parameter and the number of elements currently stored in the array, and returns a single C-string pointer that is the concatenation of all C-strings in the array. Note: The function must take 2 parameters and return "char *". Any other ways will cause some deduction of points.

  • Do the following: - Write and test a function that takes an array of doubles and...

    Do the following: - Write and test a function that takes an array of doubles and returns the average of the values in the array - Write and test a function that takes an array of doubles and returns number of values in the array that are above the average of the values in the array - Write and test a function that takes an array of Strings and returns number of values in the array that start with an...

  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

  • Write a value-returning C++ function returns the average length of an array of strings. Name the...

    Write a value-returning C++ function returns the average length of an array of strings. Name the function stringsAverageLength and use the following header: double stringsAverageLength(string array [], int n) { } where the parameter 'array' has 'n' strings and the return value is the average length of all of the strings in the array. For example, if the function is called like this: string cars[3] = { "Toyota", "Ford", "Tesla" }; cout << fixed << setprecision(2) << stringsAverageLength(cars, 3) <<...

  • Write a Java program that takes an int array as input, and returns the average of...

    Write a Java program that takes an int array as input, and returns the average of all the values in the array.  No class construction is necessary; however, the complete signature and definition and brief documents are needed.  

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a...

    Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a list of the integers that can be obtained by converting the strings to integers using an expression like int(string). Strings which cannot be converted in this way are ignored. Hint: the statement pass is a statement that does nothing when executed. For example: Test Result strings = ['123', '-39', '+45', 'x', 'COSC121', '123+', '12-3'] print(valid_integers(strings)) [123, -39, 45]

  • Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...

    Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...

  • Write a function int findLast(string words[], int numWords, string target) that returns the index of the...

    Write a function int findLast(string words[], int numWords, string target) that returns the index of the last occurence of target in words or -1 if there is no occurrence. Remember: you can compare string objects using ==, these are NOT C-strings !

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT