Lecture 15 Examples


image from book

Open table as spreadsheet

Program

Demonstrates the use of

copy.cpp

Demonstrates the copy algorithm.

count.cpp

Demonstrates the algorithm count()

date.h

Demonstrates that a non-system data type can be used with STL containers by using the class Date in this header.

deque.cpp

Demonstrates how the STL deque works.

deque2.cpp

Demonstrates how the STL deque works.

find.cpp.

Demonstrates the copy() algorithm.

iterator1.cpp

demonstrates the use of iterators to access elements of a vector as well as to obtain the iterator output of the algorithm copy( )

list_a.cpp

Demonstrates the use of a list that uses the default constructor and adds elements to both the front and back followed by removal from the front.

list_b.cpp

Demonstrates a list that is defined by the constructor that uses two pointers as arguments to input the data between these pointers into the list and then shows how some of the member functions work.

list_c.cpp

Demonstrates a list that is defined by the constructor that uses two pointers as arguments to input the data between these pointers into the list and then shows how an iterator may be used to access and change the values of the elements.

list_d.cpp

Demonstrates a list that is defined by the constructor that specifies the number elements and then uses an iterator to point into the list to show and to change the elements followed by using the algorithm find( ) to find a specific element.

multi1.cpp

Demonstrates how to construct multidimensional vectors, what the size is and to access the elements.

multi2.cpp

Demonstrates how to modify the contents of a multidimensional vector by using the [ ] operator on all dimensions.

multi3.cpp

Demonstrates how to modify a multidimensional vector by using the push_back( ) member function to create a non-rectangular table.

multi4.cpp

Demonstrates how to modify a multidimensional vector by using the push_back( ) member function to create a non-rectangular table.

new_factor.cpp

Demonstrates how the program factors.cpp can be rewritten to use the STL container stack.

new_queue.cpp

Demonstrates a date queue using the STL queue.

search.cpp

Demonstrates the algorithm search()

stl1.cpp

Demonstrates the use of the STL algorithm sort() acting on an array

stl2.cpp

Demonstrates the algorithm merge() that merges the elements of one array with the elements of another array and then sorts the resulting array.

stl3.cpp

Demonstrates the use of vectors

stl4.cpp

Demonstrates the STL template class list.

vector1.cpp

Demonstrates the definition of a vector object, the placing of elements into the vector, the changing size and capacity, and the use of overloading the [ ] operator to access elements of the vector.

vector2.cpp

Demonstrates two different constructors for vector. In additionally it demonstrates the vector function: swap( ).

vector3.cpp

Demonstrates the constructor where the number of elements is specified at definition and the values as well. It further shows how to insert new elements and delete others.

vector4.cpp

Demonstrates that a vector can be create in which the elements are dates.

vector5.cpp

Demonstrates that a vector may be accessed by using an iterator with a non-system data type like Date.

