Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Overig

Working with map and ::iterator

Beoordeling
-
Verkocht
-
Pagina's
1
Geüpload op
06-11-2025
Geschreven in
2025/2026

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.

Meer zien Lees minder

Voorbeeld van de inhoud

#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;
}
}

Geschreven voor

Documentinformatie

Geüpload op
6 november 2025
Aantal pagina's
1
Geschreven in
2025/2026
Type
OVERIG
Persoon
Onbekend
$11.99
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kan je een ander document kiezen. Je kan het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
tinahuynh

Maak kennis met de verkoper

Seller avatar
tinahuynh California State University, Long Beach
Bekijk profiel
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
-
Lid sinds
8 maanden
Aantal volgers
0
Documenten
68
Laatst verkocht
-

0.0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen