OODP UNIT 05
07 December 2022 04:06 AM
1. STL CONTAINERS:
• STL stands for Standard Template Library
• In 1994, STL was adopted as a part of ANSI & ISO standard C++
• So, they developed a set of general purpose templatized classes (data structures) and
functions (algorithms) that could be used as a standard approach for storing and processing
data
• Therefore, STL is a collection of generic classes and functions.
• The components of STL are now a part of standard c++ library defined in the namespace std
• STL contains - Containers, Algorithms, Iterators
• Algorithms use iterators to interact with objects stored in containers
• Containers - a way that stored data is organised in memory (as array)
• Algorithms - procedures applied to containers to process their data (search, sort, merge, copy)
• Iterators - generalization of concept of pointers (incremented to point to next element in
array)
CONTAINERS:
• Containers are a way that stored data is organised in the memory.
• Data in memory is organised in the form of arrays.
• There are mainly three types of STL containers:
SEQUENCE CONTAINERS:
• Can store elements is a linear sequence
• Each element is related to other elements by its position along its line
, • STL provides 3 types of sequence containers
○ Vector
○ List
○ Deque
VECTOR:
• Syntax: vector<of what>
• Acts as an alternative for built in array function
• A vector is self grown
• Vector container is similar to array with an exception that it automatically manages it's storage
size
Example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v; //create a vector of ints
v.push_back(10); //put values at end of array
v.push_back(11);
v.push_back(12);
v.push_back(13);
v[0] = 20; //replace with new values
v[3] = 23;
for(int j=0; j<v.size(); j++) //display vector contents
cout << v[j] << ‘ ‘; //20 11 12 23
cout << endl;
return 0;
}
LIST:
• Syntax: std::list<type> larg;
• An STL list container is a doubly linked list, in which each element contains a pointer not only
to the next element but also to the preceding one. The container stores the address of both
the front (first) and the back (last) elements, which makes for fast access to both ends of the
list.
• Lists are appropriate when you will make frequent insertions and deletions in the middle of
the list.
• Not suitable for random access , only vector or deque is suitable for random access
Example:
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> l = {1,2,3,4,5};
list<int>::iterator it = l.begin();
l.insert (it+1, 100); // insert 100 before 2 position
/* now the list is 1 100 2 3 4 5 */
list<int> new_l = {10,20,30,40}; // new list new_l.insert (new_l.begin() , l.begin(), l.end());
/* insert elements from beginning of list l to end of list l
before 1 position in list new_l */
07 December 2022 04:06 AM
1. STL CONTAINERS:
• STL stands for Standard Template Library
• In 1994, STL was adopted as a part of ANSI & ISO standard C++
• So, they developed a set of general purpose templatized classes (data structures) and
functions (algorithms) that could be used as a standard approach for storing and processing
data
• Therefore, STL is a collection of generic classes and functions.
• The components of STL are now a part of standard c++ library defined in the namespace std
• STL contains - Containers, Algorithms, Iterators
• Algorithms use iterators to interact with objects stored in containers
• Containers - a way that stored data is organised in memory (as array)
• Algorithms - procedures applied to containers to process their data (search, sort, merge, copy)
• Iterators - generalization of concept of pointers (incremented to point to next element in
array)
CONTAINERS:
• Containers are a way that stored data is organised in the memory.
• Data in memory is organised in the form of arrays.
• There are mainly three types of STL containers:
SEQUENCE CONTAINERS:
• Can store elements is a linear sequence
• Each element is related to other elements by its position along its line
, • STL provides 3 types of sequence containers
○ Vector
○ List
○ Deque
VECTOR:
• Syntax: vector<of what>
• Acts as an alternative for built in array function
• A vector is self grown
• Vector container is similar to array with an exception that it automatically manages it's storage
size
Example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v; //create a vector of ints
v.push_back(10); //put values at end of array
v.push_back(11);
v.push_back(12);
v.push_back(13);
v[0] = 20; //replace with new values
v[3] = 23;
for(int j=0; j<v.size(); j++) //display vector contents
cout << v[j] << ‘ ‘; //20 11 12 23
cout << endl;
return 0;
}
LIST:
• Syntax: std::list<type> larg;
• An STL list container is a doubly linked list, in which each element contains a pointer not only
to the next element but also to the preceding one. The container stores the address of both
the front (first) and the back (last) elements, which makes for fast access to both ends of the
list.
• Lists are appropriate when you will make frequent insertions and deletions in the middle of
the list.
• Not suitable for random access , only vector or deque is suitable for random access
Example:
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> l = {1,2,3,4,5};
list<int>::iterator it = l.begin();
l.insert (it+1, 100); // insert 100 before 2 position
/* now the list is 1 100 2 3 4 5 */
list<int> new_l = {10,20,30,40}; // new list new_l.insert (new_l.begin() , l.begin(), l.end());
/* insert elements from beginning of list l to end of list l
before 1 position in list new_l */