image from book

 //  program_id    copy.cpp //  written_by    Don Voils //  date_written  10/10/2006 // // //  description Demostrates the copy algorithm //               used with vector containers. // // #include<iostream> #include<algorithm> #include<vector> using namespace std; void main() {      short index;      char a_array[] = {'A','B','E','T','O','X','V','K'};      cout << "The elements of a_array[] are: ";      for(index = 0;index < 8;++index)           cout << a_array[index] << " ";      cout << endl << endl;      cout << "vector1 is defined to contain the elements of a_array[]" << endl;      vector<char> vector1(a_array,a_array+8);           vector<char> vector2(10);      // Notice that vector2 has space for 10 elements.      // If you try this exercise with fewer than room      // for 5 elements, the program will crash. This would      // imply that copy() does not execute unless there is      // enough room to receive that data. Apparently      // the vector does not expand to accept the data in      // this case.      //      cout << "The elements in vector1 are the following: ";      for(index = 0;index < (int)vector1.size(); ++index)           cout << vector1[index] << " ";      cout << endl << endl;      cout << "The first five elements of vector1 are copied"                << " into vector2" << endl << endl;      copy(vector1.begin(),vector1.begin()+5,vector2.begin());      cout << "The elements in vector2 are the following: ";      for(index = 0;index < (int)vector2.size(); ++index)           cout << vector2[index] << " ";      cout << endl << endl;            cout << "Continue? (Y/N) ";      char resp;      cin >> resp;      cout << endl << endl; } // program_id        count.cpp // written_by        Don Voils // date_written      10/5/2006 // // description    Demonstrates the algorithm count() //                 used on a vector container. // // // // #include<iostream> #include<algorithm> #include<vector> using namespace std; void main() {   char a_char[] = {'A','C','E','A','B','C','D','C','A','E'};   vector<char> vector1(a_char,a_char+9);   cout << "The elements in vector1 are: ";   for(short index=0; index< (int)vector1.size();++index)        cout << vector1[index] << " ";   cout << endl << endl;   short numb = (int)count(vector1.begin(),vector1.end(),'A');   cout << "There are " << numb << " of the char A in vector1"      << endl << endl;   cout << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id     date.h // written_by     Don Voils // date_written   9/10/2006 // description This header contains the definition //             of the class date that is used in many //             different examples. // #ifndef DATE #define DATE class Date {  private:    long month,         day,         year;  public:    Date(){ }    Date(long m, long d, long y)    { month = m;     day = d;     year = y;    }    void get_date(void);    void set_date(long m,long d,long y);    long last_day(long m,long y);    long show_day();    long show_month();    long show_year();    bool leap_year(long y);    Date operator +(long number_days);    Date operator -(long number_days);    long operator -(Date other_date);    long days_since(long m,long d,long y);    long f(long m,long y);    long g(long m);    bool incorrect_date(long m,long d,long y);    bool operator <(Date a_date);    bool operator <=(Date a_date);    bool operator ==(Date a_date);    bool operator >(Date a_date);    bool operator >=(Date a_date);    friend ostream &operator << (ostream &stream, Date ab);    friend istream &operator >> (istream &stream, Date &ab); };    ostream &operator << (ostream &stream, Date ab)    {      if(ab.month < 10)        stream << "0" << ab.month << "/";      else        stream << ab.month << "/";      if(ab.day < 10)         stream << "0" << ab.day << "/";      else        stream << ab.day << "/";      stream << ab.year;      return stream;    }    istream &operator >> (istream &stream, Date &ab)    {      char slash;      stream >> ab.month >> slash >> ab.day >> slash >> ab.year;      if(ab.year< 100)         if(ab.year >10)           ab.year += 1900;         else           ab.year += 2000;      return stream;    } long Date::show_day() {  return day; } long Date::show_month() {  return month; } long Date::show_year() {  return year; } void Date::get_date() {  char    dash;  do  {   cin >> month >> dash >> day >> dash       >> year;  }while(incorrect_date(month,day,year)); } void Date::set_date(long m,long d,long y) {  month = long(m);  day = long(d);  year = long(y); } Date Date::operator + (long number_days) {  Date next_day;  next_day.set_date(month,day,year);  for(long index=1;index <=number_days;++index)  {    if (next_day.day < last_day(next_day.month,next_day.year))    {     next_day.day += 1L;    }    else    {      if(next_day.month<12L)      {         next_day.month += 1L;         next_day.day = 1L;      }      else      {         next_day.month = 1L;         next_day.day = 1L;         next_day.year += 1L;       }    }  }  return next_day; } Date Date::operator - (long number_days) {  Date next_day;  next_day.set_date(month,day,year);  for(short index=1;index <= number_days;++index)  {    if (next_day.day != 1L)    {     next_day.day -= 1L;    }    else    {      if(next_day.month != 1L)      {      next_day.month -= 1L;      next_day.day = last_day(next_day.month,next_day.year);      }      else      {     next_day.month = 12L;     next_day.day = 31L;     next_day.year -= 1L;     }     }  }  return next_day; } long Date::last_day(long m,long y) {  long last_day;  long days_in_month[]={0L, 31L, 28L, 31L, 30L, 31L, 30L, 31L, 31L, 30L, 31L, 30L, 31L};  if (m!=2L)      last_day=days_in_month[m];  else      if (leap_year(y))           last_day =29L;      else           last_day=days_in_month[m];  return last_day; } bool Date::leap_year(long y) {   bool leap_test;   if (((y%4L== 0L) && (y%100L != 0L)) || (y%400L == 0L))       leap_test=true;   else      leap_test=false;   return leap_test; } bool Date::incorrect_date(long m,long d,long y) {   bool not_correct;   if ((m>=1L) && (m<=12L) &&           (d>=1L) && (d<=last_day(m,y)))       not_correct = false;   else        not_correct = true;   return not_correct; } long Date::days_since(long m,long d,long y) {    long number = 1461L*f(m,y)/4L + 153L*g(m)/5L + d;    return(number); } long Date::f(long m,long y) {    long number = (m<=2L) ? y - 1L :y;    return(number); } long Date::g(long m) {    long number = (m<=2L) ? m + 13L : m + 1L;    return(number); } long Date::operator - (Date b_date) {  long days;  days = days_since(month,day,year)     -days_since(b_date.month,b_date.day,b_date.year);  return days; } bool Date::operator >(Date a_date) {   bool resp;   long first_date,        second_date;   first_date=days_since(month,day,year);   second_date=days_since(a_date.month,a_date.day,a_date.year);   resp = (first_date > second_date)? true : false;   return resp; } bool Date::operator >=(Date a_date) {   bool resp;   long first_date,        second_date;   first_date=days_since(month,day,year);   second_date=days_since(a_date.month,a_date.day,a_date.year);   resp = (first_date >= second_date)? true : false;   return resp; } bool Date::operator ==(Date a_date) {   bool resp;   long first_date,        second_date;   first_date=days_since(month,day,year);   second_date=days_since(a_date.month,a_date.day,a_date.year);   resp = (first_date == second_date)? true : false;   return resp; } bool Date::operator <(Date a_date) {   bool resp;   long first_date,         second_date;   first_date=days_since(month,day,year);   second_date=days_since(a_date.month,a_date.day,a_date.year);   resp = (first_date < second_date)? true : false;   return resp; } bool Date::operator <= (Date a_date) {   bool resp;   long first_date,       second_date;   first_date=days_since(month,day,year);   second_date=days_since(a_date.month,a_date.day,a_date.year);   resp = (first_date <= second_date)? true : false;   return resp; } #endif // program_id      deque2.cpp // written_by      Don Voils // date_written    10/1/2006 // // description Demonstrates how the STL deque works. // #include<iostream> #include<deque> using namespace std; void main() {   deque<short> a_deque;   deque<short> a_deque1(10);   deque<short> a_deque2(5,6);   cout << "We made the following definitions:" << endl        << "  deque<short> a_deque;" << endl        << "  deque<short> a_deque1(10);" << endl        << "  deque<short> a_deque2(5,6);" << endl;   cout << endl << "The a_deque has the following number of elements "        << (int)a_deque.size() << endl        << "The a_deque1 has the followig number of elements "        << (int)a_deque1.size() << endl        << "The a_deque2 has the following number of elements "        << (int)a_deque2.size() << endl << endl;;   cout << "The elements in the a_deque2 are: ";   for(short index =0; index< (int)a_deque2.size(); ++index)        cout << a_deque2[index] << " ";   cout << endl << endl;   a_deque.push_back(32);   a_deque.push_back(12);   a_deque.push_front(132);   a_deque.push_front(121);   a_deque.push_front(45);   cout << "Some elements have been added to the a_deque and"        << endl << "the number of elements is: " << (int)a_deque.size()        << endl << endl;   cout << "The elements in the a_deque are: ";   for(short index =0; index< (int)a_deque.size(); ++index)        cout << a_deque[index] << " ";   cout << endl << endl;   deque<short>::iterator p;   p = a_deque.begin();   deque<short> a_deque3(p+1,p+4);   cout << "The a_deque3 is defined with some elements from a_deque as the following:"        << endl << "  deque<short>::iterator p;"        << endl << "  p = a_deque.begin();"        << endl << "  deque<short> a_deque3(p+1,p+4);" << endl        << endl << "The number of elements is: " << (int)a_deque3.size()        << endl << "and the elements in the a_deque3 are: ";   for(short index =0; index< (int)a_deque3.size(); ++index)        cout << a_deque3[index] << " ";   cout << endl << endl;   cout << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id      deque.cpp // written_by      Don Voils // date_written    10/1/2006 // // description Demonstrates how the STL deque works. // #include<iostream> #include<deque> using namespace std; void main() {   deque<short> a_deque;   cout << "The deque has the following number of elements "        << (int)a_deque.size() << endl << endl;   a_deque.push_back(32);   a_deque.push_back(12);   a_deque.push_front(132);   a_deque.push_front(121);   a_deque.push_front(45);   cout << "Some elements have been added to the deque and"        << endl << "the number of elements is: " << (int)a_deque.size()        << endl << endl;   cout << "The elements in the deque are:"        << endl;   for(short index =0; index< (int)a_deque.size(); ++index)          cout << a_deque[index] << " ";   cout << endl << endl;   a_deque[2] = 222;   a_deque.pop_back();    a_deque.pop_front();    cout << "After some changes, the number of elements are now: "         << (int)a_deque.size() << endl << endl;    cout << "The elements in the deque are: "         << endl;    for(short index =0; index< (int)a_deque.size(); ++index)      cout << a_deque[index] << " ";    cout << endl << endl;    cout << "Continue? (Y/N) ";    char resp;    cin >> resp;    cout << endl << endl; } //  program_id         find.cpp //  written_by         Don Voils //  date_written       10/5/2006 // //  description        Demonstrates the algorithm find(). // // #include<iostream> #include<algorithm> using namespace std; void main() {      short a_array[] = {23, 11, 28, 46, 17, 19, 28, 44, 92};      short* ptr;      cout << "The elements of a_array[ ] are: ";      for(short index=0; index < 9;++index)           cout << a_array[index] << " ";      cout << endl << endl;      cout << "The code will now attempt to find where"                 << " 46 is located in array[]" << endl;      ptr = find(a_array,a_array+9,46);      cout << "The value: " << *ptr << " was found in postion: "                 << (int)(ptr - a_array) + 1 << endl;      cout << endl << "Continue? (Y/N) ";      char resp;      cin >> resp;      cout << endl << endl; } //  program_id        iterator1.cpp //  written_by        Don Voils //  date_written      10/7/2006 // //  description Demonstrates the use of iterators //               to access elements of a vector //               as well as to obtain the iterator output //               of the algorithm copy() // #include<iostream> #include<iomanip> #include<algorithm> #include<vector> using namespace std; void main() {  short index,        start = 3,        end = 7;  short a_array[] = {43, 54, 2, 34, 63, 67, 25, 72, 67, 64, 28};  cout << "The elements in a_array[] are: "       << endl;  for(index = 0; index < 11;++index)       cout << setw(5) << a_array[index] << endl;  cout << endl << endl;  cout << "The elements of a_array[] are used to define vector1"       << endl << endl;  // Notice how these vectors are defined.  // In particular notice the constructors that are being used.  vector<short> vector1(a_array,a_array+11);  cout << "The elements in vector1 are: "       << endl;  for(index=0; index < (short)vector1.size();++index)       cout << setw(5) << vector1[index] << endl;  cout << endl << endl;  cout << "The definition of vector2 yields a vector with "       << "room for 11 elements" << endl;  vector<short> vector2(11);  // Notice the three vector iterators that are defined  // The vector container's iterators default to random access.  //  vector<short>::iterator iterator1 = vector1.begin() + start;  vector<short>::iterator iterator2 = vector1.begin() + end;  vector<short>::iterator iterator3;  cout << endl << "The copy of elements of vector1 into vector2 began at "       << endl << "position: " << start + 1<< " and ended before "       << "position " << end + 1 << endl << endl;  // The algorithm copy() outputs a random access iterator.  // The output iterator is one beyond the end of where  // the data was copied into the vector.  //  iterator3 = copy(iterator1,iterator2,vector2.begin());  iterator1 = vector2.begin();  // Observe that two different operators are being used  // on iterator1: the dereference operator and the increment  // operator. Question: what is the order of precendence?  //  //  cout << endl << "The elements of vector2 are: " << endl;  while(iterator1 != iterator3)      cout << setw(5) << *iterator1++ << endl;  cout << endl << endl << "Continue? (Y/N) ";  char resp;  cin >> resp;  cout << endl << endl; } //  program_id      list_a.cpp //  written_by      Don Voils //  date_written    10/23/2006 // //  description Demonstrates the use of a list that //               uses the default constructor and //               adds elements to both the front and back //               followed by removal from the front. // // #include<iostream> #include<list> using namespace std; void main() {   list<short> list1;   cout << "The value 23 is pushed on the back. " << endl;   list1.push_back(23);   cout << "The value 36 is pushed on the back. " << endl;   list1.push_back(36);   cout << "The value 42 is pushed on the front. " << endl;   list1.push_front(42);   cout << "The value 63 is pushed on the front. " << endl;   list1.push_front(63);   cout << endl << endl;   long size = (long)list1.size();   cout << "The size of the list is: " << (short)list1.size()        << endl << endl;   cout << "The elements in the list are: " << endl;   for(short index = 0;index<size;++index)   {     cout << index + 1 << " " << list1.front() << endl;     list1.pop_front();   }   cout << endl << endl << "Continue? Y/N ";   char resp;   cin >> resp;   cout << endl << endl; } //  program_id     list_b.cpp //  written_by     Don Voils //  date_written   10/23/2006 // // description Demonstrates a list that is defined by //             the constructor that uses two pointers //             as arguments to input the data between //             these pointers into the list and then shows //             how some of the list member functions work. // // #include<iostream> #include<list> using namespace std; void main() {            short array1[] = {77, 63, 55, 23, 21};            short array2[] = {63,77,89,24,64,24,63,65};      cout << "The array1[] is defined with the following elements"                 << endl;      for(short index = 0; index < 5;++index)           cout << array1[index] << " ";            cout << endl << endl           << "The array2[] is defined with the following elements"           << endl;      for(short index = 0; index < 8;++index)           cout << array2[index] << " ";            cout << endl << endl;      cout << "list1 is defined in terms of array1. " << endl << endl;            list <short> list1(array1, array1+5);      cout << "list2 is defined in terms of array2. " << endl << endl;            list <short> list2(array2, array2+7);            cout << "After the definitions: " << endl                 << "The size of list1 is: " << (short)list1.size() << endl                 << "The size of list2 is: " << (short)list2.size() << endl;      cout << endl << "The elements of list1 are reversed and those of list2 are sorted. " << endl;            list1.reverse();            list2.sort();      cout << endl << "The elements of list1 and list2 are merged into list1. " << endl;            list1.merge(list2);            cout << endl << endl                << "The size of list1 is: " << (short)list1.size() << endl                << "The size of list2 is: " << (short)list2.size()                << endl<< endl;      cout << "The elements of list1 are sorted and "                 << "then the non-unique elements are eliminated." << endl;            list1.sort();            list1.unique();            short size = (short)list1.size();            cout << endl << endl                 << "The elements of list1 after sort() and unique() are: "                 << endl;            for(short index = 0;index<size;++index)            {          cout << index + 1 << " " << list1.front() << endl;          list1.pop_front();             }             cout << endl << endl << "Continue? Y/N ";             char resp;             cin >> resp;             cout << endl << endl; } //  program_id      list_c.cpp //  written_by      Don Voils //  date_written    10/23/2006 // //  description Demonstrates a list that is defined by //              the constructor that uses two pointers //              as arguments to input the data between //              these pointers into the list and then shows //              shows how an iterator may be used to //              access and change the values of the elements. // // #include<iostream> #include<iomanip> #include<list> using namespace std; void main() {   short array1[] = {77, 63, 55, 23, 21};   cout << "The array: array1[] has the following elements: "        << endl;   for(short index=0;index<5;++index)     cout << setw(5) << array1[index] << endl;   cout << endl << endl;   cout << "The list: list1 is populated by the elements of array1[]."        << endl;   list <short> list1(array1, array1+5);   list<short>::iterator iterator1;   cout << endl << endl        << "The elements in list1 are: "        << endl;   for(iterator1 = list1.begin();iterator1!=list1.end();++iterator1)   {     cout << setw(5) << *iterator1 << endl;   }   cout << endl        << "The number 10 is being added to each of the elements in list1 : "        << endl;   for(iterator1 = list1.begin();iterator1!=list1.end();++iterator1)   {     *iterator1 +=10;   }   cout << endl << endl        << "The elements in list1 after being changed are: "        << endl;   for(iterator1 = list1.begin();iterator1!=list1.end();++iterator1)   {     cout << setw(5) << *iterator1 << endl;   }   cout << endl << endl << "Continue? Y/N ";   char resp;   cin >> resp;   cout << endl << endl; } //  program_id      list_d.cpp //  written_by      Don Voils //  date_written    10/23/2006 // //  description Demonstrates a list that is defined by //              the constructor that specifies the number //              elements and then uses an iterator //              to point into the list to show and to //              change the elements followed by using //              the algorithm find() to find a specific. //              element. // #include<iostream> #include<iomanip> #include<list> #include<algorithm> using namespace std; void main() {   list <short> list1(6);   list<short>::iterator iterator1;   short counter = 1;   cout << "The elements in list1 are: " << endl;   for(iterator1 = list1.begin();iterator1!=list1.end();++iterator1)   {     cout << setw(5) << *iterator1 << endl;   }   cout << endl        << "Each element in list1 is changed " << endl << endl;   for(iterator1 = list1.begin();iterator1!=list1.end();++iterator1)   {     ++counter;     *iterator1 += counter;   }   cout << endl << endl        << "The elements in list1 after being changed are: "        << endl;   for(iterator1 = list1.begin();iterator1!=list1.end();++iterator1)   {     cout << setw(5) << *iterator1 << endl;   }   iterator1 = find(list1.begin(),list1.end(),6);   cout << endl << "Trying to determine whether 6 is in list1. "        << endl << endl;   if(iterator1!=list1.end())      cout << endl << "We did find the number 6" << endl;   else      cout << endl << "We did not find the number 6" << endl;   iterator1 = find(list1.begin(),list1.end(),10);   cout << endl << "Trying to determine whether 10 is in list1. "        << endl << endl;   if(iterator1!=list1.end())      cout << endl << "We did find the number 10" << endl;   else      cout << endl << "We did not find the number 10" << endl;   cout << endl << endl << "Continue? Y/N ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id        multi1.cpp // written_by        Don Voils // date_written      10/1/2006 // // description This program demonstrates how to construct //             multidimensional vectors, what the size is //             and to access the elements. // // #include<iostream> #include<vector> using namespace std; void main() {   vector< vector<double> > vector1;   cout << "The size of vector1 is: " << (int)vector1.size() << endl << endl;   short ROWS = 4;   short COLUMNS = 5;   vector< vector<double> > vector2(ROWS, vector<double>(COLUMNS,2.1));   cout << "The size of vector2 is: " << (int)vector2.size() << endl << endl;   cout << endl << "In vector2 " << endl << endl;   for(short index=0;index < (int)vector2.size();++index)        cout << "The size of row " << index+1 << " is "             << (int)vector2[index].size() << endl;   cout << endl << "The elements in vector2 are: " << endl << endl;   for(short index=0;index < (int)vector2.size();++index)   {      cout << "The elements of row " << index+1 << " are: ";      for(short jndex=0; jndex < (int)vector2[index].size();++jndex)        cout << vector2[index][jndex] << " ";      cout << endl;   }   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id        multi2.cpp // written_by        Don Voils // date_written      10/1/2006 // // description This program demonstrates how to modify //             the contents of a multidimensional vector //             by using the [ ] operator on all dimensions. // // #include<iostream> #include<vector> using namespace std; void main() {   short ROWS = 4;   short COLUMNS = 5;   vector< vector<double> > vector2(ROWS, vector<double>(COLUMNS,2.1));   cout << "To begin with the elements in vector2 are:"        << endl << endl;   for(short index=0;index < (int)vector2.size();++index)   {        cout << "The elements of row " << index+1 << " are: ";        for(short jndex=0; jndex < (int)vector2[index].size();++jndex)          cout << vector2[index][jndex] << " ";        cout << endl << endl;   }   for(short index=0;index < (int)vector2.size();++index)         for(short jndex=0; jndex < (int)vector2[index].size();++jndex)          vector2[index][jndex] += (index + jndex);   cout << endl << "The elements have been changed and they are:"        << endl << endl;   for(short index=0;index < (int)vector2.size();++index)   {         cout << "The elements of row " << index+1 << " are: ";         for(short jndex=0; jndex < (int)vector2[index].size();++jndex)            cout << vector2[index][jndex] << " ";         cout << endl << endl;   }   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id        multi3.cpp // written_by        Don Voils // date_written      10/1/2006 // // description This program demonstrates how to modify //             a multidimensional vector by using the //             push_back() member function to create //             a non-rectangular table. // // #include<iostream> #include<vector> using namespace std; void main() {   short ROWS = 4;   short COLUMNS = 5;   vector< vector<double> > vector2(ROWS, vector<double>(COLUMNS,2.1));   cout << "To begin with the elements in vector2 are:"        << endl << endl;   for(short index=0;index < (int)vector2.size();++index)   {         cout << "The elements of row " << index+1 << " are: ";         for(short jndex=0; jndex < (int)vector2[index].size();++jndex)         {           cout << vector2[index][jndex];             if(jndex+1 < (int)vector2[index].size())                   cout << ", ";         }         cout << endl << endl;   }   for(short index=0;index < (int)vector2.size();++index)        if(index%2==0)           vector2[index].push_back(index + 0.3);   cout << endl << "The elements have been changed and they are:"        << endl << endl;   for(short index=0;index < (int)vector2.size();++index)   {        cout << "The elements of row " << index+1 << " are: ";        for(short jndex=0; jndex < (int)vector2[index].size();++jndex)        {          cout << vector2[index][jndex];            if(jndex+1 < (int)vector2[index].size())                 cout << ", ";        }        cout << endl << endl;   }   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id        multi4.cpp // written_by        Don Voils // date_written      10/1/2006 // // description This program demonstrates how to modify //             a multidimensional vector by using the //             push_back() member function to create //             a non-rectangular table. // // #include<iostream> #include<vector> using namespace std; void main() {   short ROWS = 4;   short COLUMNS = 5;   vector< vector<double> > vector2(ROWS, vector<double>(COLUMNS,2.1));   cout << "To begin with the elements in vector2 are:"        << endl << endl;   for(short index=0;index < (int)vector2.size();++index)   {        cout << "The elements of row " << index+1 << " are: ";        for(short jndex=0; jndex < (int)vector2[index].size();++jndex)        {             cout << vector2[index][jndex];             if(jndex+1 < (int)vector2[index].size())                  cout << " - ";        }        cout << endl;   }   vector2.push_back(vector<double>(7,6.1));   vector2.push_back(vector<double>(4,7.1));   vector2.push_back(vector<double>(6,5.1));   cout << endl << "The vector2 has added three columns and is:"        << endl << endl;   for(short index=0;index < (int)vector2.size();++index)   {        cout << "The elements of row " << index+1 << " are: ";        for(short jndex=0; jndex < (int)vector2[index].size();++jndex)        {             cout << vector2[index][jndex];             if(jndex+1 < (int)vector2[index].size())                  cout << " - ";        }        cout << endl;   }   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } //  program_id        new_factor.cpp //  written_by        Don Voils //  date_written      10/1/2006 // //  description This program demonstrates how the //               program factors.cpp can be rewritten //               to use the STL container stack. // // // #include<iostream> #include<stack> using namespace std; long factor(long a); void clear_screen(); void main() {      char resp;      do      {                 stack<long> a_stack;                 long numb;          clear_screen();          cout << "What is the number you want to factor? ";          cin >> numb;          clear_screen();          long n = numb;          try          {               do               {                    a_stack.push(factor(n));                    long divisor = a_stack.top();                    n = n / divisor;               }while(n>1);               cout << "The following is the prime factorization of "                                         << numb <<endl                                         << " ";               while(!a_stack.empty())               {                    cout << a_stack.top();                    a_stack.pop();                    if(!a_stack.empty())                         cout << " * ";               }         }         catch(int)         {             clear_screen();             cerr << "Number is either prime or too large for functon.";         }         cout << endl << endl << "Another factorization? (Y/N) ";         cin >> resp;         clear_screen();     }while(resp=='y' || resp=='Y'); } long factor(long n) {      long primes[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};      long dividend;      short index;      for(index = 0;index < 15; ++index)      {           dividend = n / primes[index];           if(dividend * primes[index] == n)           break;      }      if(index >= 15)           throw 1;      return primes[index]; } void clear_screen() {      for(short index = 1;index <= 250;++index)           cout << endl; } //  program_id       new_queue.cpp //  author           don voils //  date written    9/17/2006 // //  description This program simulates a date queue. //              using the STL queue. // // #include<iostream> #include<queue> using namespace std; void clear_screen(); // // The date class is included to demonstrate the generality of // the class: Queue contained in the header: new_queue.h that is // included below. // #include"date.h" // // Below is the test program to determine whether the queue class // works or not. // void main() {      short which;      char resp;      Date item;      queue<Date> a_queue;      do      {          clear_screen();          cout << "Select an option:" << endl << endl             << " 1. Add to the back of the queue" << endl << endl             << " 2. Is the queue empty?" << endl << endl             << " 3. What is the front element in the queue?" << endl << endl             << " 4. Remove the front element of the queue" << endl << endl             << " 5. Exit the program." << endl << endl             << endl <<endl << "Which option? ";          cin >> which;          switch(which)          {               case 1:                                         clear_screen();                    cout << "Enter a date to add to the queue (mm/dd/yyyy): ";                                      cin >> item;                          a_queue.push(item);                                      cout << endl                                           << "The queue is not full so an element was added."                                           << endl;                                      cout << endl << endl << "Continue? (Y/N) ";                                      break;               case 2:                                      clear_screen();                    if(a_queue.empty())                       cout << "The queue is empty.";                   else                                          cout << "The queue is not empty.";                                       cout << endl << endl << "Continue? (Y/N) ";                                       cin >> resp;                                       break;               case 3:                                      clear_screen();                                      if(!a_queue.empty())                    cout << "The Front element in the queue is: "                         << a_queue.front();                                     else                          cout << "The queue is empty.";                                    cout << endl << endl << "Continue? (Y/N) ";                                    cin >> resp;                                    break;               case 4:                                    clear_screen();                                    if(!a_queue.empty())                                    {                                       a_queue.pop();                                       cout << "The front element has been removed. ";                                    }                                    else                                      cout << "There were no elements to remove";                                cout << endl << endl << "Continue? (Y/N) ";                                      cin >> resp;                                      break;               case 5:                                      break;               default:                                      break;           }       }while(which != 5);       clear_screen(); } inline void clear_screen() {       for(short index=1;index<=250;++index)                  cout << endl; } //  program_id      search.cpp //  written_by      Don Voils //  date_written    10/5/2006 // //  descripton  Demonstrates the algorithm search() //               with char arrays. // // #include <algorithm> #include <iostream> using namespace std; void main() {      char sentence[] = "This is the time to go home.";      char phrase[] = "the time";      char* ptr = search(sentence,sentence+28,phrase,phrase+8);      cout << "The phrase was: " << phrase << endl                 << "The sentences was: " << sentence << endl << endl;      if(ptr==sentence+28)            cout << "The phrase was not there" << endl;      else            cout << "The phrase was in the sentence and began in position "                             << (int)(ptr-sentence +1)                      << endl;      cout << endl << "Continue? (Y/N) ";      char resp;      cin >> resp;      cout << endl << endl; } // program_id   stl1.cpp // written_by   Don Voils // date_written 9/10/2006 // // description This program demonstrates the use of the STL //             algorithm sort() acting on an array. Notice //             how much easier it is to use this function //             rather than writing your own sort function. // #include<iostream> #include<iomanip> #include<algorithm> // The following header may be required for your compiler. // //#include<functional> using namespace std; void main() {  double stuff[] = {3.44, 545.55, -45.45, 52.5, 0.34, -1.34};  short index;  cout << fixed << setprecision(2);  cout<< "The array before the sort is: " << endl;  for(index=0;index<6;++index)    cout << setw(10) << stuff[index] << endl;  sort(stuff, stuff+6);  cout << endl << "The array after the sort is: " << endl;  for(index=0;index<6;++index)    cout << setw(10) << stuff[index] << endl;  cout << endl << "Continue? (Y/N) ";  char resp;  cin >> resp;  cout << endl << endl; } // program_id   stl2.cpp // written_by   Don Voils // date_written 9/10/2006 // // description: This program using the algorithm //              merge() that merges the elements of //              one array with the elements of another //              array and then it uses the algorithm sort() //              that sorts each of the individual arrays and //              then the algorithm sort() is used to sort //              the the resulting array. Notice how much easier //              using these algorithms than writing your own //              merge and sort functions. // //              Note: In Visual Studio .NET 2005 a warning is //              created because the algorithm merge() has //              been deprecated for security reasons. // #include<iostream> #include<iomanip> #include<algorithm> // The following header may be required for // your compiler. // // #include<functional> using namespace std; void main() {  short index;  double stuff1[] = {3.44,545.55,-45.45,52.5,0.34,-1.34};  double stuff2[] = {54.34, 122.54, -53.45, 75.45};  double stuff3[10];  cout << fixed << setprecision(2);  cout << "The unsorted contents of stuff1[] are the following:" << endl;  for(index=0;index<6;++index)    cout << setw(10) << stuff1[index] << endl;  cout << endl << endl       << "The unsorted contents of stuff2[] are the following:" << endl;  for(index=0;index<4;++index)    cout << setw(10) << stuff2[index] << endl;  cout << endl << endl;  sort(stuff1, stuff1+6);  sort(stuff2, stuff2+4);  cout << endl << endl << "Each of the arrays is now sorted so that they may be merged."       << endl;  sort(stuff1, stuff1+6);  sort(stuff2, stuff2+4);  // The algorithm merge() is used to merge the array stuff1 and  // stuff2 into the array stuff3.  merge(stuff1,stuff1+6,stuff2,stuff2+4,stuff3);  cout<< endl<< "The array stuff3[] after the merge is: " << endl;  for(index=0;index<10;++index)    cout << setw(10) << stuff3[index] << endl;  cout << endl << "Continue? (Y/N) ";  char resp;  cin >> resp;  cout << endl << endl; } // program_id   stl3.cpp // written_by   Don Voils // date_written 9/10/2006 // // description This program illustrates the use of vectors. // // #include<iostream> #include<iomanip> #include<vector> using namespace std; void main() {   vector<double> stuff;   cout << fixed << setprecision(2);   cout << "The following values added to the double vector stuff:"        << endl << setw(10) << 34.45        << endl << setw(10) << 444.34        << endl << setw(10) << -234.34        << endl << setw(10) << 4.34        << endl << endl;   stuff.push_back(34.45);   stuff.push_back(444.34);   stuff.push_back(-234.34);   stuff.push_back(4.34);   cout << "At the beginning the vector stuff contains: " <<endl;   short index;   for(index=0;index<4;++index)     cout << setw(10) << stuff[index] << endl;   cout << endl << "The first element in the vector is replaced with -434.44"        << endl << "The third element in the vector is replace with 66.33";   stuff[0] = -434.44;   stuff[2] = 66.33;   cout << endl << endl << "After the first and third elements are"        << endl << "replaced the vector stuff contains: "        << endl;   for(index=0;index<4;++index)     cout << setw(10) << stuff[index] << endl;   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id   stl4.cpp // written_by   Don Voils // date_written 9/10/2006 // // program description This program demonstractes the //             STL template class list and some //             of its member functions. // #include<iostream> #include<iomanip> #include<list> using namespace std; void main() {   char stuff1[] = {'E', 'D', 'C', 'B', 'A'};   char stuff2[] = {'B', 'C', 'G', 'H', 'J', 'M', 'N'};   cout << "The array stuff1[] contains the following elements:" << endl;   for(short index = 0;index <5;++index)     cout << setw(5) << stuff1[index] << endl;   cout << endl << "The array stuff2[] contains the following elements:" << endl;   for(short index = 0;index <7;++index)     cout << setw(5) << stuff2[index] << endl;   cout << endl << "The elements of array stuff1 are being added to"        << " list1 at definition time." << endl;   list<char> list1(stuff1,stuff1+5);   cout << endl << "The elements of array stuff2 are being added to"        << " list2 at definition time." << endl;   list<char> list2(stuff2,stuff2+7);   cout << endl << "After definition list1 has " << (int) list1.size() << " elements"        << endl << "and list2 has " << (int) list2.size() << " elements"        << endl << endl;      // The list list1 is reversed   cout << endl << "The element in list1 are reversed." << endl;   list1.reverse();      // The list list2 is merged into list1.   cout << endl        << "The elements of list2 are merged with the elements of list1. "        << endl;   list1.merge(list2);   cout << endl << "After a merge of list1 and list2, list1 has "        << (int)list1.size() << " elements" << endl << "and list2 has "        << (int)list2.size() << " elements" << endl << endl;      // The list list2 eliminates duplicate entries.   cout << endl        << "The elements in list1 are now unique."        << endl << endl;   list1.unique();   cout << "After making list1 have unique elements, it has "        << (int)list1.size() << " elements" << endl << endl;   cout << "The elements of list1, are:" << endl;   while(!list1.empty())   {     cout << setw(5) << list1.front()<< endl;     list1.pop_front();   }   cout << endl << "The elements of list1 have been removed."        << endl << "The size of list1 is now: "        << (int)list1.size()<< endl;   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program-id      vector1.cpp // written_by      Don Voils // date written:   9/30/2006 // description  Demonstrates the definition of a vector //              object, the placing of elements into the //              vector, the changing size and capacity, //              and the use of overloading the [ ] operator //              to access elements of the vector. // // #include<iostream> #include<iomanip> #include<vector> using namespace std; void main() {   // The following defines a short vector.   vector<short> v;   // Checks the size() and capacity() before any   // elements are added. This may be different for   // different systems.   cout << "Before adding any elements: "<< endl        << "The size of the vector is: " << (int) v.size() << endl        << "The capacity of the vector is: " << (int)v.capacity()        << endl << endl;   cout << "The following four elements are placed at the end of the vector "        << "one at a time." << endl        << setw(5) << 5 << endl        << setw(5) << 7 << endl        << setw(5) << 16 << endl        << setw(5) << 23 << endl        << endl << endl;   // places four shorts at the back of the vector one at a time.   v.push_back(5);   v.push_back(7);   v.push_back(16);   v.push_back(23);   // output the four short elements of the vector one at a time.   cout << "After adding these elements, the vector v contains: "        << endl;   for(short index=0; index<(int)v.size();++index)     cout << setw(5) << v[index] << endl;   cout << endl << "After adding four elements: " << endl        << "The size of the vector is: " << (int)v.size() << endl        << "The capacity of the vector is: " << (int)v.capacity()        << endl << endl;   cout << endl << "Two elements of the vector are replaced using"        << " the overloaded [ ] operator." << endl;   v[0] = 24;   v[1] = 44;   cout << endl <<"After overloading the [] operator and changing"        << endl << "the first and second element the output would be:"        << endl;   // outputing to the screen the elements to see that they   // have been changed above.   for(short index=0; index<(int)v.size();++index)     cout << setw(5) << v[index] << endl;   cout << endl << "Continue? (Y/N) " ;   char resp;   cin >> resp;   cout << endl << endl; } //  program-id        vector2.cpp //  written_by        Don Voils //  date-written      9/30/2006 // //  description: This program demonstrates //               two different constructors for //               vector. In additionally it demonstrates //               the vector swap() functon. // // #include<iostream> #include<iomanip> #include<vector> using namespace std; void main() {   cout << fixed << setprecision(2);   double d_array[] = {44.32, 55.23, 563.22, 632.46, 526.34};   double* d_ptr = d_array;   cout << "The elements of the double array: d_array[] are:" << endl;   for(short index=0; index< 5; ++index)     cout << setw(10) << *(d_ptr + index) << endl;   cout << endl << endl;   // Defining a vector using the constructor with pointer arguments.   cout << "The double vector v1 is defined using the elements of"        << endl << "the double array d_array[] and a pointer d_ptr"        << endl << "that points to the beginning of the array d_array[]"        << endl << endl;   vector<double> v1(d_ptr, d_ptr+5);   cout << "After definition, the size of v1 is " << (int)v1.size()        << " and the capacity of v1 is " << (int)v1.capacity()        << endl << endl;   cout << "The elements stored in v1 are: " << endl;   for(short index=0; index < (int)v1.size();++index)        cout << setw(10) << v1[index] << endl;   cout << endl << endl;   // Defining a vector using the constructor which specifies the size.   vector<double> v2(3);   cout << endl << "After definition, the size of the vector v2 is " << (int)v2.size()        << " and the capacity of v2 is " << (int)v2.capacity()        << endl;   // Using the vector member functions that swaps all of the elements in one vector   // to another vector.   v1.swap(v2);   cout << endl << "The elements in vector v1 are swaped with"        << endl << "the elements of the vector v2."        << endl << endl;   // The swapping of the elements of vector v1 to v2 increased the size and   // capacity of the vector v2 and decreased the size and capacity of v1.   cout << endl << "After the swap, the size of the vector v1 is "        << (int)v1.size()        << " and the capacity of v1 is " << (int)v1.capacity()        << endl;   cout << endl << "After the swap, the size of the vector v2 is " << (int)v2.size()        << " and the capacity of v2 is " << (int)v2.capacity()        << endl;   cout << endl << "The elements stored in v2 are: " << endl;   for(short index=0; index < (int)v2.size();++index)        cout << setw(10) << v2[index] << endl;   cout << endl << endl;   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } //  program-id         vector3.cpp //  written_by         Don Voils //  date-written       9/30/2006 // //  description:  This program demonstrates the //                constructor where the number of //                elements are specified at definition //                and the values as well. It further //                shows how to insert new elements and //                delete others. // // #include<iostream> #include<iomanip> #include<vector> using namespace std;  void main() {   cout << fixed << setprecision(2);   // Used the vector constructor which specifies the size of the vector   // and then fills it with that number of copies of a data element.   vector<double> v1(6,34.21);   vector<double> v2;   cout << "The vector v1 is defined using the constructor"        << endl << "where you specify the number of elements and then"        << endl << "fill the vector so that all elements are the same number."        << endl << endl << endl << "The size of v1 is:" << (int)v1.size()        << " and the capacity of v1 is: " << (int)v1.capacity() << endl;   cout << endl << "The vector v2 is defined using the default constructor"        << endl << "The size of v2 is:" << (int)v2.size()        << " and the capacity of v2 is: " << (int)v2.capacity() << endl;   cout << endl << "The elements stored in v1 are: "        << endl;   for(short index=0;index< (int)v1.size();++index)     cout << setw(10) << v1[index] << endl;   v1.swap(v2);   cout << endl << endl << "The elements in v1 are swapped with v2" << endl;   cout << endl << endl << "After swaping "        << "the size of v1 is:" << (int)v1.size()        << " and the capacity of v1 is: " << (int)v1.capacity() << endl;   cout << endl << endl << "After swaping "        << "the size of v2 is:" << (int)v2.size()        << " and the capacity of v2 is: "        << (int)v2.capacity() << endl;   // Inserts two new elements into a vector object and then erases two others.   cout << endl << "The number " << 432.32        << " is inserted into position 3"        << endl << "and the number " << 532.33        << " is inserted into postion 5 in the vector v2" << endl;   v2.insert(v2.begin()+2, 432.32);   v2.insert(v2.begin()+4, 532.33);   cout << endl        << "The numbers in position 2 and 5 are erased from the vector v2 "        << endl;   v2.erase(v2.begin()+1);   v2.erase(v2.begin()+4);   cout << endl << "After inserting two elements into v2 and erasing two, "        << "the elements in v2 are: " << endl;   for(short index=0;index< (int)v2.size();++index)        cout << setw(10) << v2[index] << endl;   cout << endl << "Continue? (Y/N)";   char resp;   cin >> resp;   cout << endl << endl; } // program_id    vector4.cpp // written_by    Don Voils // date_written  10/1/2006 // // description   Demonstrates that a vector can be //               created in which the elements are dates. // // #include<iostream> #include<vector> using namespace std; #include"date.h" void main() {   Date date1(10L,15L,2004L);   Date date2(1L,19L,1955L);   Date date3(5L,5L,1957L);   Date date4(9L,13L,1960L);   Date date5(9L,29L,1962L);   Date date6(11L,24L,1934L);   Date date7(9L,7L,1940L);   Date date_array[ ] = {date1, date2, date3, date4, date5, date6};   vector<Date> vector1(date_array, date_array+6);   cout << "The date vector contains the following elements:" << endl;   for(short index=0;index < (int)vector1.size(); ++index)        cout << "   " << vector1[index] << endl;   vector1.insert(vector1.begin()+3,date7);   cout << endl << endl        << "The date vector contains the following elements after an insertion:"        << endl;   for(short index=0;index < (int)vector1.size(); ++index)      cout << "   " << vector1[index] << endl;   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id        vector5.cpp // written_by        Don Voils // date_written      09/30/2006 // // description This program demonstrates that a vector //             may be accessed by using an iterator. // // #include<iostream> #include<vector> using namespace std; #include"date.h" void main() {   Date date1(1L,1L,1970L);   Date date2(1L,19L,1955L);   Date date3(5L,5L,1957L);   Date date4(9L,13L,1960L);   Date date5(9L,29L,1962L);   Date date6(11L,24L,1934L);   Date date7(9L,7L,1940L);   Date date_array[ ] = {date1, date2, date3, date4, date5, date6};   vector<Date> vector1(date_array, date_array+6);   cout << "The date vector contains the following elements:" << endl;   for(short index=0;index < (int)vector1.size(); ++index)     cout << " " << vector1[index] << endl;   cout << endl << "The following is done using an iterator." << endl;   vector1.insert(vector1.begin()+3, date7);   vector<Date>::iterator p;   p = vector1.begin();   cout << endl << endl        << "The date vector contains the following elements after an insertion:"        << endl;   while(p != vector1.end())   {       cout << " " << *p << endl;       ++p;   }   cout << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } 




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net