SlideShare a Scribd company logo
1 of 2
Download to read offline
Write a stack class that has an STL vector as a data member. Your class should implement push,
peek, and pop.
Solution
template
class Stack {
public:
Stack(const std::initializer_list& i):elem(i) {}
int size() const { return elem.size(); }
bool empty() const { return elem.empty();}
void push(const T&);
T pop();
T peek();
private:
std::vector elem;
};
template
void Stack::push(const T& t) {
elem.push_back(t);
}
template
T Stack::pop() {
if (empty()) {
throw std::out_of_range("underflow");
}
auto x = elem.back();
elem.pop_back();
return x;
}
template
T Stack::peek() {
if (empty()) {
throw std::out_of_range("underflow");
}
return elem.back();
}

More Related Content

More from RITU1ARORA

The hardenability curves of several iron alloys are shown in the fig.pdf
The hardenability curves of several iron alloys are shown in the fig.pdfThe hardenability curves of several iron alloys are shown in the fig.pdf
The hardenability curves of several iron alloys are shown in the fig.pdf
RITU1ARORA
 
An environment would be considered harsh to an organism if it would.pdf
An environment would be considered harsh to an organism if  it would.pdfAn environment would be considered harsh to an organism if  it would.pdf
An environment would be considered harsh to an organism if it would.pdf
RITU1ARORA
 
