set
- Each value must be unique
- Elements are always const
- Can be inserted or removed
- Sorted by a specific order indicated by comparison object
- Implemented as binary search trees
Initialization
#include <iostream>
#include <set>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
int array[] = {1, 2, 3, 4, 4};//contains repeat elements
std::set<int> s(array, array+5);//1 2 3 4
std::cout<<"Size: "<<s.size()<<std::endl;
std::cout<<"Max_size: "<<s.max_size()<<std::endl;
display(s.begin(), s.end());
return 0;
}
Operations
#include <iostream>
#include <set>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::set<int> s = {1, 2, 3, 4};
//insert
s.insert(5);//1 2 3 4 5
auto it = s.begin();
std::advance(it, 1);
s.insert(it, 10);//max efficiency inserting, 1 2 3 4 5 10
//erase
s.erase(5);//1 2 3 4 10
//find
auto fit = s.find(10);
std::cout<<*fit<<std::endl;
//count
s.insert(10);//not insert a repeat element
std::cout<<s.count(10)<<std::endl;//1
//lower_bound and upper_bound
auto l = s.lower_bound(2);
auto u = s.upper_bound(8);
std::cout<<*l<<std::endl;
std::cout<<*u<<std::endl;
display(s.begin(), s.end());
return 0;
}
Set 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 <set>
#include <vector>
#include <typeinfo>
#include <algorithm>
#include "Rectangle.h"
void display(const std::set<Rectangle> &c)
{
for(auto it = c.begin(); it != c.end(); ++it)
{
std::cout<<it->getArea()<<std::endl;
}
}
int main(int argc, char *argv[])
{
std::set<Rectangle> s;
//insert
Rectangle r1(1, 2);
Rectangle r2(2, 3);
s.insert(r1);
s.insert(r2);
display(s);
//find
Rectangle r3(1, 2);
auto it = s.find(r3);
std::cout<<it->getArea()<<std::endl;
return 0;
}
- Insert objects in set, overload operator <
- sort elements by operator <
- find element by checking both a < b and b < a are false
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
//constructor
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);
};
#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 <set>
#include <vector>
#include <typeinfo>
#include <algorithm>
#include "Rectangle.h"
struct comp{
bool operator()(const Rectangle & a, const Rectangle &b) const
{
return (a.getArea() < b.getArea());
}
};
void display(const std::set<Rectangle, comp> &c)
{
for(auto it = c.begin(); it != c.end(); ++it)
{
std::cout<<it->getArea()<<std::endl;
}
}
int main(int argc, char *argv[])
{
std::set<Rectangle, comp> s;
Rectangle r1(1, 2);
Rectangle r2(2, 3);
s.insert(r1);
s.insert(r2);
display(s);
Rectangle r3(1, 2);
auto it = s.find(r3);
std::cout<<it->getArea()<<std::endl;
return 0;
}
- Insert objects in set, use compare struct operator()
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
//constructor
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);
};
#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 <set>
#include <vector>
#include <typeinfo>
#include <algorithm>
#include "Rectangle.h"
bool cmp(const Rectangle &a, const Rectangle &b)
{
return (a.getArea() < b.getArea());
}
void display(const std::set<Rectangle, bool(*)(const Rectangle &, const Rectangle &)> c)
{
for(auto it = c.begin(); it != c.end(); ++it)
{
std::cout<<it->getArea()<<std::endl;
}
}
int main(int argc, char *argv[])
{
std::set<Rectangle, bool(*)(const Rectangle &, const Rectangle &)> s(cmp);
Rectangle r1(1, 2);
Rectangle r2(2, 3);
s.insert(r1);
s.insert(r2);
display(s);
Rectangle r3(1, 2);
auto it = s.find(r3);
std::cout<<it->getArea()<<std::endl;
return 0;
}
- Insert objects in set, use comparison function
//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 <set>
#include <vector>
#include <typeinfo>
#include <algorithm>
#include "Rectangle.h"
auto comp = [](const Rectangle &a, const Rectangle &b) {return (a<b);};
void display(const std::set<Rectangle, decltype(comp)> &c)
{
for(auto it = c.begin(); it != c.end(); ++it)
{
std::cout<<it->getArea()<<std::endl;
}
}
int main(int argc, char *argv[])
{
std::set<Rectangle, decltype(comp)> s(comp);
Rectangle r1(1, 2);
Rectangle r2(2, 3);
s.insert(r1);
s.insert(r2);
display(s);
Rectangle r3(1, 2);
auto it = s.find(r3);
std::cout<<it->getArea()<<std::endl;
return 0;
}
- Insert objects in set, use lambda
Reference