Lab 13: Practicing STL Vector Container: 1a. Write a template function(in header
ID: 3934931 • Letter: L
Question
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
Explanation / Answer
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 <vector>
using std::vector;
template <typename T>
int searchUsingIndex(vector<T> values, T item)
{
for(int i = 0 ; i < values.size(); ++i)
{
if(values[i] == item)
return i;
}
return -1;
}
template <typename T>
int searchUsingIterator(vector<T> values, T item)
{
int i = 0;
for(typename vector<T>::iterator it=values.begin(); it != values.end(); ++it, ++i)
{
if(*it == item)
return i;
}
return -1;
}
q1.cpp
#include <iostream>
#include <vector>
#include "q1.h"
using namespace std;
int main()
{
vector<int> num = {3,6,10,1};
vector<string> names = {"alice","bob","john" ,"peter"};
string search="john";
cout<<"Index of 2 in num vector using index: "<<searchUsingIndex(num, 2)<<endl;
cout<<"Index of 2 in num vector using iterator: "<<searchUsingIterator(num, 2)<<endl;
cout<<"Index of 10 in num vector using index: "<<searchUsingIndex(num, 10)<<endl;
cout<<"Index of 10 in num vector using iterator: "<<searchUsingIterator(num, 10)<<endl;
cout<<"Index of "john" in name vector using index : "<<searchUsingIndex(names, search)<<endl;
cout<<"Index of "john" in name vector using iterator: "<<searchUsingIterator(names, search)<<endl;;
}
output
Index of 2 in num vector using index: -1
Index of 2 in num vector using iterator: -1
Index of 10 in num vector using index: 2
Index of 10 in num vector using iterator: 2
Index of "john" in name vector using index : 2
Index of "john" in name vector using iterator: 2
====================
q2 files
q2.h
#include <deque>
#include <iostream>
using namespace std;
//we need to pass by reference
void addFrontBack(deque<int> &q)
{
for(int i = 1; i < 7; i ++)
{
if(i % 2 == 1)
q.push_front(i);
else
q.push_back(i);
}
}
template <typename T>
void print(const deque<T> &q)
{
for(typename deque<T>::const_iterator it = q.begin(); it != q.end(); ++it)
{
cout<< *it <<endl;
}
cout<<endl;
}
//need to pass by reference
template<typename T>
void changeBack(deque<T> &q, T newValue)
{
if(q.empty())
return;
typename deque<T>::iterator it = q.end()-1;//get 1 location behind end
*(it) = newValue;
}
q2.cpp
#include "q2.h"
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> q;
int n;
addFrontBack(q);
cout<<"The deque contents are "<<endl;
print(q);
cout<<"Enter the new value for the back of deque: ";
cin >> n;
changeBack(q, n);
cout<<"The deque contents after changing back value "<<endl;
print(q);
}
outpu
The deque contents are
5
3
1
2
4
6
Enter the new value for the back of deque: 25
The deque contents after changing back value
5
3
1
2
4
25
q3 files
compile: g++ -std=c++11 q3.cpp
q3.h
#include <iostream>
#include <list>
using namespace std;
template <typename T>
void read_file(ifstream &file, list<T> &l )
{
T value;
while(file >> value)
l.push_back(value);
}
template <typename T>
void fill_from_vector(list<T> &l, vector<T> v)
{
l.assign(v.begin(), v.end());
}
template <typename T>
void change_value(list<T> &l, T search, T replacement)
{
for(typename list<T>::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 <typename T>
void print(const list<T> &l)
{
for(typename list<T>::const_iterator it = l.begin(); it != l.end(); ++it)
{
cout<< *it <<endl;
}
cout<<endl;
}
q3.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include "q3.h"
using namespace std;
int main()
{
ifstream infile("names.txt");
vector<int> num_vec = {1,2,3,4,5};
list<string> names;
list<int> 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]"<<endl;
print(names);
cout<<"num list loaded from a vector"<<endl;
print(num_list);
int searchNum,replaceNum;
cout<<"Enter a number to change: ";
cin>>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 "<<endl;
print(num_list);
print(names);
}
input file names.txt
john
alice
bob
peter
output
names list [loaded from file]
john
alice
bob
peter
num list loaded from a vector
1
2
3
4
5
Enter a number to change: 3
Enter the replacement: 77
Enter a name to change: alice
Enter the replacement: henry
After changes , the 2 lists are
1
2
77
4
5
john
henry
bob
peter
Q4 files
q4.h
#include <list>
using namespace std;
template <typename T>
class queue
{
private:
list<T> 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 <iostream>
using namespace std;
int main()
{
queue<int> q;
cout<<"enqueuing 1-10"<<endl;
for(int i=1; i<=10 ; i++)
q.enqueue(i);
cout<<"q.size = "<<q.size()<<endl;
cout<<"front = "<<q.front()<<endl;
cout<<"back = "<<q.back()<<endl;
cout<<"dequeing q"<<endl;
while(!q.empty())
{
cout<<q.deque()<<endl;
}
}
output
enqueuing 1-10
q.size = 10
front = 1
back = 10
dequeing q
1
2
3
4
5
6
7
8
9
10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.