<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Victor Prince</title>
    <description>The latest articles on DEV Community by Victor Prince (@v1kk1).</description>
    <link>https://dev.to/v1kk1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F415629%2F69e6d14d-9f80-4fef-afda-da3b8ae82e3f.jpeg</url>
      <title>DEV Community: Victor Prince</title>
      <link>https://dev.to/v1kk1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/v1kk1"/>
    <language>en</language>
    <item>
      <title>STL(Standard Template Library)</title>
      <dc:creator>Victor Prince</dc:creator>
      <pubDate>Fri, 03 Jul 2020 21:24:08 +0000</pubDate>
      <link>https://dev.to/v1kk1/stl-standard-template-library-2jlg</link>
      <guid>https://dev.to/v1kk1/stl-standard-template-library-2jlg</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hello fellow devs,&lt;br&gt;
Learning STL is really important for interviews as well as competitive programming in this post we will cover some basic STL🐿.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;⏳Motivation-why should we apply long algorithms when c++ provides it in one line  of code.&lt;/p&gt;

&lt;p&gt;⌛Goal-Learn the basics of STL&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So without much prelude lets  start our STL journey.&lt;br&gt;
This will be the first post in our journey.&lt;br&gt;
So lets start with containers,there are several STL containers like vectors,pairs,maps etc.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;1)VECTORS&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In c++ most of us are familiar with arrays but not with vectors,vectors are nothing but dynamic arrays.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For instance consider we are creating an array of size 'n' but after some lines of code we realise that we need a larger array so we copy the values of the previous array into a larger one of a large size say '4n' imagine this happening again and again what will you do,no worries we have got  🐭vectors🐺.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A vector is dynamically sized that is it increases its size by itself when the value to be stored increases&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The below picture shows the code&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8pqrfyzi8pzaap18oeyu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8pqrfyzi8pzaap18oeyu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please dont forget to include vector&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now lets try printing the values in it&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;For printing the values we can use for loops as well as iterators&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx9gzqobwo6gnuwme0kc6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx9gzqobwo6gnuwme0kc6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This will produce an empty result as we have not added anything to the vector&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;but whatif we want to create a vector with some default values,yes we can do that too&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0u5ev8lb486dsx6svo2e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0u5ev8lb486dsx6svo2e.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
**The output of this will be 5 0`s we can replace 0's with strings,integers etc&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn535cx38ktqflqznekn7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn535cx38ktqflqznekn7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To add elements to a vector push_back(value) method is used usually a macro pb is defined for pushback in competitons to save time&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmwovjti5h5zc3awtxlu8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmwovjti5h5zc3awtxlu8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F347mo78gvte245zx1x61.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F347mo78gvte245zx1x61.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F32iq7htqqatlfgjto1br.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F32iq7htqqatlfgjto1br.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
**The above code also produces the same output.&lt;/p&gt;

&lt;h3&gt;
  
  
  Always keep in mind,when vector is passed as a  parameter to a function a copy is created.It may take a lot of time and memory to create a new vectors,so never give vector as a parameter,but if there is no other option give like shown below
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv053yo5d8kfplprwrotn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv053yo5d8kfplprwrotn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Other features provided by vector includes *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1]vi.size()&lt;br&gt;
2]vi.empty()&lt;br&gt;
3]vi.resize()&lt;br&gt;
4]vi.clear()&lt;br&gt;
5]vi.erase()&lt;br&gt;
try these out by yourselves.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;em&gt;2)PAIRS&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;As the name suggests pairs are use to store pairs of values&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;The code snippet given below demonstrates the use of pairs to store 2 integers&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Pairs is under the &lt;em&gt;utility&lt;/em&gt; header&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1smulj6tbsmngfznzgrv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1smulj6tbsmngfznzgrv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In pairs values can be assigned as shown below&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwy1ey5mhdl3mn76rckxr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwy1ey5mhdl3mn76rckxr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Both of the above code snippets produce the same output **&lt;/strong&gt;'3'.**&lt;br&gt;
&lt;strong&gt;make_pair(values) is used to give values explicitly&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To swap two pairs swap function is used&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;pair1.swap(pair2)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;em&gt;3)SET&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;*&lt;em&gt;Consider we need a container with the following features *&lt;/em&gt;&lt;br&gt;
1]add an element but no duplicates.&lt;br&gt;
2]remove elements&lt;br&gt;
3]get count&lt;br&gt;
4]check whether elements are present&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;c++ provides 'set' for this purpose&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;It is under the 'set' header&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcllkg5x5byhhlefqejr0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcllkg5x5byhhlefqejr0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;As you have noticed iterator is used to traverse through a&lt;/strong&gt;set**&lt;br&gt;
&lt;strong&gt;Sets wont contain duplicates&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Although we are inserting 100 times the value 1 to the set it will only contain a single '1'&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdu2a1xcphc1diesu7v6e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdu2a1xcphc1diesu7v6e.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set also has a find element which  searches for an element in O(logn)time complexity.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;if the element is not present the end() iterator is returned.&lt;/strong&gt;&lt;br&gt;
** Dont confuse this with the normal find function which locates an element in O(n)time complexity **&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;4)MAPS&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;This is the last container we will be discussing about in this note &lt;br&gt;
Actually map is verymuch like set ,except it contains not just values but pairs.Map ensures that at most one pair with specific key exists.It also has [] operator defined.&lt;/p&gt;

&lt;p&gt;Its under the map header.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>cpp</category>
      <category>anonymous</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
