<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>changing/acess a vector's element | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/changing-acess-a-vectors-element/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Wed, 01 Feb 2023 09:58:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>C++ Vectors-Part II</title>
		<link>https://studentprojects.in/software-development/cpp/c-vectors-part-ii/</link>
					<comments>https://studentprojects.in/software-development/cpp/c-vectors-part-ii/#respond</comments>
		
		<dc:creator><![CDATA[Shubhajna Rai]]></dc:creator>
		<pubDate>Thu, 09 Feb 2023 09:47:02 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ vectors]]></category>
		<category><![CDATA[changing/acess a vector&#039;s element]]></category>
		<guid isPermaLink="false">https://studentprojects.in/?p=10156</guid>

					<description><![CDATA[<p>E. Changing/Accessing a Vector&#8217;s Elements Any element can be changed at any place, which is the same as accessing them. The same way that it was done with arrays, any element at any given place may be accessed by its index number. Output: The at() method, which accepts the index number as an argument and</p>
<p>The post <a href="https://studentprojects.in/software-development/cpp/c-vectors-part-ii/">C++ Vectors-Part II</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<h4>E. Changing/Accessing a Vector&#8217;s Elements</h4>



<p>Any element can be changed at any place, which is the same as accessing them. The same way that it was done with arrays, any element at any given place may be accessed by its index number.</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">vector&lt;int&gt; v = {1, 2, 3, 4};
cout &lt;&lt; "Element at index 0 is " &lt;&lt; v[0] &lt;&lt; endl;</code></pre>



<p>Output:</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">Element at index 0 is 1</code></pre>



<p>The at() method, which accepts the index number as an argument and returns the element at that place, is another method that gets added for vectors.</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">vector&lt;int&gt; v = {1, 2, 3, 4};
cout &lt;&lt; "Element at index 2 is " &lt;&lt; v.at(2) &lt;&lt; endl;</code></pre>



<p>Output:</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">Element at index 2 is 3</code></pre>



<h4>F. Element removal from a vector</h4>



<p>A vector&#8217;s elements can be removed in a variety of methods. When we use the pop back method to remove an element from the tail of a vector, we do so in the most cost-effective way possible. The element is popped from the back using the pop back() function, which requires no parameters.</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;
 
void display(vector&lt;int&gt; v)
{
    cout &lt;&lt; "The elements are: ";
    for (auto it : v)
    {
        cout &lt;&lt; it &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
}
 
int main()
{
    vector&lt;int&gt; v = {1, 2, 3, 4};
    display(v);
    v.pop_back(); //4 gets popped from the back
    display(v);
}</code></pre>



<p>Output:</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">The elements are: 1 2 3 4 
The elements are: 1 2 3</code></pre>



<p>Another method to remove an element allows us to remove it from any random position. Here, we use the erase() method. The syntax for using the erase method is</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">vector_name.erase(iterator);</code></pre>



<p>The iterator in this case is a pointer to the location where the element is popped. Here&#8217;s an illustration of how to utilise the wipe() method:</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;
 
void display(vector&lt;int&gt; v)
{
    cout &lt;&lt; "The elements are: ";
    for (auto it : v)
    {
        cout &lt;&lt; it &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
}
 
int main()
{
    vector&lt;int&gt; v = {1, 2, 3, 4};
    display(v);
    v.erase(v.begin()); 
    display(v);
}</code></pre>



<p>Output:</p>



<pre class="wp-block-code"><code lang="cpp" class="language-cpp">The elements are: 1 2 3 4 
The elements are: 2 3 4</code></pre><p>The post <a href="https://studentprojects.in/software-development/cpp/c-vectors-part-ii/">C++ Vectors-Part II</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/cpp/c-vectors-part-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
