Page 1 of 1

BEGINNER C++ STANDARD TEMPLATE LIBRARY (STL) SEQUENCE CONTAINERS: COMP BEGINNER C++ STANDARD TEMPLATE LIBRARY (STL) SEQUENCE CONTAINERS: COMP Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 19 July 2009 - 10:12 AM

BEGINNER C++ STANDARD TEMPLATE LIBRARY (STL) SEQUENCE CONTAINERS: COMPARISON OF ARRAY, DEQUE, LIST, AND VECTOR TUTORIAL


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

WHAT YOU WILL LEARN IN THIS TUTORIAL

1. You will learn the beginner STL sequence container vocabulary.

2. You will learn the syntax used to declare or instantiate STL sequence containers.

3. You will learn the syntax used for STL sequence container file input.

4. You will learn how to use operators to compare elements of STL sequence containers.

5. You will learn how to display the contents of STL sequence containers.

6. You will learn the syntax used to delete all of the elements from STL sequence containers.

7. You will learn how to pick the right STL sequence container for your program application.

I. ASSUMPTIONS

Hello; nice to meet you. Welcome to the Beginner C++ Standard Template Library (STL) Sequence Containers: 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

1. 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.

2. 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.

3. 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.

4. 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.

[code]
//************************************
//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";
}
}

Is This A Good Question/Topic? 0
  • +

Page 1 of 1