5) What is the dependent variable measured in the BOD test (Explain .pdf
5) What is the dependent variable measured in the BOD test (Explain .pdf5) What is the dependent variable measured in the BOD test (Explain .pdf
5) What is the dependent variable measured in the BOD test (Explain .pdf
RITU1ARORA
 
RAREST MethodWrite a method rarest that accepts a map whose keys a.pdf
RAREST MethodWrite a method rarest that accepts a map whose keys a.pdfRAREST MethodWrite a method rarest that accepts a map whose keys a.pdf
RAREST MethodWrite a method rarest that accepts a map whose keys a.pdf
RITU1ARORA
 
1. Martin would say that in a society with ample protection for whis.pdf
1. Martin would say that in a society with ample protection for whis.pdf1. Martin would say that in a society with ample protection for whis.pdf
1. Martin would say that in a society with ample protection for whis.pdf
RITU1ARORA
 
MULTIPLE CHOICE CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
MULTIPLE CHOICE  CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdfMULTIPLE CHOICE  CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
MULTIPLE CHOICE CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
RITU1ARORA
 

More from RITU1ARORA (20)

Which statement correctly saves the number of items in the variable .pdf
Which statement correctly saves the number of items in the variable .pdfWhich statement correctly saves the number of items in the variable .pdf
Which statement correctly saves the number of items in the variable .pdf
 
What does the fossil record of Basilosaurid whales tell us about wha.pdf
What does the fossil record of Basilosaurid whales tell us about wha.pdfWhat does the fossil record of Basilosaurid whales tell us about wha.pdf
What does the fossil record of Basilosaurid whales tell us about wha.pdf
 
Three decision makers have assessed utilities for the following deci.pdf
Three decision makers have assessed utilities for the following deci.pdfThree decision makers have assessed utilities for the following deci.pdf
Three decision makers have assessed utilities for the following deci.pdf
 
Think about our economy. Irrespective of economic conditions, we con.pdf
Think about our economy. Irrespective of economic conditions, we con.pdfThink about our economy. Irrespective of economic conditions, we con.pdf
Think about our economy. Irrespective of economic conditions, we con.pdf
 
The hardenability curves of several iron alloys are shown in the fig.pdf
The hardenability curves of several iron alloys are shown in the fig.pdfThe hardenability curves of several iron alloys are shown in the fig.pdf
The hardenability curves of several iron alloys are shown in the fig.pdf
 
The de Broglie relationships are both valid for bloch electrons. Tur.pdf
The de Broglie relationships are both valid for bloch electrons. Tur.pdfThe de Broglie relationships are both valid for bloch electrons. Tur.pdf
The de Broglie relationships are both valid for bloch electrons. Tur.pdf
 
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdfTextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
TextBook Error Control Code (Lin and Costello) If the syndrome is z.pdf
 
Based on taxonomical classification, to which of the following organi.pdf
Based on taxonomical classification, to which of the following organi.pdfBased on taxonomical classification, to which of the following organi.pdf
Based on taxonomical classification, to which of the following organi.pdf
 
Answer choices are Internalized Principle, Societal Expectations, S.pdf
Answer choices are Internalized Principle, Societal Expectations, S.pdfAnswer choices are Internalized Principle, Societal Expectations, S.pdf
Answer choices are Internalized Principle, Societal Expectations, S.pdf
 
An environment would be considered harsh to an organism if it would.pdf
An environment would be considered harsh to an organism if  it would.pdfAn environment would be considered harsh to an organism if  it would.pdf
An environment would be considered harsh to an organism if it would.pdf
 
A committee of three people is to be randomly selected from a group .pdf
A committee of three people is to be randomly selected from a group .pdfA committee of three people is to be randomly selected from a group .pdf
A committee of three people is to be randomly selected from a group .pdf
 
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdfA man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
A man with Klinefelter syndrome (XXY) is found to be colour-blind (a.pdf
 
5) What is the dependent variable measured in the BOD test (Explain .pdf
5) What is the dependent variable measured in the BOD test (Explain .pdf5) What is the dependent variable measured in the BOD test (Explain .pdf
5) What is the dependent variable measured in the BOD test (Explain .pdf
 
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
3. Discuss the 10 most Important Website Attributes.SolutionMo.pdf
 
RAREST MethodWrite a method rarest that accepts a map whose keys a.pdf
RAREST MethodWrite a method rarest that accepts a map whose keys a.pdfRAREST MethodWrite a method rarest that accepts a map whose keys a.pdf
RAREST MethodWrite a method rarest that accepts a map whose keys a.pdf
 
1. Which of the following is a characteristic of specific to corpora.pdf
1. Which of the following is a characteristic of specific to corpora.pdf1. Which of the following is a characteristic of specific to corpora.pdf
1. Which of the following is a characteristic of specific to corpora.pdf
 
Nosocomial infectionsare acquired at various social events.are a.pdf
Nosocomial infectionsare acquired at various social events.are a.pdfNosocomial infectionsare acquired at various social events.are a.pdf
Nosocomial infectionsare acquired at various social events.are a.pdf
 
Mitochondria were most likely acquire earlier than plastids in the e.pdf
Mitochondria were most likely acquire earlier than plastids in the e.pdfMitochondria were most likely acquire earlier than plastids in the e.pdf
Mitochondria were most likely acquire earlier than plastids in the e.pdf
 
1. Martin would say that in a society with ample protection for whis.pdf
1. Martin would say that in a society with ample protection for whis.pdf1. Martin would say that in a society with ample protection for whis.pdf
1. Martin would say that in a society with ample protection for whis.pdf
 
MULTIPLE CHOICE CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
MULTIPLE CHOICE  CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdfMULTIPLE CHOICE  CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
MULTIPLE CHOICE CHOOSE RIGHT ANSWER. I GOT MOSTLY WRONG ANSWERS PL.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

Write a stack class that has an STL vector as a data member. Your cl.pdf

  • 1. Write a stack class that has an STL vector as a data member. Your class should implement push, peek, and pop. Solution template class Stack { public: Stack(const std::initializer_list& i):elem(i) {} int size() const { return elem.size(); } bool empty() const { return elem.empty();} void push(const T&); T pop(); T peek(); private: std::vector elem; }; template void Stack::push(const T& t) { elem.push_back(t); } template T Stack::pop() { if (empty()) { throw std::out_of_range("underflow"); } auto x = elem.back(); elem.pop_back(); return x; } template T Stack::peek() { if (empty()) { throw std::out_of_range("underflow"); }