Professional Documents
Culture Documents
PRADOSH BANDYOPADHYAY
PRADOSH BANDYOPADHYAY 1
INTRODUCTION
The Standard Template Library (STL) is a set of C++ template
classes to provide common programming data structures and
functions such as lists, stacks, arrays, etc.
It is a library of container classes, algorithms, and iterators. It is a
generalized library and so, its components are parameterized.
Algorithm
Sorting
Searching
Important STL Algorithms
Useful Array algorithms
Partition Operations
PRADOSH BANDYOPADHYAY 3
CONTAINER
Containers or container classes store objects and data.
PRADOSH BANDYOPADHYAY 4
CONTAINER
Sequence Containers: implement data structures which can be
accessed in a sequential manner.
• vector
• list
• deque
• arrays
• forward_list( Introduced in C++11)
PRADOSH BANDYOPADHYAY 5
CONTAINER
Container Adaptors : provide a different interface for sequential
containers.
• queue
• priority_queue
• Stack
Associative Containers : implement sorted data structures that can be
quickly searched (O(log n) complexity).
set
multiset
map
multimap
PRADOSH BANDYOPADHYAY 6
FUNCTION
The STL includes classes that overload the function call operator.
Instances of such classes are called function objects or functors.
Functors allow the working of the associated function to be
customized with the help of parameters to be passed.
Iterators
As the name suggests, iterators are used for working upon a
sequence of values. They are the major feature that allow
generality in STL.
PRADOSH BANDYOPADHYAY 7
VECTOR
// CREATING VECTOR AND LOAD THE VECTOR WITH VALUES
[ include header file: #include <vector> ]
//Declare the vector
vector <int> v1;
PRADOSH BANDYOPADHYAY 8
VECTOR: Printing Vector elements
// OPTION 1
for(int i=0; i<v1.size();i++)
cout<<v1[i]<<"\t";
cout<<"\n";
cout<<"\n";
PRADOSH BANDYOPADHYAY 9
VECTOR: Reverse printing
// OPTION 1
vector <int> ::reverse_iterator riter;
for(riter= v1.rbegin(); riter !=v1.rend(); riter++)
cout<<*riter<<"\t";
// OPTION 2
while(!v1.empty()){
cout << v1.back() << ' ';
v1.pop_back();
}
PRADOSH BANDYOPADHYAY 10
List
Lists are sequence containers that allow non-contiguous memory
allocation.
Header : #include<list>
PRADOSH BANDYOPADHYAY 11
List
// Creating A List for integer Data
for(it=L1.begin();it !=L1.end();it++){
cout<<*it<<"\t";
}
cout<<"\n\n";
[40 10 50 30 20]
PRADOSH BANDYOPADHYAY 13
List: Sorting and printing
// Sort and Printing the List elements
L1.sort();
cout<<"After sorting ...... "<<"\n";
for(it=L1.begin();it !=L1.end();it++){
cout<<*it<<"\t";
}
cout<<"\n\n";
[ 10 20 30 40 50]
PRADOSH BANDYOPADHYAY 14
List: Insert
// insert at front
L1.push_front(70);
// insert at back
L1.push_back(90);
for(it=L1.begin();it !=L1.end();it++){
cout<<*it<<"\t";
}
cout<<"\n\n";
[70 10 20 30 40 50 90]
PRADOSH BANDYOPADHYAY 15
File operations:
// Header used
#include <iostream>
#include <fstream>
#include <string>
PRADOSH BANDYOPADHYAY 16
File operations: Write to file
// Create file
ofstream write ("File1.txt");
// Write to the File
write<<" First line"<<"\n";
write<<" Second Line "<<"\n";
write<<" Test Write operations"<<"\n";
write<<" OOPs With C++"<<"\n";
// Close the file
write.close();
PRADOSH BANDYOPADHYAY 17
File operations: Read from file
// Read line by Line line
string str;
// Creating Read File Objcet
ifstream read("File1.txt");
if(read.is_open()){
while(getline(read,str)){
cout<<str<<"\n";
}//end of while
}//end of if block
//Close the file read object
read.close();
PRADOSH BANDYOPADHYAY 18
Read from file word by word
PRADOSH BANDYOPADHYAY 19
Read from file word by word
if(read1.is_open()){
while(!(read1.eof())){
read1>>readWord;
++count;
cout<<readWord<<"\n";
}//end of while
cout<<"Total words = "<<count<<"\n";
}//end of if block
PRADOSH BANDYOPADHYAY 20
Write object to the File
/* Suppose you have a student class with attribute roll, name
and marks */
PRADOSH BANDYOPADHYAY 21
Write object to the File
//Creating a file writing Object
ofstream writeObject ("File2.txt");
PRADOSH BANDYOPADHYAY 22
Read object to the File
//Creating a file Reading Object
ifstream readObject("File2.txt");
// Read line by line
if(readObject.is_open()){
while(getline(readObject,str)){
cout<<str<<"\n";
}//end of while
}//end of if block
readObject.close(); //close the read object
PRADOSH BANDYOPADHYAY 23