STL stands for STANDARD TEMPLATE LIBRARY in C++ which makes the programmer\u2019s job too easier. STL decreases the coding size as well as the coding time. In programming , using STL is preferred by an C programmer.
\nHere I am giving a description of STL frequently used by us.<\/p>\n
Frequently used container classes of STL :
\n1. vector
\n2. queue
\n3. string
\n4. stack
\n5. map
\n6. list
\n7. set
\n8. pair<\/p>\n
Vector class:<\/strong> To use a vector we must include Declaration : All functions of vector class :<\/strong> An example:<\/strong> int main() \/\/create empty vector for strings \/\/reserve memory for five elements to avoid reallocation \/\/append some elements \/\/print elements separated with spaces \/\/print ”technical data” \/\/swap second and fourth element \/\/insert element "always" before element "?" \/\/assign "!" to the last element \/\/print elements separated with spaces \/\/print "technical data" again } queue Class :<\/strong> To use queue we must include Declaration Frequently used functions:<\/strong> [The functions defined in Vector class can be used in queue also,check for further]<\/p>\n An example of queue and deque<\/strong>Uva 10935<\/a> int main(){<\/p>\n int a,b,n,i,k,c;<\/p>\n while(scanf("%d",&n)&&n){ deque<int>q; for(i=1;i<=n;i++){ do{ q.push_front(q.back()); c=0; cout<<endl<<"Remaining card:"; stack Class :<\/strong> To use stack we must include Declaration Frequently used functions: STL stands for STANDARD TEMPLATE LIBRARY in C++ which makes the programmer\u2019s 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 … <\/p>\n
\nA 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.<\/p>\n
\n[code]
\n#include <vector>
\nUsing namespace std;[\/code]<\/p>\n
\n[code]Vector<datatype> c;[\/code]
\n \/\/Elem refers to all kind of data types like int,float,char,double,string,struct,class,pair etc.<\/p>\n
\n*vector c \/\/ Creates an empty vector without any elements
\n*vector c1(c2) \/\/ Creates a copy of another vector of the same type (all elements are copied)
\n*vector c(n) \/\/ Creates a vector with n elements that are created by the default constructor
\n*vector c(n,elem) \/\/Creates a vector initialized with n copies of element elem
\n*vector c(beg,end) \/\/Creates a vector initialized with the elements of the range [beg,end)
\n*c.~vector() \/\/Destroys all elements and frees the memory
\n*c.size() \/\/Returns the actual number of elements
\n*c.max_size() \/\/Returns the maximum number of elements possible
\n*capacity() \/\/Returns the maximum possible number of elements without reallocation
\n*reserve() \/\/Enlarges capacity, if not enough yet
\n*c1 == c2 \/\/Returns whether c1 is equal to c2
\n*c1 = c2 \/\/Assigns all elements of c2 to c1
\n*c.assign(beg,end) \/\/Assigns the elements of the range [beg,end)
\n*c1.swap(c2) \/\/Swaps the data of c1 and c2
\n*swap(c1,c2) \/\/ Same (as global function)
\n*c.at(idx) \/\/Returns the element with index idx (throws range error exception if idx is out of range)
\n*c[idx] \/\/Returns the element with index idx (no range checking)
\n*c.front() \/\/Returns the first element (no check whether a first element exists)
\n*c.back() \/\/Returns the last element (no check whether a last element exists)
\n*c.insert(pos,elem) \/*Inserts at iterator position pos a copy of elem and returns the position of the new element*\/
\n*c.insert(pos,n,elem) \/\/Inserts at iterator position pos n copies of elem (returns nothing)
\n*c.insert(pos,beg,end) \/*Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing)*\/
\n*c.insert(pos,beg,end) \/*Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing)*\/
\n*c.push_back(elem) \/\/Appends a copy of elem at the end
\n*c.pop_back() \/\/Removes the last element (does not return it)
\n*c.resize(num) \/*Changes the number of elements to num (if size() grows, new elements are created by their default constructor)*\/
\n*c.resize(num,elem) \/\/ Changes the number of elements to num (if size() grows, new elements are copies of elem)
\n*c.clear() \/\/Removes all elements (makes the container empty)<\/p>\n
\n[code]
\n #include <iostream>
\n #include <vector>
\n #include <string>
\n #include <algorithm>
\n using namespace std;<\/p>\n
\n {<\/p>\n
\n vector<string> sentence;<\/p>\n
\n sentence.reserve(5);<\/p>\n
\n sentence.push_back("Hello,");
\n sentence.push_back("how");
\n sentence.push_back("are");
\n sentence.push_back("you");
\n sentence.push_back("?");<\/p>\n
\n copy (sentence.begin(), sentence.end(),
\n ostream_iterator<string>(cout," "));
\n cout << endl;<\/p>\n
\n cout << " max_size(): " << sentence.max_size() << endl;
\n cout << " size(): " << sentence.size() << endl;
\n cout << " capacity(): " << sentence.capacity() << endl;<\/p>\n
\n swap (sentence[1], sentence [3]);<\/p>\n
\n sentence.insert (find(sentence.begin(),sentence.end(),"?"),
\n "always");<\/p>\n
\n sentence.back() = "!";<\/p>\n
\n copy (sentence.begin(), sentence.end(),
\n ostream_iterator<string>(cout," "));
\n cout << endl;<\/p>\n
\n cout << " max_size(): " << sentence.max_size() << endl;
\n cout << " size(): " << sentence.size() << endl;
\n cout << " capacity(): " << sentence.capacity() << endl;<\/p>\n
\n\/*The output of the program might look like this:
\n Hello, how are you ?
\n max_size(): 268435455
\n size(): 5
\n capacity(): 5
\n Hello, you are how always !
\n max_size(): 268435455
\n size(): 6
\n capacity(): 10
\n*\/[\/code]<\/p>\n
\n 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.<\/p>\n
\n[code]#include<queue>
\nusing namespace std;[\/code] <\/p>\n
\n[code]queue<datatype>name;[\/code]<\/p>\n
\npush(value);
\nfront();
\npop();
\nempty();<\/p>\n
\n[code]
\n#include<stdio.h>
\n#include<string.h>
\n#include<iostream>
\n#include<queue>
\n#include<deque>
\nusing namespace std;<\/p>\n
\n if(n==1){
\ncout<<"Discarded cards:"<<endl;
\ncout<<"Remaining card: 1"<<endl;
\ncontinue;
\n }<\/p>\n
\nqueue<int>q2;<\/p>\n
\n q.push_front(i);
\n }<\/p>\n
\nk=q.back();
\nq2.push(k);
\nq.pop_back();<\/p>\n
\nq.pop_back();
\n}
\nwhile(q.size()>1);<\/p>\n
\ncout<<"Discarded cards:";
\nwhile(!q2.empty()){
\ncout<<" "<<q2.front();
\nif(c<n-2)
\n cout<<",";
\n c++;
\nq2.pop();
\n}<\/p>\n
\nwhile(!q.empty()){
\ncout<<" "<<q.front()<<endl;
\nq.pop_front();
\n }
\n}
\nreturn 0;
\n}
\n[\/code]<\/p>\n
\nstack is one of the data structures that is also called Last-in-First-out [LIFO]. In STL<\/p>\n
\n[code]#include<stack>
\nusing namespace std;[\/code] <\/p>\n
\n[code]stack<datatype>name;[\/code]<\/p>\n
\npush(value);
\ntop();
\npop();
\nempty();
\n[The functions defined in Vector class can be used in stack also,check for further]
\nUVA 10420<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"