What's wrong with my vector-intersection function?
<p>I have a function that accepts two vectors v1 and v2. Compares the elements in both of them and is supposed to return the common elements from both. Both vectors have 5 strings in them.</p>

<p>It doesn't work as expected, though. For example, I enter for v1:</p>

<pre><code>dog cat lizard snake pig
</code></pre>

<p>and v2 has:</p>

<pre><code>cat sheep cow snake fish
</code></pre>

<p>The result though is: </p>

<pre><code>snake
</code></pre>

<p>How can I fix it so that the output would look something like the follow?</p>

<pre><code>cat snake
</code></pre>

<p>my code</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
using namespace std;
const int CAPACITY = 5;

template &lt;typename t&gt;
vector &lt;t&gt; inter(const vector &lt;t&gt; &amp; v1, const vector &lt;t&gt; &amp; v2)
{
    vector &lt;t&gt; v3;
    for(int i = 0; v1.size(); i++ )
    {
       for(int j= 0; v2.size(); j++)
       {
            if (v1[i] == v2[j])
            {
                v3.push_back(v1[i]);
            }
       }
    }
    return v3;

}

int main()
{
    vector&lt;string&gt; vec1;
    string a;
    cout &lt;&lt; "Enter five stings for vector 1 \n"&lt;&lt; endl;
    for(int i = 0; i&lt; CAPACITY; i++ )
    {
        cin &gt;&gt; a;
        vec1.push_back(a);
    }
    vector&lt;string&gt; vec2;
    string b;
    cout &lt;&lt; "Enter five stings for vector 2 \n"&lt;&lt; endl;
    for(int i = 0; i&lt; CAPACITY; i++ )
    {
        cin &gt;&gt; b;
        vec2.push_back(b);
    }

    cout&lt;&lt;inter(vec1, vec2);
}
</code></pre>
