Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Saturday, September 24, 2011

C++ STL UNARY PREDICATE FUNCTIONS

C++ STL UNARY PREDICATE FUNCTIONS TUTORIAL







REVISED: Sunday, March 3, 2013


CONTENTS:
I.   INTRODUCTION
II.  PREDICATE FUNCTIONS
III. WRITING UNARY PREDICATES

YOU WILL LEARN HOW TO:
1. Write STL unary predicates.
2. Use the STL unary predicate functions with arrays.
3. Use the STL unary predicate functions with lists.
4. Use the STL unary predicate functions with vectors.

• I. INTRODUCTION


Welcome to the “C++ STL Unary Predicate Function Tutorial.”

• II. PREDICATE FUNCTIONS


Predicate functions test the truth or falsity of conditions; for example, an isEmpty function or an isFull function. A binary predicate function takes two arguments, performs a comparison using those two arguments and returns a bool value indicating the result. A unary predicate function takes a single value argument, performs a comparison using that argument and returns a bool value indicating that result.

A. UNARY PREDICATE FUNCTIONS


Examples of the unary predicate functions are: replace_if, remove_if, partition, stable_partition, replace_copy_if, remove_copy_if, count_if, and find_if.


In the STL, instead of seeing unary predicate function you will normally just see the word predicate.


For discussion purposes, lets take a look at the STL count_if( ) function.


The count_if( ) function has three parameters. The first parameter is the starting point. The second parameter is the ending point. The third parameter is the unary predicate which returns the condition you want for inclusion in the count returned by the count_if( ) function.

count_if( ) returns the total of the number of elements that match a unary predicate condition between the starting point and the ending point in a series of elements.

1. ARRAY EXAMPLE


The syntax for instantiating an array object is as follows:

type arrayName [arrayElements];


a. A one-dimensional array name by itself without the elements enclosed by the square brackets is automatically converted to a pointer to the address of the first element of the array. Therefore, the array name A, in the attached array example program, is used for the first parameter.

b. The amount of memory required to store each element of an array is determined by its type. And, all the elements of the array are stored in a contiguous block of memory. Therefore, the sizeof operator is very useful when working with an array. In the const int N = (sizeof(A) / sizeof(A[0])); statement of the following array example program; sizeof( ) divides the number of bytes occupied by the whole A[ ] array by the number of bytes occupied by the first element of the A[ ] array. Because each element in the array occupies the same amount of memory, the result is the number of elements in the A[ ] array. After being computed, the number of elements in the A[ ] array is assigned to the variable N. Using pointer arithmetic, A + N is used for the second parameter.

c. The third parameter is the unary predicate which returns the condition you want for inclusion in the count returned by the count_if( ) function. The attached example program unary predicate function name is isMoreThan30.

The array example program is as follows:

//***********************************
//C++ STL UNARY PREDICATE
//FUNCTIONS TUTORIAL
//Array Example:
//***********************************
#ifndef CALC_ERROR_H
#define CALC_ERROR_H
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>            
#include <string>
#include <vector>
using namespace std;
#endif 
//CALC_ERROR_H
   
void ClearScreen( ); 
//Clears the screen.
void Pause( );            
//Waits for user response.
bool evenlyDivisibleByTwo(int i);  //Returns true if number
//is an even number.

//***********************************
//Clear Screen.
//***********************************
void ClearScreen( )
{
    system( "CLS" );
    cout << endl << endl << endl;
}

//***********************************
//Pause.
//***********************************
void Pause( )
{
    cout << endl << endl << endl;
    cout << "  ";
    system( "PAUSE" );
}

//***********************************
//Unary Predicate Function:
//Returns true if number is more than
//30.
//***********************************
bool isMoreThan30( int value )
{
   return value >30;
}

