SlideShare a Scribd company logo
1 of 7
Download to read offline
Lab 13: Practicing STL
Vector Container:
1a. Write a template function(in header file) to search a value stored in a vector and return the
position(index) of the item if found; otherwise, return a negative value. Then write a driver to
test it. The driver should at least test two different type of vector. Use vector index to do it.
1b. Write a template function(in header file) to search a value stored in a vector and return the
position(index) of the item if found; otherwise, return a negative value. Then write a driver to
test it. The driver should at least test two different type of vector. Use iterator to do it.
Deque Container:
2a. Write a function to add first 6 integers alternately to front and back of a deque. (in header
file)
2b. Write a template function to display the content of a deque using iterator (in header file)
2c. Write a template function to change back value to a specified value using iterator (in header
file)
Then write a driver to test the above three template functions.
List Container – see textbook page 605 for basic operations:
3a. Write a template function to read values from an input stream to a list (in header file)
3b. Write a template function to fill in a list with a given vector (in header file)
3c. Write a template function to change a given value in a list to another given value(use find(),
insert() and erase()) (in header file)
Then write a driver to test the above three template functions.
Queue wrapper with list
4. Write a template queue class as defined below:
private data member: a STL list
public member functions:
-empty
-size
-enqueue
-deque
-front
-back
Then write a driver to test the above queue class.
Follow our class coding standard to complete this lab, compile and run it, check out for credit
Solution
Here is the answer for the questions:
Please compile using -std=c++11 option
example g++ -std=c++11 q3.cpp
Q1 files
compile : g++ -std=c++11 q1.cpp
q1.h
#include
using std::vector;
template
int searchUsingIndex(vector values, T item)
{
for(int i = 0 ; i < values.size(); ++i)
{
if(values[i] == item)
return i;
}
return -1;
}
template
int searchUsingIterator(vector values, T item)
{
int i = 0;
for(typename vector::iterator it=values.begin(); it != values.end(); ++it, ++i)
{
if(*it == item)
return i;
}
return -1;
}
q1.cpp
#include
#include
#include "q1.h"
using namespace std;
int main()
{
vector num = {3,6,10,1};
vector names = {"alice","bob","john" ,"peter"};
string search="john";
cout<<"Index of 2 in num vector using index: "<
#include
using namespace std;
//we need to pass by reference
void addFrontBack(deque &q)
{
for(int i = 1; i < 7; i ++)
{
if(i % 2 == 1)
q.push_front(i);
else
q.push_back(i);
}
}
template
void print(const deque &q)
{
for(typename deque::const_iterator it = q.begin(); it != q.end(); ++it)
{
cout<< *it <
void changeBack(deque &q, T newValue)
{
if(q.empty())
return;
typename deque::iterator it = q.end()-1;//get 1 location behind end
*(it) = newValue;
}
q2.cpp
#include "q2.h"
#include
#include
using namespace std;
int main()
{
deque q;
int n;
addFrontBack(q);
cout<<"The deque contents are "<> n;
changeBack(q, n);
cout<<"The deque contents after changing back value "<
#include
using namespace std;
template
void read_file(ifstream &file, list &l )
{
T value;
while(file >> value)
l.push_back(value);
}
template
void fill_from_vector(list &l, vector v)
{
l.assign(v.begin(), v.end());
}
template
void change_value(list &l, T search, T replacement)
{
for(typename list::iterator it = l.begin(); it != l.end(); ++it)
{
if(*it == search)
{
l.insert(it, replacement);
l.erase(it); //it will have move forward and now pointing to old value
return;
}
}
}
template
void print(const list &l)
{
for(typename list::const_iterator it = l.begin(); it != l.end(); ++it)
{
cout<< *it <
#include
#include
#include
#include "q3.h"
using namespace std;
int main()
{
ifstream infile("names.txt");
vector num_vec = {1,2,3,4,5};
list names;
list num_list;
fill_from_vector(num_list,num_vec);
if(infile.fail())
{
cout<<"Could not find file names.txt";
}
else
read_file(infile, names);
cout<<"names list [loaded from file]"<>searchNum;
cout<<"Enter the replacement: ";
cin>>replaceNum;
change_value(num_list, searchNum, replaceNum);
string searchName,replaceName;
cout<<"Enter a name to change: ";
cin>>searchName;
cout<<"Enter the replacement: ";
cin>>replaceName;
change_value(names, searchName, replaceName);
cout<<"After changes , the 2 lists are "<
using namespace std;
template
class queue
{
private:
list elements;
public:
bool empty()
{
return elements.empty();
}
int size()
{
return elements.size();
}
void enqueue(T val)
{
elements.push_back(val);
}
T deque()
{
T val = elements.front();
elements.pop_front();
return val;
}
T front()
{
return elements.front();
}
T back()
{
return elements.back();
}
};
q4.cpp
#include "q4.h"
#include
using namespace std;
int main()
{
queue q;
cout<<"enqueuing 1-10"<

More Related Content

Similar to Lab 13 Practicing STLVector Container1a. Write a template func.pdf

Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxdunhamadell
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptOliverKane3
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxChandrakantDivate1
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxmonicafrancis71118
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxcargillfilberto
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxdrandy1
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 

Similar to Lab 13 Practicing STLVector Container1a. Write a template func.pdf (20)

Functional programming in C++
Functional programming in C++Functional programming in C++
Functional programming in C++
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Array Cont
Array ContArray Cont
Array Cont
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Functions1
Functions1Functions1
Functions1
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
Functions
FunctionsFunctions
Functions
 

More from info673628

Firms HL and LL are identical except for their leverage ratios and t.pdf
Firms HL and LL are identical except for their leverage ratios and t.pdfFirms HL and LL are identical except for their leverage ratios and t.pdf
Firms HL and LL are identical except for their leverage ratios and t.pdfinfo673628
 
Find the domain of the composite function fog. 1 The domain contains .pdf
Find the domain of the composite function fog. 1 The domain contains .pdfFind the domain of the composite function fog. 1 The domain contains .pdf
Find the domain of the composite function fog. 1 The domain contains .pdfinfo673628
 
Do the following two problems1. Any algorithm that solves the sea.pdf
Do the following two problems1. Any algorithm that solves the sea.pdfDo the following two problems1. Any algorithm that solves the sea.pdf
Do the following two problems1. Any algorithm that solves the sea.pdfinfo673628
 
Describe two traits that represent a sustainable societyand two tr.pdf
Describe two traits that represent a sustainable societyand two tr.pdfDescribe two traits that represent a sustainable societyand two tr.pdf
Describe two traits that represent a sustainable societyand two tr.pdfinfo673628
 
Compare Windows Assembly to that of a UNIX system.SolutionTher.pdf
Compare Windows Assembly to that of a UNIX system.SolutionTher.pdfCompare Windows Assembly to that of a UNIX system.SolutionTher.pdf
Compare Windows Assembly to that of a UNIX system.SolutionTher.pdfinfo673628
 
Below is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfBelow is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfinfo673628
 
You have prepared the spread plates below using 0.1 mL from a liquid .pdf
You have prepared the spread plates below using 0.1 mL from a liquid .pdfYou have prepared the spread plates below using 0.1 mL from a liquid .pdf
You have prepared the spread plates below using 0.1 mL from a liquid .pdfinfo673628
 
While bacteria exhibit simple cell division where the quantity o.pdf
While bacteria exhibit simple cell division where the quantity o.pdfWhile bacteria exhibit simple cell division where the quantity o.pdf
While bacteria exhibit simple cell division where the quantity o.pdfinfo673628
 
Which of the following items appears on the income statement before i.pdf
Which of the following items appears on the income statement before i.pdfWhich of the following items appears on the income statement before i.pdf
Which of the following items appears on the income statement before i.pdfinfo673628
 
What part of the root is responsible for water and nutrient absorptio.pdf
What part of the root is responsible for water and nutrient absorptio.pdfWhat part of the root is responsible for water and nutrient absorptio.pdf
What part of the root is responsible for water and nutrient absorptio.pdfinfo673628
 
What is the purpose of the balance sheet Be sure to discuss the thr.pdf
What is the purpose of the balance sheet Be sure to discuss the thr.pdfWhat is the purpose of the balance sheet Be sure to discuss the thr.pdf
What is the purpose of the balance sheet Be sure to discuss the thr.pdfinfo673628
 
What is culture Why should culture play a role in aiding our unders.pdf
What is culture Why should culture play a role in aiding our unders.pdfWhat is culture Why should culture play a role in aiding our unders.pdf
What is culture Why should culture play a role in aiding our unders.pdfinfo673628
 
What are the Federal Reserves major assets and liabilities What a.pdf
What are the Federal Reserves major assets and liabilities What a.pdfWhat are the Federal Reserves major assets and liabilities What a.pdf
What are the Federal Reserves major assets and liabilities What a.pdfinfo673628
 
Use whichever properties (associative andor communities) are nece.pdf
Use whichever properties (associative andor communities) are nece.pdfUse whichever properties (associative andor communities) are nece.pdf
Use whichever properties (associative andor communities) are nece.pdfinfo673628
 
Use a 5 significance level unless specified otherwise.Please give.pdf
Use a 5 significance level unless specified otherwise.Please give.pdfUse a 5 significance level unless specified otherwise.Please give.pdf
Use a 5 significance level unless specified otherwise.Please give.pdfinfo673628
 
two children own two-way radios that have a maximum range of 2 miles.pdf
two children own two-way radios that have a maximum range of 2 miles.pdftwo children own two-way radios that have a maximum range of 2 miles.pdf
two children own two-way radios that have a maximum range of 2 miles.pdfinfo673628
 
The place within an enzyme where a substrate binds is called the ATP.pdf
The place within an enzyme where a substrate binds is called the  ATP.pdfThe place within an enzyme where a substrate binds is called the  ATP.pdf
The place within an enzyme where a substrate binds is called the ATP.pdfinfo673628
 
READ BEFORE YOU START Please read the given Word document fo.pdf
READ BEFORE YOU START  Please read the given Word document fo.pdfREAD BEFORE YOU START  Please read the given Word document fo.pdf
READ BEFORE YOU START Please read the given Word document fo.pdfinfo673628
 
Refer to the table below. A student creates the table above as a stu.pdf
Refer to the table below.  A student creates the table above as a stu.pdfRefer to the table below.  A student creates the table above as a stu.pdf
Refer to the table below. A student creates the table above as a stu.pdfinfo673628
 
prove the followings1- every subset of a finite set is finite2-.pdf
prove the followings1- every subset of a finite set is finite2-.pdfprove the followings1- every subset of a finite set is finite2-.pdf
prove the followings1- every subset of a finite set is finite2-.pdfinfo673628
 

More from info673628 (20)

Firms HL and LL are identical except for their leverage ratios and t.pdf
Firms HL and LL are identical except for their leverage ratios and t.pdfFirms HL and LL are identical except for their leverage ratios and t.pdf
Firms HL and LL are identical except for their leverage ratios and t.pdf
 
Find the domain of the composite function fog. 1 The domain contains .pdf
Find the domain of the composite function fog. 1 The domain contains .pdfFind the domain of the composite function fog. 1 The domain contains .pdf
Find the domain of the composite function fog. 1 The domain contains .pdf
 
Do the following two problems1. Any algorithm that solves the sea.pdf
Do the following two problems1. Any algorithm that solves the sea.pdfDo the following two problems1. Any algorithm that solves the sea.pdf
Do the following two problems1. Any algorithm that solves the sea.pdf
 
Describe two traits that represent a sustainable societyand two tr.pdf
Describe two traits that represent a sustainable societyand two tr.pdfDescribe two traits that represent a sustainable societyand two tr.pdf
Describe two traits that represent a sustainable societyand two tr.pdf
 
Compare Windows Assembly to that of a UNIX system.SolutionTher.pdf
Compare Windows Assembly to that of a UNIX system.SolutionTher.pdfCompare Windows Assembly to that of a UNIX system.SolutionTher.pdf
Compare Windows Assembly to that of a UNIX system.SolutionTher.pdf
 
Below is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfBelow is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdf
 
You have prepared the spread plates below using 0.1 mL from a liquid .pdf
You have prepared the spread plates below using 0.1 mL from a liquid .pdfYou have prepared the spread plates below using 0.1 mL from a liquid .pdf
You have prepared the spread plates below using 0.1 mL from a liquid .pdf
 
While bacteria exhibit simple cell division where the quantity o.pdf
While bacteria exhibit simple cell division where the quantity o.pdfWhile bacteria exhibit simple cell division where the quantity o.pdf
While bacteria exhibit simple cell division where the quantity o.pdf
 
Which of the following items appears on the income statement before i.pdf
Which of the following items appears on the income statement before i.pdfWhich of the following items appears on the income statement before i.pdf
Which of the following items appears on the income statement before i.pdf
 
What part of the root is responsible for water and nutrient absorptio.pdf
What part of the root is responsible for water and nutrient absorptio.pdfWhat part of the root is responsible for water and nutrient absorptio.pdf
What part of the root is responsible for water and nutrient absorptio.pdf
 
What is the purpose of the balance sheet Be sure to discuss the thr.pdf
What is the purpose of the balance sheet Be sure to discuss the thr.pdfWhat is the purpose of the balance sheet Be sure to discuss the thr.pdf
What is the purpose of the balance sheet Be sure to discuss the thr.pdf
 
What is culture Why should culture play a role in aiding our unders.pdf
What is culture Why should culture play a role in aiding our unders.pdfWhat is culture Why should culture play a role in aiding our unders.pdf
What is culture Why should culture play a role in aiding our unders.pdf
 
What are the Federal Reserves major assets and liabilities What a.pdf
What are the Federal Reserves major assets and liabilities What a.pdfWhat are the Federal Reserves major assets and liabilities What a.pdf
What are the Federal Reserves major assets and liabilities What a.pdf
 
Use whichever properties (associative andor communities) are nece.pdf
Use whichever properties (associative andor communities) are nece.pdfUse whichever properties (associative andor communities) are nece.pdf
Use whichever properties (associative andor communities) are nece.pdf
 
Use a 5 significance level unless specified otherwise.Please give.pdf
Use a 5 significance level unless specified otherwise.Please give.pdfUse a 5 significance level unless specified otherwise.Please give.pdf
Use a 5 significance level unless specified otherwise.Please give.pdf
 
two children own two-way radios that have a maximum range of 2 miles.pdf
two children own two-way radios that have a maximum range of 2 miles.pdftwo children own two-way radios that have a maximum range of 2 miles.pdf
two children own two-way radios that have a maximum range of 2 miles.pdf
 
The place within an enzyme where a substrate binds is called the ATP.pdf
The place within an enzyme where a substrate binds is called the  ATP.pdfThe place within an enzyme where a substrate binds is called the  ATP.pdf
The place within an enzyme where a substrate binds is called the ATP.pdf
 
READ BEFORE YOU START Please read the given Word document fo.pdf
READ BEFORE YOU START  Please read the given Word document fo.pdfREAD BEFORE YOU START  Please read the given Word document fo.pdf
READ BEFORE YOU START Please read the given Word document fo.pdf
 
Refer to the table below. A student creates the table above as a stu.pdf
Refer to the table below.  A student creates the table above as a stu.pdfRefer to the table below.  A student creates the table above as a stu.pdf
Refer to the table below. A student creates the table above as a stu.pdf
 
prove the followings1- every subset of a finite set is finite2-.pdf
prove the followings1- every subset of a finite set is finite2-.pdfprove the followings1- every subset of a finite set is finite2-.pdf
prove the followings1- every subset of a finite set is finite2-.pdf
 

Recently uploaded

UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfJerry Chew
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfcupulin
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MysoreMuleSoftMeetup
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 

Recently uploaded (20)

UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 

Lab 13 Practicing STLVector Container1a. Write a template func.pdf

  • 1. Lab 13: Practicing STL Vector Container: 1a. Write a template function(in header file) to search a value stored in a vector and return the position(index) of the item if found; otherwise, return a negative value. Then write a driver to test it. The driver should at least test two different type of vector. Use vector index to do it. 1b. Write a template function(in header file) to search a value stored in a vector and return the position(index) of the item if found; otherwise, return a negative value. Then write a driver to test it. The driver should at least test two different type of vector. Use iterator to do it. Deque Container: 2a. Write a function to add first 6 integers alternately to front and back of a deque. (in header file) 2b. Write a template function to display the content of a deque using iterator (in header file) 2c. Write a template function to change back value to a specified value using iterator (in header file) Then write a driver to test the above three template functions. List Container – see textbook page 605 for basic operations: 3a. Write a template function to read values from an input stream to a list (in header file) 3b. Write a template function to fill in a list with a given vector (in header file) 3c. Write a template function to change a given value in a list to another given value(use find(), insert() and erase()) (in header file) Then write a driver to test the above three template functions. Queue wrapper with list 4. Write a template queue class as defined below: private data member: a STL list public member functions: -empty -size -enqueue -deque -front -back Then write a driver to test the above queue class. Follow our class coding standard to complete this lab, compile and run it, check out for credit Solution
  • 2. Here is the answer for the questions: Please compile using -std=c++11 option example g++ -std=c++11 q3.cpp Q1 files compile : g++ -std=c++11 q1.cpp q1.h #include using std::vector; template int searchUsingIndex(vector values, T item) { for(int i = 0 ; i < values.size(); ++i) { if(values[i] == item) return i; } return -1; } template int searchUsingIterator(vector values, T item) { int i = 0; for(typename vector::iterator it=values.begin(); it != values.end(); ++it, ++i) { if(*it == item) return i; } return -1; } q1.cpp #include #include #include "q1.h"
  • 3. using namespace std; int main() { vector num = {3,6,10,1}; vector names = {"alice","bob","john" ,"peter"}; string search="john"; cout<<"Index of 2 in num vector using index: "< #include using namespace std; //we need to pass by reference void addFrontBack(deque &q) { for(int i = 1; i < 7; i ++) { if(i % 2 == 1) q.push_front(i); else q.push_back(i); } } template void print(const deque &q) { for(typename deque::const_iterator it = q.begin(); it != q.end(); ++it) { cout<< *it < void changeBack(deque &q, T newValue) { if(q.empty()) return; typename deque::iterator it = q.end()-1;//get 1 location behind end *(it) = newValue; } q2.cpp
  • 4. #include "q2.h" #include #include using namespace std; int main() { deque q; int n; addFrontBack(q); cout<<"The deque contents are "<> n; changeBack(q, n); cout<<"The deque contents after changing back value "< #include using namespace std; template void read_file(ifstream &file, list &l ) { T value; while(file >> value) l.push_back(value); } template void fill_from_vector(list &l, vector v) { l.assign(v.begin(), v.end()); } template void change_value(list &l, T search, T replacement) { for(typename list::iterator it = l.begin(); it != l.end(); ++it) { if(*it == search) { l.insert(it, replacement); l.erase(it); //it will have move forward and now pointing to old value return;
  • 5. } } } template void print(const list &l) { for(typename list::const_iterator it = l.begin(); it != l.end(); ++it) { cout<< *it < #include #include #include #include "q3.h" using namespace std; int main() { ifstream infile("names.txt"); vector num_vec = {1,2,3,4,5}; list names; list num_list; fill_from_vector(num_list,num_vec); if(infile.fail()) { cout<<"Could not find file names.txt"; } else read_file(infile, names); cout<<"names list [loaded from file]"<>searchNum; cout<<"Enter the replacement: "; cin>>replaceNum; change_value(num_list, searchNum, replaceNum); string searchName,replaceName; cout<<"Enter a name to change: "; cin>>searchName; cout<<"Enter the replacement: ";
  • 6. cin>>replaceName; change_value(names, searchName, replaceName); cout<<"After changes , the 2 lists are "< using namespace std; template class queue { private: list elements; public: bool empty() { return elements.empty(); } int size() { return elements.size(); } void enqueue(T val) { elements.push_back(val); } T deque() { T val = elements.front(); elements.pop_front(); return val; } T front() { return elements.front(); } T back() { return elements.back(); }
  • 7. }; q4.cpp #include "q4.h" #include using namespace std; int main() { queue q; cout<<"enqueuing 1-10"<