0% found this document useful (0 votes)
65 views21 pages

Standard Template Library STL

The document discusses the Standard Template Library (STL) in C++. It has three main components: containers, iterators, and algorithms. Containers like lists and vectors are used to store data. Iterators act like pointers to traverse container elements. Common list operations include push_back(), push_front(), sort(), unique(), size(), front(), pop_front(), insert(), and erase(). The document provides examples using lists and their functions.

Uploaded by

Tanzeel Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views21 pages

Standard Template Library STL

The document discusses the Standard Template Library (STL) in C++. It has three main components: containers, iterators, and algorithms. Containers like lists and vectors are used to store data. Iterators act like pointers to traverse container elements. Common list operations include push_back(), push_front(), sort(), unique(), size(), front(), pop_front(), insert(), and erase(). The document provides examples using lists and their functions.

Uploaded by

Tanzeel Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Standard Template Library

STL
Standard Template Library
• Three basic components of STL are:
• containers
• iterator
• algorithm
STL
• Containers
• container are template classes for data storage
• many types of containers exist
• e.g., list for linke list, vector for arrays and many more
• Iterators
• Iterators are pointers of STL
• They are used to point to the individual data items in the container for
reading or writing
• Algorithms
• to be discussed later on
list container and iterator
list<int> intlist;
list<int>::iterator intlistitr;

• Iterators can be initialized using list functions


intlistitr = intlist.begin();
now the iterator point to the first element of the list
• intlistitr++
• it will take the pointer to the next object
• *intlistitr
• dereferencing the pointere to access the data
Useful list member functions
• push_back(data);
• inserts data at the end
• push_front(data);
• inserts data at the front
• sort()
• sort the link list nodes
• unique()
• delete the duplicate values
Example
list<int> int_list;
int_list.push_back(1);
int_list.push_back(1);
int_list.push_back(8);
int_list.push_back(9);
int_list.push_back(7);
int_list.push_back(8);
int_list.push_back(2);
int_list.push_back(3);
int_list.push_back(3);
// 1 1 8 9 7 8 2 3 3
int_list.sort(); // 1 1 2 3 3 7 8 8 9
int_list.unique(); // 1 2 3 7 8 9

list<int>::iterator list_iter;
for(list_iter = int_list.begin(); list_iter != int_list.end(); list_iter++)
cout<<*list_iter<<endl;
list
• size()
• returns the number of items in the list
• front()
• returns the first element
• pop_front()
• deletes the first element
Example
list functions
• insert(position, value);

• erase(position)
STL list
ct and ct1 are list
object
TABLE 5-10 Operations specific to a list (cont’d.)
Reverse iterator
Relational Operator overloaded
STL stack
• STL also provides stack class.
• #include <stack>
• Operations supported are
• size()
• empty()
• push(item)
• top()
• pop()
STL queue
#include <queue>

You might also like