//***********************************
// MAIN() FUNCTION.
//***********************************
int main(int argc, char* argv[ ])
{
    ClearScreen( );
    int A[ ] = { 9, 12, 32, 18, 4, 96, 5, 42, 33, 10 };
    const int N = (sizeof(A) / sizeof(A[0]));
    cout << endl << endl;

    cout << "  The " << count_if(A, (A + N), isMoreThan30);

    cout << " numbers more than 30 are: ";
    for( int i = 0; i < N; i++ )
    {
        if (isMoreThan30( A[ i ] ))
        {
            cout << A[ i ] << " ";
        }
    }
      Pause( );
    return EXIT_SUCCESS;
}

2. LIST EXAMPLE:


The syntax for instantiating a list object is as follows:

list <type> objectName;


list is a C++ keyword. The angle brackets contain the word type <type> which is a placeholder for the type of the list you are instantiating. objectName is the name of the list object you are instantiating.

When working with a list the count_if( ) function’s three parameters are as follows:

a. objectName.begin( ) sets the list element starting point.

begin( ) is a function that automatically moves the pointer to the first element of the list.

b. objectName.end( ) sets the list element ending point.

end( ) is a function that automatically works with the begin( ) function to establish a range of list elements which will be tested for the condition of the unary predicate. In this case the range is from the first element of the list to the last element of the list.

c. Unary Predicate


The unary predicate is established by the programmer by writing a Boolean function which returns the appropriate condition for the count_if( ) function. The attached list example program unary predicate function name is isDivisibleByThree.

The list example program is as follows:

//**********************************
//C++ STL UNARY PREDICATE
//FUNCTIONS TUTORIAL
//List Example:
//**********************************
#ifndef CALC_ERROR_H
#define CALC_ERROR_H
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <sstream>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>            
#include <string>
#include <vector>
using namespace std;
#endif
//CALC_ERROR_H
   
void ClearScreen( );  
//Clears the screen.
void Pause( );             
//Waits for user response.
bool evenlyDivisibleByTwo(int i);  //Returns true if number is
//an even number.

//**********************************
//Clear Screen.
//**********************************
void ClearScreen( )
{
    system( "CLS" );
    cout << endl << endl << endl;
}

//**********************************
//Pause.
//**********************************
void Pause( )
{
    cout << endl << endl << endl;
    cout << "  ";
    system( "PAUSE" );
}

//**********************************
//Unary Predicate Function:
//Returns true if number is more than
//30.
//**********************************
//bool isMoreThan30( int value )
//{
//   return value > 30;
//}

//**********************************
//Unary Predicate Function:
//Returns true if number is
//divisible by 3.
//**********************************
bool isDivisibleBy3(int i)
{
  if((i%3) == 0) return true;
   
  return false;
}

//**********************************
// MAIN() FUNCTION.
//**********************************
int main(int argc, char* argv[ ])
{
    ClearScreen( );

//Instantiate object myList.
    list<int> myList;

//Insert list elements.
    for ( int i=6; i<=20; ++i )
    {
        myList.push_back( i );
        if ( isDivisibleBy3( i ) )
        {
            cout << "  " << i;
        }
    }

//Instantiate iterator.
    list<int>::iterator myIt;
   
    cout << "  are the " << count_if(myList.begin( ), myList.end( ), isDivisibleBy3);
    cout << " numbers divisible by 3.  ";
 
     Pause( );
    return EXIT_SUCCESS;
}

3. VECTOR EXAMPLE


The syntax for instantiating a vector object is as follows:

vector <type> objectName;


vector is a C++ keyword. The angle brackets contain the word type <type> which is a placeholder for the type of the vector you are instantiating. objectName is the name of the vector object you are instantiating.

When working with a vector the count_if( ) function’s three parameters are as follows:

a. objectName.begin( ) sets the vector element starting point.

begin( ) is a function that automatically moves the pointer to the first element of the vector.

b. objectName.end( ) sets the vector element ending point.

end( ) is a function that automatically works with the begin( ) function to establish a range of vector elements which will be tested for the condition of the unary predicate. In this case the range is from the first element of the vector to the last element of the vector.

c. Unary Predicate


The unary predicate is established by the programmer by writing a Boolean function which returns the appropriate condition for the count_if( ) function. The attached vector example program unary predicate function name is evenlyDivisibleByTwo.

The vector example program is as follows:

