{"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":"<p>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.<br \/>\nHere I am giving a description of STL frequently used by us.<\/p>\n<p>Frequently used container classes of STL :<br \/>\n1.    vector<br \/>\n2.    queue<br \/>\n3.    string<br \/>\n4.    stack<br \/>\n5.    map<br \/>\n6.    list<br \/>\n7.    set<br \/>\n8.    pair<\/p>\n<p><strong>Vector class:<\/strong><br \/>\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<p>To use a vector we must include<br \/>\n[code]<br \/>\n#include &lt;vector&gt;<br \/>\nUsing namespace std;[\/code]<\/p>\n<p>Declaration :<br \/>\n[code]Vector&lt;datatype&gt; c;[\/code]<br \/>\n \/\/Elem refers to all kind of data types like int,float,char,double,string,struct,class,pair etc.<\/p>\n<p><strong>All functions of vector class :<\/strong><br \/>\n*vector  c          \/\/ Creates an empty vector without any elements<br \/>\n*vector  c1(c2)     \/\/ Creates a copy of another vector of the same type (all elements are copied)<br \/>\n*vector  c(n)       \/\/ Creates a vector with n elements that are created by the default constructor<br \/>\n*vector  c(n,elem)  \/\/Creates a vector initialized with n copies of element  elem<br \/>\n*vector  c(beg,end) \/\/Creates a vector initialized with the elements of the range [beg,end)<br \/>\n*c.~vector()              \/\/Destroys all elements and frees the memory<br \/>\n*c.size()                 \/\/Returns the actual number of elements<br \/>\n*c.max_size()             \/\/Returns the maximum number of elements possible<br \/>\n*capacity()               \/\/Returns the maximum possible number of elements without reallocation<br \/>\n*reserve()                \/\/Enlarges capacity, if not enough yet<br \/>\n*c1 == c2                 \/\/Returns whether c1 is equal to c2<br \/>\n*c1 = c2                  \/\/Assigns all elements of c2 to c1<br \/>\n*c.assign(beg,end)        \/\/Assigns the elements of the range [beg,end)<br \/>\n*c1.swap(c2)              \/\/Swaps the data of c1 and c2<br \/>\n*swap(c1,c2)              \/\/ Same (as global function)<br \/>\n*c.at(idx)                \/\/Returns the element with index idx (throws range error exception if idx is out of range)<br \/>\n*c[idx]                   \/\/Returns the element with index idx (no range checking)<br \/>\n*c.front()                \/\/Returns the first element (no check whether a first element exists)<br \/>\n*c.back()                 \/\/Returns the last element (no check whether a last element exists)<br \/>\n*c.insert(pos,elem)       \/*Inserts at iterator position pos a copy of elem and returns the position of the new       element*\/<br \/>\n*c.insert(pos,n,elem)     \/\/Inserts at iterator position pos n copies of elem (returns nothing)<br \/>\n*c.insert(pos,beg,end)    \/*Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing)*\/<br \/>\n*c.insert(pos,beg,end)   \/*Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing)*\/<br \/>\n*c.push_back(elem)       \/\/Appends a copy of elem at the end<br \/>\n*c.pop_back()            \/\/Removes the last element (does not return it)<br \/>\n*c.resize(num)           \/*Changes the number of elements to num (if size() grows, new elements are created by their default constructor)*\/<br \/>\n*c.resize(num,elem)      \/\/ Changes the number of elements to num (if size() grows, new elements are copies of elem)<br \/>\n*c.clear()               \/\/Removes all elements (makes the container empty)<\/p>\n<p><strong>An example:<\/strong><br \/>\n[code]<br \/>\n   #include &lt;iostream&gt;<br \/>\n   #include &lt;vector&gt;<br \/>\n   #include &lt;string&gt;<br \/>\n   #include &lt;algorithm&gt;<br \/>\n   using namespace std;<\/p>\n<p>   int main()<br \/>\n   {<\/p>\n<p>       \/\/create empty vector for strings<br \/>\n       vector&lt;string&gt; sentence;<\/p>\n<p>       \/\/reserve memory for five elements to avoid reallocation<br \/>\n       sentence.reserve(5);<\/p>\n<p>       \/\/append some elements<br \/>\n       sentence.push_back(&quot;Hello,&quot;);<br \/>\n       sentence.push_back(&quot;how&quot;);<br \/>\n       sentence.push_back(&quot;are&quot;);<br \/>\n       sentence.push_back(&quot;you&quot;);<br \/>\n       sentence.push_back(&quot;?&quot;);<\/p>\n<p>       \/\/print elements separated with spaces<br \/>\n       copy (sentence.begin(), sentence.end(),<br \/>\n             ostream_iterator&lt;string&gt;(cout,&quot; &quot;));<br \/>\n       cout &lt;&lt; endl;<\/p>\n<p>       \/\/print &#8221;technical data&#8221;<br \/>\n       cout &lt;&lt; &quot; max_size(): &quot; &lt;&lt; sentence.max_size() &lt;&lt; endl;<br \/>\n       cout &lt;&lt; &quot; size():     &quot; &lt;&lt; sentence.size()     &lt;&lt; endl;<br \/>\n       cout &lt;&lt; &quot; capacity(): &quot; &lt;&lt; sentence.capacity() &lt;&lt; endl;<\/p>\n<p>       \/\/swap second and fourth element<br \/>\n       swap (sentence[1], sentence [3]);<\/p>\n<p>       \/\/insert element &quot;always&quot; before element &quot;?&quot;<br \/>\n       sentence.insert (find(sentence.begin(),sentence.end(),&quot;?&quot;),<br \/>\n                        &quot;always&quot;);<\/p>\n<p>       \/\/assign &quot;!&quot; to the last element<br \/>\n       sentence.back() = &quot;!&quot;;<\/p>\n<p>       \/\/print elements separated with spaces<br \/>\n       copy (sentence.begin(), sentence.end(),<br \/>\n             ostream_iterator&lt;string&gt;(cout,&quot; &quot;));<br \/>\n       cout &lt;&lt; endl;<\/p>\n<p>       \/\/print &quot;technical data&quot; again<br \/>\n       cout &lt;&lt; &quot; max_size(): &quot; &lt;&lt; sentence.max_size() &lt;&lt; endl;<br \/>\n       cout &lt;&lt; &quot; size():     &quot; &lt;&lt; sentence.size()     &lt;&lt; endl;<br \/>\n       cout &lt;&lt; &quot; capacity(): &quot; &lt;&lt; sentence.capacity() &lt;&lt; endl;<\/p>\n<p>   }<br \/>\n\/*The output of the program might look like this:<br \/>\n   Hello, how are you ?<br \/>\n     max_size(): 268435455<br \/>\n     size():     5<br \/>\n     capacity(): 5<br \/>\n   Hello, you are how always !<br \/>\n     max_size(): 268435455<br \/>\n     size():     6<br \/>\n     capacity(): 10<br \/>\n*\/[\/code]<\/p>\n<p><strong>queue Class :<\/strong><br \/>\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<p>To use queue we must include<br \/>\n[code]#include&lt;queue&gt;<br \/>\nusing namespace std;[\/code] <\/p>\n<p>Declaration<br \/>\n[code]queue&lt;datatype&gt;name;[\/code]<\/p>\n<p><strong>Frequently used functions:<\/strong><br \/>\npush(value);<br \/>\nfront();<br \/>\npop();<br \/>\nempty();<\/p>\n<p>[The functions defined in Vector class can be used in queue also,check for further]<\/p>\n<p><strong>An example of queue and deque<\/strong><a href=\"http:\/\/uva.onlinejudge.org\/external\/109\/10935.html\" title=\"Uva 10935\" target=\"_blank\">Uva 10935<\/a><br \/>\n[code]<br \/>\n#include&lt;stdio.h&gt;<br \/>\n#include&lt;string.h&gt;<br \/>\n#include&lt;iostream&gt;<br \/>\n#include&lt;queue&gt;<br \/>\n#include&lt;deque&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main(){<\/p>\n<p>int a,b,n,i,k,c;<\/p>\n<p>while(scanf(&quot;%d&quot;,&amp;n)&amp;&amp;n){<br \/>\n if(n==1){<br \/>\ncout&lt;&lt;&quot;Discarded cards:&quot;&lt;&lt;endl;<br \/>\ncout&lt;&lt;&quot;Remaining card: 1&quot;&lt;&lt;endl;<br \/>\ncontinue;<br \/>\n    }<\/p>\n<p>deque&lt;int&gt;q;<br \/>\nqueue&lt;int&gt;q2;<\/p>\n<p>for(i=1;i&lt;=n;i++){<br \/>\n    q.push_front(i);<br \/>\n    }<\/p>\n<p>do{<br \/>\nk=q.back();<br \/>\nq2.push(k);<br \/>\nq.pop_back();<\/p>\n<p>q.push_front(q.back());<br \/>\nq.pop_back();<br \/>\n}<br \/>\nwhile(q.size()&gt;1);<\/p>\n<p>c=0;<br \/>\ncout&lt;&lt;&quot;Discarded cards:&quot;;<br \/>\nwhile(!q2.empty()){<br \/>\ncout&lt;&lt;&quot; &quot;&lt;&lt;q2.front();<br \/>\nif(c&lt;n-2)<br \/>\n   cout&lt;&lt;&quot;,&quot;;<br \/>\n   c++;<br \/>\nq2.pop();<br \/>\n}<\/p>\n<p>cout&lt;&lt;endl&lt;&lt;&quot;Remaining card:&quot;;<br \/>\nwhile(!q.empty()){<br \/>\ncout&lt;&lt;&quot; &quot;&lt;&lt;q.front()&lt;&lt;endl;<br \/>\nq.pop_front();<br \/>\n }<br \/>\n}<br \/>\nreturn 0;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p><strong>stack Class :<\/strong><br \/>\nstack is one of the data structures that is also called Last-in-First-out [LIFO]. In STL<\/p>\n<p>To use stack we must include<br \/>\n[code]#include&lt;stack&gt;<br \/>\nusing namespace std;[\/code] <\/p>\n<p>Declaration<br \/>\n[code]stack&lt;datatype&gt;name;[\/code]<\/p>\n<p>Frequently used functions:<br \/>\npush(value);<br \/>\ntop();<br \/>\npop();<br \/>\nempty();<br \/>\n[The functions defined in Vector class can be used in stack also,check for further]<br \/>\n<a href=\"http:\/\/uva.onlinejudge.org\/external\/119\/10420.html\" target=\"_blank\">UVA 10420<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.ujjal.net\/?p=53\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A brief collection of STL&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-programming-with-c-2"],"_links":{"self":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=53"}],"version-history":[{"count":0,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ujjal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}