Copying Containers - C++ STL

C++ examples for STL:map

Description

Copying Containers

Demo Code

#include <iostream>
#include <map>

using namespace std;

class Scrumptious
{
public://w w  w.java 2  s. c  o  m
    string Dessert;
};

bool operator < (const Scrumptious & first, const Scrumptious & second)
{
    return first.Dessert < second.Dessert;
}

class Nutrition
{
public:
    int VitaminC;
    int Potassium;
};

int main()
{
    map<Scrumptious, Nutrition> myMap;

    Scrumptious ap = {"A"}; // Braces notation!
    Nutrition apn = {7, 9};

    Scrumptious ic = {"I"};
    Nutrition icn = {2, 1};

    Scrumptious cc = {"C"};
    Nutrition ccn = {9, 2};

    Scrumptious ms = {"M"};
    Nutrition msn = {4, 5};

    myMap[ap] = apn;
    myMap[ic] = icn;
    myMap[cc] = ccn;
    myMap[ms] = msn;

    map<Scrumptious,Nutrition> Duplicate = myMap;
    map<Scrumptious,Nutrition> AnotherDuplicate(myMap);

    myMap[ap].Potassium = 20;
    cout << myMap[ap].Potassium << endl;
    cout << Duplicate[ap].Potassium << endl;
    cout << AnotherDuplicate[ap].Potassium << endl;

    return 0;
}

Result


Related Tutorials