//**********************************
//C++ STL PREDICATE
//FUNCTIONS TUTORIAL
//Vector Example:
//**********************************
#ifndef CALC_ERROR_H
#define CALC_ERROR_H
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>            
#include <string>
#include <vector>
using namespace std;
#endif
//CALC_ERROR_H
   
void ClearScreen( ); 
//Clears the screen.
void Pause( );            
//Waits for user response.
bool evenlyDivisibleByTwo(int i);  //Returns true if number
//is an even number.

//**********************************
//Clear Screen.
//**********************************
void ClearScreen( )
{
    system( "CLS" );
    cout << endl << endl << endl;
}

//**********************************
//Pause.
//**********************************
void Pause( )
{
    cout << endl << endl << endl;
    cout << "  ";
    system( "PAUSE" );
}

//********************************** 
//Unary Predicate Function:
//Returns true if number is an even
//number.
//********************************** 
bool evenlyDivisibleByTwo( int i )
{
  return !( i%2 );
}

//********************************** 
// MAIN() FUNCTION.
//********************************** 
int main(int argc, char* argv[ ])
{
    ClearScreen( );

    vector< int > VO;

    int i;

    for( i = 1; i < 40; i++ )
    VO.push_back( i );

    cout << "  The even numbers are: ";
    for(i = 0; i < VO.size( ); i++)
    {
        if (evenlyDivisibleByTwo( VO[ i ] ))
        {
            cout << VO[ i ] << " ";
        }
    }
    cout << endl << endl;

    i = count_if(VO.begin( ), VO.end( ), evenlyDivisibleByTwo);
    cout << "  " << i << " TOTAL" << endl;

    Pause( );

    return EXIT_SUCCESS;
}

• III. WRITING UNARY PREDICATES


The following must be taken into consideration when writing predicates:

A. A unary predicate is called like a function; therefore, a unary predicate must be callable.

B. A unary predicate must accept one argument and return a value that is convertible to Boolean. One argument is supplied when the predicate is called. The return value is used as a conditional expression and must be convertible to type bool.

C. The predicate must not modify its argument. A unary predicate must not modify the elements in the input sequence that it receives as an argument. The unary predicate can inspect the sequence elements, but it must not modify them.

D. Unary predicates logic must not be dependent on the order in which sequence elements are supplied to it.


YOU HAVE LEARNED HOW TO:
1. Write STL unary predicates.
2. Use the STL unary predicate functions with arrays.
3. Use the STL unary predicate functions with lists.
4. Use the STL unary predicate functions with vectors.

Elcric Otto Circle






-->


-->


-->






How to Link to My Home Page
It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"




C++ COMPARISON OF ARRAY, DEQUE, LIST, AND VECTOR...









REVISED: Sunday, March 3, 2013




CONTENTS
I.     ASSUMPTIONS
II.    INTRODUCTION – STL SEQUENCE CONTAINERS VOCABULARY
III.  ARRAY
IV.  DEQUE
V.   LIST
VI.  VECTOR
VII. PARTIAL PROGRAM

YOU WILL LEARN:
1.     STL sequence container vocabulary.
2. Syntax used to declare or instantiate STL sequence containers.
3. Syntax used for STL sequence container file input.
4. How to use operators to compare elements of STL sequence containers.
5. How to display the contents of STL sequence containers.
6. Syntax used to delete all of the elements from STL sequence containers.
7.     How to pick the right STL sequence container for your program application.

I. ASSUMPTIONS


Welcome to the "C++ Comparison of Array, Deque, List, and Vector Tutorial."

This tutorial assumes you understand the fundamentals of the C++ language and are interested in the STL Sequence Containers.

II. INTRODUCTION - STL Sequence Containers Vocabulary

A. What is an array?


An array is not a STL sequence container. However, the array has been included in the tutorial for comparison purposes since everyone who understands the fundamentals of the C++ language also understands arrays.

Arrays are data structures consisting of related data items of the same type.

B. What is a deque?


The STL sequence container deque, pronounced “deck,” provides rapid insertions and deletions at the front or back of the deque and direct access to any element.

