map
- Each key is unique
- Elements are sorted by its key following a specific order indicated by its comparison object
- Implemented as a balanced binary search tree
Initialization
#include <iostream>
#include <map>
#include <string>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->first<<" "<<it->second<<std::endl;
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::map<std::string, int> m;
m["Buick"] = 1;
m["Acura"] = 2;
//insert
m.insert(std::make_pair("Lincoln", 3));
m.insert(std::make_pair("Honda", 4));
std::cout<<"Size: "<<m.size()<<std::endl;
std::cout<<"Max_size: "<<m.max_size()<<std::endl;
//erase
m.erase("Buick");//remove by key
auto it = m.find("Acura");//remove by iterator
m.erase(it);
display(m.begin(), m.end());
return 0;
}
Map Containing Objects
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
//constructor
Rectangle(){}
Rectangle(double w, double l):width(w), length(l){}
//accessor
double getWidth() const;
double getLength() const;
double getArea() const;
//mutator
void setWidth(double w);
void setLength(double l);
//operator
bool operator<(const Rectangle &right) const
{
if(getArea() < right.getArea())
return true;
else
return false;
}
};
#endif
//Rectangle.cpp
#include "Rectangle.h"
double Rectangle::getWidth() const
{
return width;
}
double Rectangle::getLength() const
{
return length;
}
double Rectangle::getArea() const
{
return width*length;
}
void Rectangle::setWidth(double w)
{
width = w;
}
void Rectangle::setLength(double l)
{
length = l;
}
//main.cpp
#include <iostream>
#include <map>
#include "Rectangle.h"
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->first.getArea()<<" "<<it->second<<std::endl;
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::map<Rectangle, int> m;
Rectangle r1(1, 2);
Rectangle r2(2, 3);
Rectangle r3(2, 4);
m[r1] = 1;
m[r2] = 2;
m[r3] = 3;
display(m.begin(), m.end());
return 0;
}
- Insert object in map, overload operator <
- Sorted by operator <
- find element by checking both a < b and b < a are false
#include <iostream>
#include <map>
#include "Rectangle.h"
struct comp{
bool operator()(const Rectangle &a, const Rectangle &b)
{
return (a.getArea() < b.getArea());
}
};
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->first.getArea()<<" "<<it->second<<std::endl;
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::map<Rectangle, int, comp> m;
Rectangle r1(1, 2);
Rectangle r2(2, 3);
Rectangle r3(2, 4);
m[r1] = 1;
m[r2] = 2;
m[r3] = 3;
display(m.begin(), m.end());
return 0;
}
- Insert object in map, use compare struct operator()
#include <iostream>
#include <map>
#include "Rectangle.h"
bool cmp(const Rectangle &a, const Rectangle &b)
{
return (a.getArea() < b.getArea());
}
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->first.getArea()<<" "<<it->second<<std::endl;
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::map<Rectangle, int, bool(*)(const Rectangle &a, const Rectangle &b)> m(cmp);
Rectangle r1(1, 2);
Rectangle r2(2, 3);
Rectangle r3(2, 4);
m[r1] = 1;
m[r2] = 2;
m[r3] = 3;
display(m.begin(), m.end());
return 0;
}
- Insert object in map, use comparision function
#include <iostream>
#include <map>
#include "Rectangle.h"
auto comp = [](const Rectangle &a, const Rectangle &b) {return (a.getArea()<b.getArea());};
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<it->first.getArea()<<" "<<it->second<<std::endl;
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::map<Rectangle, int, decltype(comp)> m(comp);
Rectangle r1(1, 2);
Rectangle r2(2, 3);
Rectangle r3(2, 4);
m[r1] = 1;
m[r2] = 2;
m[r3] = 3;
display(m.begin(), m.end());
return 0;
}
- Insert object in map, use lambda
Reference