#include <iostream> #include <unordered_set> int main(int argc, char *argv[]) { std::unordered_multiset<int> s = {1, 2, 3, 4, 4}; for(int i = 0; i < s.bucket_count(); i++) { std::cout<<"Bucket "<<i<<": "; for(auto it = s.begin(i); it != s.end(i); ++it) std::cout<<*it<<" "; std::cout<<std::endl; } return 0; }
#include <iostream> #include <unordered_set> int main(int argc, char *argv[]) { std::unordered_multiset<int> s = {1, 2, 3, 4, 4}; std::cout<<"Size: "<<s.size()<<std::endl;//5 s.insert(3);// 1 2 3 3 4 4 auto it = s.find(3); s.erase(it);// 1 2 3 4 4 s.erase(4);//1 2 3 for(int i = 0; i < s.bucket_count(); i++) { std::cout<<"Bucket "<<i<<": "; for(auto it = s.begin(i); it != s.end(i); ++it) std::cout<<*it<<" "; std::cout<<std::endl; } return 0; }
//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); }; #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 <functional> #include <unordered_set> #include "Rectangle.h" struct comp{ bool operator()(const Rectangle &a, const Rectangle &b) const { return (a.getArea() == b.getArea()); } }; struct RectangleHash { size_t operator()(const Rectangle &r) const { std::hash<double> h; return (h(r.getWidth()) ^ h(r.getLength())); } }; template <class T> void display(T it, T end) { while(it != end) { std::cout<<it->getArea()<<" "; std::advance(it, 1); } std::cout<<std::endl; } int main(int argc, char *argv[]) { std::unordered_multiset<Rectangle, RectangleHash, comp> s; s.insert(Rectangle(1, 2)); s.insert(Rectangle(2, 3)); s.insert(Rectangle(3, 4)); s.insert(Rectangle(4, 5)); s.insert(Rectangle(1, 2)); std::cout<<"Size: "<<s.size()<<std::endl; for(int i = 0; i < s.bucket_count(); i++) { std::cout<<"Bucket "<<i<<": "; for(auto it = s.begin(i); it != s.end(i); ++it) std::cout<<it->getArea()<<" "; std::cout<<std::endl; } display(s.begin(), s.end()); return 0; }