The term deque is short for “double-ended que.”


To use a deque you must include the Standard Library header file <deque> in your program.

C. What is a list?


The STL sequence container list is a double linked list which provides rapid insertions and deletions anywhere within the list.

To use a list you must include the Standard Library header file <list> in your program.

D. What is a vector?


The STL sequence container vector provides rapid insertions and deletions at the back of the vector and direct access to any element.

To use a vector you must include the Standard Library header file <vector> in your program.

III. ARRAY


The following syntax is used to declare an array:

type array_name[array_row_size] [array_column_size];


For example, in the attached program we will use:

float a[8][8];


Arrays are C-style pointer-based arrays.

Arrays remain the same size throughout program execution.

The following syntax is normally used to initialize the array to zero:

type array_name = {0};


However, in order to demonstrate the row and column array characteristics of a multidimensional array, in the attached program we used:

//***********************************
//Initialize array to zero.
//***********************************
void Zero(float(&a)[8][8])
{
    for (int row = 0; row <= 7; row++)
    {
        for (int column = 0; column <= 7; column++)
        {
            a[row][column] = 0;
        }
    }
}


The following code is used as a C-style pointer based method for writing a simple array parser. As you examine the array portion of the program compare the array code to the STL sequence containers in order to see the new programming options provided by the STL.

//***********************************
//Reads file into array parser.
//***********************************
void ArrayParser(float(&a)[8][8])
{

//***********************************
    ifstream myfile;
    myfile.open("Parser.dat");

    if (myfile.is_open())
//Checks to see if file is open.
    {
        myfile >> a[0][4];

        myfile.close();
//Closes file.
    }
    else
    {
        if(!myfile)
        {
            cout << "  Unable to open file.\n";
        }
    }

//***********************************
    if((a[0][4]) <= 9)
    {
        (a[0][0]) = (a[0][4]);
//9 or less
    }

//***********************************
    else
        if(((a[0][4]) > 9) && ((a[0][4]) <= 99))
        {
            float loop1 = 0;
            float temp1 = (a[0][4]);

            while(temp1 > 9)
//99 or less
            {
                temp1 = (temp1 - 10);
                ++loop1;
            }                  
            (a[0][1]) = loop1;
            (a[0][0]) = temp1;
        }

//***********************************
        else
            if (((a[0][4]) > 99) && ((a[0][4]) <= 999))
            {
                float loop2 = 0;
                float temp2 = (a[0][4]);

                while(temp2 > 99)
//999 or less
                {
                    temp2 = (temp2 - 100);
                    ++loop2;
                }
                (a[0][2]) = loop2;
                float loop1 = 0;
                float temp1 = temp2;
                while(temp1 > 9)
//99 or less
                {
                    temp1 = (temp1 - 10);
                    ++loop1;
                }
                (a[0][1]) = loop1;
                (a[0][0]) = temp1;
            }

//***********************************
            else
                if (((a[0][4]) > 999) && ((a[0][4]) <= 9999))
                {
                    float loop3 = 0;
                    float temp3 = (a[0][4]);
                    while(temp3 > 999)
//9999 or less
                    {
                        temp3 = temp3 - 1000;
                        ++loop3;
                    }
                    (a[0][3]) = loop3;
                    float loop2 = 0;
                    float temp2 = temp3;
                    while(temp2 > 99)
//999 or less
                    {
                        temp2 = (temp2 - 100);
                        ++loop2;
                    }
                    (a[0][2]) = loop2;
                    float loop1 = 0;
                    float temp1 = temp2;
                    while(temp1 > 9)
//99 or less
                    {
                        temp1 = (temp1 - 10);
                        ++loop1;
                    }
                    (a[0][1]) = loop1;
                    (a[0][0]) = temp1;
                }
}

