• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 1 out of 1 pages
Other

Working with map and ::iterator

Document preview thumbnail
Preview 1 out of 1 pages

The provided code is a C++ program that manages a simple map of contacts with their associated names. Here's a breakdown of the key components: 1. **Includes and Namespace**: The program includes the necessary headers for input/output, string handling, and using maps from the Standard Template Library (STL). The `using namespace std;` statement allows accessing standard library features without needing to prefix them with `std::`. 2. **Main Function**: This is where the program execution begins. 3. **Map Declaration**: A `map` named `contactPerson` is declared, which uses `string` types for both keys (company names) and values (contact names). 4. **Populating the Map**: The map is populated with four entries where each company name is mapped to a fictional contact name, some of which play on jokes related to their brands (e.g., "E. Coli" for "Chipotle Mexican Grill"). 5. **Output Statements**: - It prints the contact name associated with "Takata Inc". - It displays the total number of contacts stored in the map using `contactP()`. 6. **Iterating Over the Map**: A `for` loop is used to iterate through each element in the `contactPerson` map. For each entry, it prints the company name and its associated contact. 7. **Checking for Existence**: Inside the loop, it checks if "Volkswagon of America" is in the map using `find()`. If the company is not found, it outputs a message; otherwise, it confirms its presence. However, the check is incorrectly placed inside the loop, so it will print the existence message for each contact. Overall, this program demonstrates basic map functionality in C++, including adding entries, retrieving values, iterating through the map, and checking for the existence of a key. The use of humorous names in the map values adds a light-hearted touch to the program.

Content preview

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
map<string, string>contactPerson;

contactPerson["Volkswagon of America"] = "I.B. Cheaton";
contactPerson["Wallers Fargo Bank"] = "Ima Swindler";
contactPerson["Chipotle Mexican Grill"] = "E. Coli";
contactPerson["Takata Inc"] = "I. X. Plodum";

cout << "Takata Inc = " << contactPerson["Takata Inc"] << endl << endl;
cout << "# of contacts: " << contactPerson.size() << endl << endl;

for (map<string, string>::iterator it = contactPerson.begin(); it!=
contactPerson.end(); ++it)
{
cout << (*it).first << ":\t" << (*it).second << endl << endl;

if (contactPerson.find("Volkswagon of America") == contactPerson.end())
cout << "It's not in the map!" << endl << endl;
else
cout << "It's in the map" << endl << endl;
}
}

Connected book
 image
Publisher: 2017 ISBN: 9780134498379 Edition: Unknown

Document information

Uploaded on
November 6, 2025
Number of pages
1
Written in
2025/2026
Type
Other
Person
Unknown
$11.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
68
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions