Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
VecHotel3
//g++ 7.4.0 ////////////////////////////////////////////////////////////////////////////// //VecHotel3: example of vector as class attribute and use of priority queue //this code is created by Rezaul Hoque on August 17,2022; //contact:jewelmrh@yahoo.com;Dhaka,Bangladesh;https://rezaulhoque.wordpress.com,https://hoquestake.blogspot.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; ////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; class Hotel{ std::string star; vector<int> rent; public: Hotel(std::string s,vector<int> r); std::string getS() const; vector<int> getR() const; }; class ComS{ public: bool operator()(const Hotel & h1,const Hotel& h2) { return h1.getS()<h2.getS(); } }; class ComR{ public: bool operator()(const Hotel & h1,const Hotel& h2) { return h1.getR()<h2.getR(); } }; Hotel::Hotel(std::string s,vector<int> r) { this->star=s; this->rent=r; /*** //no need priority_queue<Hotel,vector<Hotel>,ComS> QS; priority_queue<Hotel,vector<Hotel>,ComR> QR; ***/ } std::string Hotel::getS() const { return this->star; } vector<int> Hotel::getR() const { return this->rent; } int main() { vector<int> r1,r2,r3,r4; r1.push_back(900); r1.push_back(800); r2.push_back(300); r2.push_back(400); r3.push_back(2500); r3.push_back(3000); r4.push_back(1000); r4.push_back(1500); Hotel a("***",r1); Hotel b("****",r2); Hotel c("****",r3); Hotel d("**",r4); /**" priority_queue<Hotel, vector<Hotel>,ComS> QS; QS.push(a); QS.push(b); QS.push(c); QS.push(d); std::cout<<"Hotels listed on stars:\nStars Rent\n"; while (!QS.empty()){ Hotel h=QS.top(); QS.pop(); std::cout<<h.getS()<<" "<<h.getR()<<" \n"; } std::cout<<"\n"; ***/ std::cout<<"Hotels listed by room rent:\nStars Rent\n"; priority_queue<Hotel, vector<Hotel>,ComR> QR; QR.push(a); QR.push(b); QR.push(c); QR.push(d); while (!QR.empty()){ Hotel h=QR.top(); QR.pop(); std::cout<<h.getS()<<" "; for(int k=0;k<2;k++){ std::cout<<" "<<h.getR()[k]<<"\n"; } std::cout<<"\n"; } return 0; }
run
|
edit
|
history
|
help
0
Policy based smart pointer
Cours C++
ListCPP
Dar
NameTempSpecial
Callback
decode
https://codeforces.com/contest/449/problem/D (INCLuSION EXCLUSION principle)
srednie
const_cast