//***********************************
//Displays array parser elements.
//***********************************
void DisplayArrayParser(float(&a)[8][8])
{
    system("CLS");
    cout << endl << endl << endl;
    cout << "                             ARRAY PARSER "                                        << endl << endl;                                        
    cout << "  The array parser read from a file the random number " <<  a[0][4] << "."             << endl << endl;
    cout << "  The array parser parsed each digit of that number into a separate number:"         << endl << endl;
    cout << "  " << a[0][3] << "  " << a[0][2] << "  " << a[0][1] << "  " << a[0][0]                 << endl << endl;
    cout << "  Reading the parsed numbers from right to left <==  "                                  << endl << endl;
    cout << "  a[0][3] The 4th digit is         " << a[0][3]                                        << endl;
    cout << "  a[0][2] The 3rd digit is         " << a[0][2]                                        << endl;
    cout << "  a[0][1] The 2nd digit is         " << a[0][1]                                        << endl;
    cout << "  a[0][0] The 1st digit is         " << a[0][0]                                        << endl << endl;
    cout << "  a[0][4] The original number is " << a[0][4]                                      << endl << endl;
}


Notice the syntax in the array example used for array file input.

Pay very close attention to the operator code used to compare elements of the array and to make decisions based on those comparisons.

Also notice how the contents of the elements of the array were displayed.

The array example is very straight forward and should be easy to understand.

IV. DEQUE


The following syntax is used to declare or instantiate a deque object:

deque<type> deque_name;


For example, in the attached program we will use:

deque<float> d;


A deque container class represents a double-ended queue supporting random access iteration, is equivalant to a vector, and has the additional capability of being able to add elements to the beginning of the deque.

The following syntax is used to delete all of the deque elements:

deque_name.clear();


For example, in the attached program we use:

d.clear();


The following code is used as a class object based method for writing the deque parser. As you examine the deque portion of the program compare the deque code to the array and look for differences. You will be surprised to see more similarities than differences.

//***********************************
//Reads file into deque parser.
//***********************************
void DequeParser(std::deque<float> & d)
{

//***********************************
    ifstream myfile;
    myfile.open("Parser.dat");
    float num = 0.0;
    float count = 0.0;
    if (myfile.is_open())
//Checks to see if file is open.
    {
        myfile >> num;
        count = sizeof(num);
        d.push_back(num);
//v[0] equals the appended
//nine random numbers.
        myfile.close();
//Closes file.
    }
    else
    {
        if(!myfile)
        {
            cout << "  Unable to open file.\n";
        }
    }

//***********************************
    if(d[0] <= 9)
    {
        d.push_back(d[0]); 
//9 or less  v[1]
    }

//***********************************
    else
        if((d[0] > 9) && (d[0] <= 99))
        {
            float loop1 = 0;
            float temp1 = d[0];
            while(temp1 > 9)                  
            {
                ++loop1;
                temp1 = (temp1 - 10);
            }
            d.push_back(loop1);
//99 or less v[1]
            d.push_back(temp1);
//v[2]
        }

//***********************************
        else
            if ((d[0] > 99) && (d[0] <= 999))
            {
                float loop2 = 0;
                float temp2 = d[0];
                while(temp2 > 99)
//999 or less
                {
                    ++loop2;
                    temp2 = (temp2 - 100);
                }
                d.push_back(loop2);
                float loop1 = 0;
                float temp1 = temp2;
                while(temp1 > 9)
//99 or less
                {
                    ++loop1;
                    temp1 = (temp1 - 10);
                }
                d.push_back(loop1);
//99 or less v[1]
                d.push_back(temp1);
//v[2]
            }

//***********************************
            else
                if ((d[0] > 999) && (d[0] <= 9999))
                {
                    float loop3 = 0;
                    float temp3 = d[0];
                    while(temp3 > 999)
//9999 or less
                    {
                        ++loop3;
                        temp3 = temp3 - 1000;
                    }
                    d.push_back(loop3);
                    float loop2 = 0;
                    float temp2 = temp3;
                    while(temp2 > 99)
//999 or less
                    {
                        ++loop2;
                        temp2 = (temp2 - 100);
                    }
                    d.push_back(loop2);
                    float loop1 = 0;
                    float temp1 = temp2;
                    while(temp1 > 9)
//99 or less
                    {
                        ++loop1;
                        temp1 = (temp1 - 10);
                    }
                    d.push_back(loop1);
                    d.push_back(temp1);
                }
                cout << "  Count equals " << count << endl << endl;
}

