Array::fill() and array::swap() in C++ STL?

C++Server Side ProgrammingProgramming

<p>In this section we will see what are the usage of array::fill() and the array::swap() in C++ STL.</p><p>The array::fill() function is used to fill the array with some specified value. Let us see one example to get the idea.</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/1CatMl" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include&lt;iostream&gt; #include&lt;array&gt; using namespace std; main() { &nbsp; &nbsp;array&lt;int, 10&gt; arr = {00, 11, 22, 33, 44, 55, 66, 77, 88, 99}; &nbsp; &nbsp;cout &lt;&lt; &quot;Array elements: &quot;; &nbsp; &nbsp;for(auto it = arr.begin(); it != arr.end(); it++){ &nbsp; &nbsp; &nbsp; cout &lt;&lt; *it &lt;&lt; &quot; &quot;; &nbsp; &nbsp;} &nbsp; &nbsp;//fill array with 5 &nbsp; &nbsp;arr.fill(5); &nbsp; &nbsp;cout &lt;&lt; &quot; Array elements after fill: &quot;; &nbsp; &nbsp;for(auto it = arr.begin(); it != arr.end(); it++){ &nbsp; &nbsp; &nbsp; cout &lt;&lt; *it &lt;&lt; &quot; &quot;; &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">Array elements: 0 11 22 33 44 55 66 77 88 99 Array elements after fill: 5 5 5 5 5 5 5 5 5 5</pre><p>The array::swap() function is used to swap the contents of one array to the other array. Let us see one example to get the idea.</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/oAPNbZ" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include&lt;iostream&gt; #include&lt;array&gt; using namespace std; main() { &nbsp; &nbsp;array&lt;int, 10&gt; arr1 = {00, 11, 22, 33, 44, 55, 66, 77, 88, 99}; &nbsp; &nbsp;array&lt;int, 10&gt; arr2 = {85, 41, 23, 65, 74, 02, 51, 74, 98, 22}; &nbsp; &nbsp;cout &lt;&lt; &quot;Array1 elements: &quot;; &nbsp; &nbsp;for(auto it = arr1.begin(); it != arr1.end(); it++){ &nbsp; &nbsp; &nbsp; cout &lt;&lt; *it &lt;&lt; &quot; &quot;; &nbsp; &nbsp;} &nbsp; &nbsp;cout &lt;&lt; &quot; Array2 elements: &quot;; &nbsp; &nbsp;for(auto it = arr2.begin(); it != arr2.end(); it++){ &nbsp; &nbsp; &nbsp; cout &lt;&lt; *it &lt;&lt; &quot; &quot;; &nbsp; &nbsp;} &nbsp; &nbsp;//swap array elements &nbsp; &nbsp;arr1.swap(arr2); &nbsp; &nbsp;cout &lt;&lt; &quot; Array1 elements (After swap): &quot;; &nbsp; &nbsp;for(auto it = arr1.begin(); it != arr1.end(); it++){ &nbsp; &nbsp; &nbsp; cout &lt;&lt; *it &lt;&lt; &quot; &quot;; &nbsp; &nbsp;} &nbsp; &nbsp;cout &lt;&lt; &quot; Array2 elements (After swap): &quot;; &nbsp; &nbsp;for(auto it = arr2.begin(); it != arr2.end(); it++){ &nbsp; &nbsp; &nbsp; cout &lt;&lt; *it &lt;&lt; &quot; &quot;; &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">Array1 elements: 0 11 22 33 44 55 66 77 88 99 Array2 elements: 85 41 23 65 74 2 51 74 98 22 Array1 elements (After swap): 85 41 23 65 74 2 51 74 98 22 Array2 elements (After swap): 0 11 22 33 44 55 66 77 88 99</pre>
raja
Updated on 30-Jul-2019 22:30:26

Advertisements