#include <iostream> #include <list> #include <vector> #include <stack> int main(int argc, char *argv[]) { std::vector<int> v = {1, 2, 3, 4}; std::stack<int, std::vector<int>> s(v); v.clear();//remove vector //size std::cout<<"Size: "<<s.size()<<std::endl; //push s.push(5); std::cout<<"Top: "<<s.top()<<std::endl; //pop s.pop(); std::cout<<"Top: "<<s.top()<<std::endl; return 0; }
#include <iostream> #include <list> #include <stack> int main(int argc, char *argv[]) { std::list<int> v = {1, 2, 3, 4}; std::stack<int, std::list<int>> s(v); v.clear();//remove vector std::cout<<"Top: "<<s.top()<<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 <list> #include <stack> #include "Rectangle.h" int main(int argc, char *argv[]) { std::list<Rectangle> l; //insert elements auto it = l.begin(); l.insert(it, Rectangle(1, 2)); ++it; l.insert(it, Rectangle(2, 3)); ++it; l.insert(it, Rectangle(3, 4)); //list to stack std::stack<Rectangle, std::list<Rectangle>> s(l);//12 std::cout<<s.top().getArea()<<std::endl; return 0; }