//***********************************
//Displays deque parser elements.
//***********************************
void DisplayDequeParser(std::deque<float> & d)
{
    cout << endl << endl << endl;
    cout << "                             DEQUE PARSER "                             << endl << endl;                                        
    cout << "  The deque parser read from a file the random number " <<  d[0] << "."    << endl << endl;
    cout << "  The deque parser parsed each digit of that number into a separate number:" << endl << endl;
    cout << "  " << d[1] << "  " << d[2] << "  " << d[3] << "  " << d[4]                   << endl << endl;
    cout << "  Reading the parsed numbers from right to left <==  "                        << endl << endl;
    cout << "  d[1] The 4th digit is        " << d[1]                                   << endl;
    cout << "  d[2] The 3rd digit is        " << d[2]                                   << endl;
    cout << "  d[3] The 2nd digit is        " << d[3]                                   << endl;
    cout << "  d[4] The 1st digit is        " << d[4]                                   << endl << endl;
    cout << "  d[0] The original number is    " << d[0]                                 << endl << endl;
}

Were you surprised?


Notice the syntax in the deque example used for deque file input. The only major difference is the use of push_back in the deque example.

Pay very close attention to the operator code used to compare elements of the deque and to make decisions based on those comparisons. As you can see the techniques are exactly the same as using an array.

Also notice how the contents of the elements of the array were displayed. The deque display code techniques are exactly the same as using an array.

If you can understand the array example, you have no problems understanding the deque example. The array example and the deque example are almost the same with the exception of deque using push_back.

V. LIST


The following syntax is used to declare or instantiate a list object:

std::list<type>list_name;


For example, in the attached program we will use:

list<float> l;


The list class represents a linked list which supports bidirectional iteration.

The following syntax is used to delete all of the list elements:

list_name.clear();


For example, in the attached program we used:

l.clear();


As before, the following code is used as a class object based method for writing the list parser. As you examine the list portion of the program compare the list code to the array and deque and look for differences. This time you will see differences.

//***********************************
//Reads file into list parser.
//***********************************
void ListParser(std::list<float> & l)
{

//***********************************
    ifstream myfile;
    myfile.open("Parser.dat");
    float num = 0.0;
    float count = 0.0;
    if (myfile.is_open())
//Checks to see if file is open.
    {
        myfile >> num;
        count = sizeof(num);
        l.push_back(num);
//v[0] equals the appended
//nine random numbers.
        myfile.close();
//Closes file.
    }
    else
    {
        if(!myfile)
        {
            cout << "  Unable to open file.\n";
        }
    }

//***********************************
    list<float>::iterator li;
    li = l.begin();
    if(*li <= 9)
    {
        l.push_back(*li);
//9 or less  l[1]
    }

//***********************************
    else
        if((*li > 9) && (*li <= 99))
        {
            float loop1 = 0;
            float temp1 = *li;
            while(temp1 > 9)                  
            {
                ++loop1;
                temp1 = (temp1 - 10);
            }
            l.push_back(loop1);
//99 or less l[1]
            l.push_back(temp1);
//l[2]
        }

//***********************************
        else
            if ((*li > 99) && (*li <= 999))
            {
                float loop2 = 0;
                float temp2 = *li;
                while(temp2 > 99)
//999 or less
                {
                    ++loop2;
                    temp2 = (temp2 - 100);
                }
                l.push_back(loop2);
                float loop1 = 0;
                float temp1 = temp2;
                while(temp1 > 9)
//99 or less
                {
                    ++loop1;
                    temp1 = (temp1 - 10);
                }
                l.push_back(loop1);
//99 or less l[1]
                l.push_back(temp1);
//l[2]
            }

//***********************************
            else
                if ((*li > 999) && (*li <= 9999))
                {
                    float loop3 = 0;
                    float temp3 = *li;
                    while(temp3 > 999)
//9999 or less
                    {
                        ++loop3;
                        temp3 = temp3 - 1000;
                    }
                    l.push_back(loop3);
                    float loop2 = 0;
                    float temp2 = temp3;
                    while(temp2 > 99)
//999 or less
                    {
                        ++loop2;
                        temp2 = (temp2 - 100);
                    }
                    l.push_back(loop2);
                    float loop1 = 0;
                    float temp1 = temp2;
                    while(temp1 > 9)
//99 or less
                    {
                        ++loop1;
                        temp1 = (temp1 - 10);
                    }
                    l.push_back(loop1);
                    l.push_back(temp1);
                }
                cout << "  Count equals " << count << endl << endl;
}

