C++ map copying constructor

Description

C++ map copying constructor

#include <iostream>
#include <map>

using namespace std;

class Shape//from   ww w. j a v a  2  s .  c  om
{
public:
    string name;
};

bool operator < (const Shape & first, const Shape & second)
{
    return first.name < second.name;
}

class Rectangle
{
public:
    int height;
    int width;
};

int main()
{
    map<Shape, Rectangle> myMap;

    Shape ap = {"A"}; // Braces notation!
    Rectangle apn = {7, 9};

    Shape ic = {"I"};
    Rectangle icn = {2, 1};

    Shape cc = {"C"};
    Rectangle ccn = {9, 2};

    Shape ms = {"M"};
    Rectangle msn = {4, 5};

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

    map<Shape,Rectangle> Duplicate = myMap;
    map<Shape,Rectangle> AnotherDuplicate(myMap);

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

    return 0;
}



PreviousNext

Related