You are on page 1of 23

STL & FILE IMPLEMENTATION

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.

STL has four components


1. Algorithms
2. Containers
3. Functions
4. Iterators
PRADOSH BANDYOPADHYAY 2
ALGORITHM
The header algorithm defines a collection of functions especially
designed to be used on ranges of elements.
Algorithms act on containers. They provide the means by which you
will perform initialization, sorting, searching, and transforming of the
contents of containers.

Algorithm
Sorting
Searching
Important STL Algorithms
Useful Array algorithms
Partition Operations
PRADOSH BANDYOPADHYAY 3
CONTAINER
Containers or container classes store objects and data.

There are in total seven standard “first-class” container classes


and three container adaptor classes and only seven header files
that provide access to these containers or container adaptors.

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;

// Load the vector with values


for(int i =0;i<5;i++)
v1.push_back(i);

PRADOSH BANDYOPADHYAY 8
VECTOR: Printing Vector elements
// OPTION 1
for(int i=0; i<v1.size();i++)
cout<<v1[i]<<"\t";
cout<<"\n";

// OPTION 2: using iterator


vector <int>::iterator it;
for(it = v1.begin();it != v1.end();it++)
cout<<*it<<"\t";

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.

As compared to vector, list has slow traversal, but once a


position has been found, insertion and deletion are quick.

Header : #include<list>

PRADOSH BANDYOPADHYAY 11
List
// Creating A List for integer Data

list <int> L1;


// Populate the List structure with integer
L1.push_back(40);
L1.push_back(10);
L1.push_back(50);
L1.push_back(30);
L1.push_back(20);
PRADOSH BANDYOPADHYAY 12
List: Printing the Data
// Printing the List elements
list <int>::iterator it;

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

// Create a read file object


ifstream read1("File1.txt");
// Define a string type varible
string readWord;
// Define a variable for counting words
int count =0;

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 */

//Creating Student object and show function print the value of


//object
Student S1(10,"Pradosh", 400);
S1.show();

PRADOSH BANDYOPADHYAY 21
Write object to the File
//Creating a file writing Object
ofstream writeObject ("File2.txt");

// Write to the file


writeObject<<S1.getRoll()<<"\t"<<S1.getName()<<"\t"<<S1.getMarks()<<"\n";
// Close Write Object
writeObject.close();

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

You might also like