SlideShare a Scribd company logo
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

DOCX
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
PPTX
Object Oriented Design and Programming Unit-05
PDF
Sppu University|BE|Computer Engineering|OOPs|unit 6 notes|ppt
PDF
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
PDF
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
PPT
Standard Template Library (STL) in Object Oriented Programming
PDF
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf
DOCX
Write a program to find the number of comparisons using the binary se.docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Object Oriented Design and Programming Unit-05
Sppu University|BE|Computer Engineering|OOPs|unit 6 notes|ppt
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
Standard Template Library (STL) in Object Oriented Programming
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf
Write a program to find the number of comparisons using the binary se.docx

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

PPTX
Advanced oops using c and c++.Pptx in Srm
PDF
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
PDF
1- The design of a singly-linked list below is a picture of the functi (1).pdf
PDF
Data Structure.pdf
PDF
Given below is the code for the question. Since the test files (ment.pdf
PPTX
Standard Template Library
PDF
Please code in C++ and do only the �TO DO�s and all of them. There a.pdf
PPTX
Standard template library
PPT
standard template library(STL) in C++
PPTX
14. containers, vector, list
PPTX
11. template
PDF
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
PDF
An Introduction to Part of C++ STL
DOCX
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
PDF
include ltfunctionalgt include ltiteratorgt inclu.pdf
DOCX
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
PDF
maincpp include ListItemh include ltstringgt in.pdf
PDF
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
PPT
Savitch Ch 18
ODP
Talk on Standard Template Library
Advanced oops using c and c++.Pptx in Srm
Implement the sequence class from Section 3.2 of the textbook. The d.pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
Data Structure.pdf
Given below is the code for the question. Since the test files (ment.pdf
Standard Template Library
Please code in C++ and do only the �TO DO�s and all of them. There a.pdf
Standard template library
standard template library(STL) in C++
14. containers, vector, list
11. template
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
An Introduction to Part of C++ STL
COS30008 Semester 1, 2016 Dr. Markus Lumpe 1 Swinbu.docx
include ltfunctionalgt include ltiteratorgt inclu.pdf
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
maincpp include ListItemh include ltstringgt in.pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
Savitch Ch 18
Talk on Standard Template Library

More from info673628 (20)

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

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Institutional Correction lecture only . . .
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Types and Its function , kingdom of life
Anesthesia in Laparoscopic Surgery in India
O7-L3 Supply Chain Operations - ICLT Program
Institutional Correction lecture only . . .
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Microbial diseases, their pathogenesis and prophylaxis
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
A systematic review of self-coping strategies used by university students to ...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Computing-Curriculum for Schools in Ghana
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Classroom Observation Tools for Teachers
Abdominal Access Techniques with Prof. Dr. R K Mishra
102 student loan defaulters named and shamed – Is someone you know on the list?
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

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"<