//***********************************
//Displays list parser elements.
//***********************************
void DisplayListParser(std::list<float> & l)
{
    list<float>::iterator li;
    li = l.begin();
    cout << endl << endl << endl;
    cout << "                             LIST PARSER "                                                          << endl << endl;                                        
    cout << "  The list parser read from a file the random number " <<  *li << "."                                   << endl << endl;
    cout << "  The list parser parsed each digit of that number into a separate number:"                           << endl << endl;
    cout << "  "; li++; cout << *li << "  "; li++; cout << *li << "  "; li++; cout << *li << "  "; li++; cout << *li << endl << endl;
    cout << "  Reading the parsed numbers from right to left <==  "                                                  << endl << endl;
    cout << "  l[1] The 4th digit is        "; li = l.begin(); li++; cout << *li                                   << endl;
    cout << "  l[2] The 3rd digit is        "; li++; cout << *li                                                   << endl;
    cout << "  l[3] The 2nd digit is        "; li++; cout << *li                                                   << endl;
    cout << "  l[4] The 1st digit is        "; li++; cout << *li                                                   << endl << endl;
    cout << "  l[0] The original number is    "; li = l.begin(); cout << *li                                         << endl << endl;
}


Notice the syntax in the list example used for list file input. The list and deque file input examples are the same.

Pay very close attention to the operator code used to compare elements of the list and to make decisions based on those comparisons.

The major difference is the use of the

list<float>::iterator li;

iterator in the list example.


Notice how the contents of the elements of the list were displayed. The list display code techniques make use of the

list<float>::iterator li;

iterator in the list example.


The deque example and the list example make similar use of push_back. The list example is a little more complicated in that the list iterator is used. However, the list iterator is logical and easy to understand after examining the list example closely and comparing it to the array and deque examples.

VI. VECTOR


The following syntax is used to declare or instantiate a vector object:

vector<type>vector_name(vector_size);


For example, in the attached program we will use:

vector<float> v(0);


STL vectors are object-based arrays.

Vector supports random access iterator.

A vector container class represents a dynamic array that can change size when required during program execution and supports random access iteration.

The following syntax is used to delete all of the vector elements:

vector_name.clear();


For example, in the attached program we used:

v.clear();


The following code is used as a class object based method for writing the vector parser. As you examine the vector portion of the program compare the vector code to the array, deque and list. Look for differences and similarities.

//***********************************
//Reads file into vector parser.
//***********************************
void VectorParser(std::vector<float> & v)
{
//***********************************
ifstream myfile;
myfile.open("Parser.dat");
float num = 0.0;
float count = 0.0;
if (myfile.is_open())
//Checks to see if file is open.
{
myfile >> num;
count = sizeof(num);
v.push_back(num);
//v[0] equals the appended
//nine random numbers.
myfile.close(); //Closes file.
}
else
{
if(!myfile)
{
cout << " Unable to open file.\n";
}
}

/*
WORK IN PROGRESS.  TO BE CONTINUED...
*/

YOU HAVE LEARNED:
1. STL sequence container vocabulary.
2. Syntax used to declare or instantiate STL sequence containers.
3. Syntax used for STL sequence container file input.
4. How to use operators to compare elements of STL sequence containers.
5. How to display the contents of STL sequence containers.
6. Syntax used to delete all of the elements from STL sequence containers.
7. How to pick the right STL sequence container for your program application.


Elcric Otto Circle




-->

-->

-->





How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"