A brief collection of STL

STL stands for STANDARD TEMPLATE LIBRARY in C++ which makes the programmer’s job too easier. STL decreases the coding size as well as the coding time. In programming , using STL is preferred by an C programmer.
Here I am giving a description of STL frequently used by us.

Frequently used container classes of STL :
1. vector
2. queue
3. string
4. stack
5. map
6. list
7. set
8. pair

Vector class:
A vector models a dynamic array. Thus, it is an abstraction that manages its elements with a dynamic array.However, note that the standard does not specify that the implementation use a dynamic array. Rather, it follows from the constraints and specification of the complexity of its operation.

To use a vector we must include
[code]
#include <vector>
Using namespace std;[/code]

Declaration :
[code]Vector<datatype> c;[/code]
//Elem refers to all kind of data types like int,float,char,double,string,struct,class,pair etc.

All functions of vector class :
*vector c // Creates an empty vector without any elements
*vector c1(c2) // Creates a copy of another vector of the same type (all elements are copied)
*vector c(n) // Creates a vector with n elements that are created by the default constructor
*vector c(n,elem) //Creates a vector initialized with n copies of element elem
*vector c(beg,end) //Creates a vector initialized with the elements of the range [beg,end)
*c.~vector() //Destroys all elements and frees the memory
*c.size() //Returns the actual number of elements
*c.max_size() //Returns the maximum number of elements possible
*capacity() //Returns the maximum possible number of elements without reallocation
*reserve() //Enlarges capacity, if not enough yet
*c1 == c2 //Returns whether c1 is equal to c2
*c1 = c2 //Assigns all elements of c2 to c1
*c.assign(beg,end) //Assigns the elements of the range [beg,end)
*c1.swap(c2) //Swaps the data of c1 and c2
*swap(c1,c2) // Same (as global function)
*c.at(idx) //Returns the element with index idx (throws range error exception if idx is out of range)
*c[idx] //Returns the element with index idx (no range checking)
*c.front() //Returns the first element (no check whether a first element exists)
*c.back() //Returns the last element (no check whether a last element exists)
*c.insert(pos,elem) /*Inserts at iterator position pos a copy of elem and returns the position of the new element*/
*c.insert(pos,n,elem) //Inserts at iterator position pos n copies of elem (returns nothing)
*c.insert(pos,beg,end) /*Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing)*/
*c.insert(pos,beg,end) /*Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing)*/
*c.push_back(elem) //Appends a copy of elem at the end
*c.pop_back() //Removes the last element (does not return it)
*c.resize(num) /*Changes the number of elements to num (if size() grows, new elements are created by their default constructor)*/
*c.resize(num,elem) // Changes the number of elements to num (if size() grows, new elements are copies of elem)
*c.clear() //Removes all elements (makes the container empty)

An example:
[code]
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{

//create empty vector for strings
vector<string> sentence;

//reserve memory for five elements to avoid reallocation
sentence.reserve(5);

//append some elements
sentence.push_back("Hello,");
sentence.push_back("how");
sentence.push_back("are");
sentence.push_back("you");
sentence.push_back("?");

//print elements separated with spaces
copy (sentence.begin(), sentence.end(),
ostream_iterator<string>(cout," "));
cout << endl;

//print ”technical data”
cout << " max_size(): " << sentence.max_size() << endl;
cout << " size(): " << sentence.size() << endl;
cout << " capacity(): " << sentence.capacity() << endl;

//swap second and fourth element
swap (sentence[1], sentence [3]);

//insert element "always" before element "?"
sentence.insert (find(sentence.begin(),sentence.end(),"?"),
"always");

//assign "!" to the last element
sentence.back() = "!";

//print elements separated with spaces
copy (sentence.begin(), sentence.end(),
ostream_iterator<string>(cout," "));
cout << endl;

//print "technical data" again
cout << " max_size(): " << sentence.max_size() << endl;
cout << " size(): " << sentence.size() << endl;
cout << " capacity(): " << sentence.capacity() << endl;

}
/*The output of the program might look like this:
Hello, how are you ?
max_size(): 268435455
size(): 5
capacity(): 5
Hello, you are how always !
max_size(): 268435455
size(): 6
capacity(): 10
*/[/code]

queue Class :
queue is one of the data structures that is also called First-in-First-out [FIFO]. In STL there are also some built in functions for implementing queue.

To use queue we must include
[code]#include<queue>
using namespace std;[/code]

Declaration
[code]queue<datatype>name;[/code]

Frequently used functions:
push(value);
front();
pop();
empty();

[The functions defined in Vector class can be used in queue also,check for further]

An example of queue and dequeUva 10935
[code]
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<deque>
using namespace std;

int main(){

int a,b,n,i,k,c;

while(scanf("%d",&n)&&n){
if(n==1){
cout<<"Discarded cards:"<<endl;
cout<<"Remaining card: 1"<<endl;
continue;
}

deque<int>q;
queue<int>q2;

for(i=1;i<=n;i++){
q.push_front(i);
}

do{
k=q.back();
q2.push(k);
q.pop_back();

q.push_front(q.back());
q.pop_back();
}
while(q.size()>1);

c=0;
cout<<"Discarded cards:";
while(!q2.empty()){
cout<<" "<<q2.front();
if(c<n-2)
cout<<",";
c++;
q2.pop();
}

cout<<endl<<"Remaining card:";
while(!q.empty()){
cout<<" "<<q.front()<<endl;
q.pop_front();
}
}
return 0;
}
[/code]

stack Class :
stack is one of the data structures that is also called Last-in-First-out [LIFO]. In STL

To use stack we must include
[code]#include<stack>
using namespace std;[/code]

Declaration
[code]stack<datatype>name;[/code]

Frequently used functions:
push(value);
top();
pop();
empty();
[The functions defined in Vector class can be used in stack also,check for further]
UVA 10420

3 Replies to “A brief collection of STL”

Leave a Reply

Your email address will not be published. Required fields are marked *