Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/default-filters.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/default-filters.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-theme.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-theme.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-styles.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-styles.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/block-supports/duotone.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/block-supports/duotone.php on line 1

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768
{"id":53,"date":"2011-04-05T10:27:33","date_gmt":"2011-04-05T04:27:33","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=53"},"modified":"2011-04-05T10:27:33","modified_gmt":"2011-04-05T04:27:33","slug":"stl-2","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=53","title":{"rendered":"A brief collection of STL"},"content":{"rendered":"

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>
\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

To use a vector we must include
\n[code]
\n#include <vector>
\nUsing namespace std;[\/code]<\/p>\n

Declaration :
\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

All functions of vector class :<\/strong>
\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

An example:<\/strong>
\n[code]
\n #include <iostream>
\n #include <vector>
\n #include <string>
\n #include <algorithm>
\n using namespace std;<\/p>\n

int main()
\n {<\/p>\n

\/\/create empty vector for strings
\n vector<string> sentence;<\/p>\n

\/\/reserve memory for five elements to avoid reallocation
\n sentence.reserve(5);<\/p>\n

\/\/append some elements
\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

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

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

\/\/swap second and fourth element
\n swap (sentence[1], sentence [3]);<\/p>\n

\/\/insert element "always" before element "?"
\n sentence.insert (find(sentence.begin(),sentence.end(),"?"),
\n "always");<\/p>\n

\/\/assign "!" to the last element
\n sentence.back() = "!";<\/p>\n

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

\/\/print "technical data" again
\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

queue Class :<\/strong>
\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

To use queue we must include
\n[code]#include<queue>
\nusing namespace std;[\/code] <\/p>\n

Declaration
\n[code]queue<datatype>name;[\/code]<\/p>\n

Frequently used functions:<\/strong>
\npush(value);
\nfront();
\npop();
\nempty();<\/p>\n

[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>
\n[code]
\n#include<stdio.h>
\n#include<string.h>
\n#include<iostream>
\n#include<queue>
\n#include<deque>
\nusing namespace std;<\/p>\n

int main(){<\/p>\n

int a,b,n,i,k,c;<\/p>\n

while(scanf("%d",&n)&&n){
\n if(n==1){
\ncout<<"Discarded cards:"<<endl;
\ncout<<"Remaining card: 1"<<endl;
\ncontinue;
\n }<\/p>\n

deque<int>q;
\nqueue<int>q2;<\/p>\n

for(i=1;i<=n;i++){
\n q.push_front(i);
\n }<\/p>\n

do{
\nk=q.back();
\nq2.push(k);
\nq.pop_back();<\/p>\n

q.push_front(q.back());
\nq.pop_back();
\n}
\nwhile(q.size()>1);<\/p>\n

c=0;
\ncout<<"Discarded cards:";
\nwhile(!q2.empty()){
\ncout<<" "<<q2.front();
\nif(c<n-2)
\n cout<<",";
\n c++;
\nq2.pop();
\n}<\/p>\n

cout<<endl<<"Remaining card:";
\nwhile(!q.empty()){
\ncout<<" "<<q.front()<<endl;
\nq.pop_front();
\n }
\n}
\nreturn 0;
\n}
\n[\/code]<\/p>\n

stack Class :<\/strong>
\nstack is one of the data structures that is also called Last-in-First-out [LIFO]. In STL<\/p>\n

To use stack we must include
\n[code]#include<stack>
\nusing namespace std;[\/code] <\/p>\n

Declaration
\n[code]stack<datatype>name;[\/code]<\/p>\n

Frequently used functions:
\npush(value);
\ntop();
\npop();
\nempty();
\n[The functions defined in Vector class can be used in stack also,check for further]
\n
UVA 10420<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

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