#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::multiset<int> s = {1, 2, 2, 3, 4};//containing repeat elements std::multiset<int> s2(s); std::multiset<int> s3(s.begin(), s.end()); int array[] = {1, 1, 2, 2}; std::multiset<int> s4(array, array+4); display(s4.begin(), s4.end()); return 0; }
//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::multiset<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::multiset<Rectangle, comp> s; Rectangle r1(1, 2); Rectangle r2(2, 3); Rectangle r3(1, 2); s.insert(r1); s.insert(r2); s.insert(r3); display(s); Rectangle r4(1, 2); auto it = s.find(r4); std::cout<<it->getArea()<<std::endl; return 0; }