How to use map container?

Zevvysan

I'm not very experienced with maps and was hoping someone could give me some pointers and examples on how to use them. The goal for this map is to keep track of how many customers a waiter serves in a restaurant. Once the restaurant closes, I would then list each waiter and the number of people served. This is the map as it appears in the h file.

std::map<std::string,int> servers;

The waiters can be represented as "waiterName". What would an example of adding on customers served to a waiter look like? And how would I list the waiters and customers served?

cout << waiterName << " served " << (customers served by this waiter) << endl;

Thank you for the help!

Swordfish
#include <map>
#include <string>
#include <iostream>

std::ostream& operator<<(std::ostream &os, std::pair<std::string, int> const &s)
{
    os << s.first << " served " << s.second;
    return os;
}

int main()
{
    std::map<std::string, int> servers;
    servers["foo"]++;
    servers["foo"]++;
    servers["foo"]++;
    servers["foo"]++;
    servers["bar"]++;
    servers["bar"]++;

    for (auto s : servers)
        std::cout << s << '\n';
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related