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 .
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();
}