Lab 13 Practicing STL Vector Container 1a Write a template f

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

1a.

2a.

Edit & Run

  #include <iostream>  #include <deque>  #include <stack>    using namespace std;    //Purpose:function to add first 6 integers alternately to front and back of a deque  void AddAlt(deque<int> & D)  {          for (int i = 1; i < 6; i++)          {                  D.push_front(i++);                  D.push_back(i);          }  }    //Purpose:a template function to display the content of a deque using iterator  template <typename T>  int Display(ostream & out, deque<T> & D)  {          for (deque<int>::iterator it = D.begin(); it != D.end(); it++)          {                  cout << *it << \"   \";              cout << endl;          }  }    //Purpose:a template function to change back value to a specified value   template <typename T>  int changeBack(deque<T> & D, T item)  {          while (!D.empty())          {                  cin >> item;                  D.pop_back();                  D.push_back(item);                  cout << D.back() << \"  \";          }          cout << endl;  }    template <typename T>  int main()  {                    int item;          stack<T> stInt;                cout << \"Enter item:\" << endl;          cin >> item;          AddAlt(item);            for (int i = 0; i < 6; i++)          {                  stInt.push(i);                  Display(i);          }            stInt.push(item);          changeBack(D, item);          return 0;  }  

Edit & Run

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site