Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>remove_if</code> shifts elements, for which the compare function returns false, from right to left; which in other words means, it overwrites the elements, for which compare returns true, with elements, for which compare returns false. The size of the vector doesn't change, however.</p> <p><a href="http://en.cppreference.com/w/cpp/algorithm/remove" rel="nofollow">This reads</a>,</p> <blockquote> <p>Removes all elements satisfying specific criteria from the range [first, last). The first version removes all elements that are equal to value, the second version removes all elements for which predicate p returns true.</p> <p><strong>Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten.</strong> The elements between the old and the new ends of the range have unspecified values. Iterator to the new end of the range is returned. Relative order of the elements that remain is preserved.</p> </blockquote> <p>So what you want to do should be expressed as:</p> <pre><code>void filterCat (vector&lt;Teacher&gt; &amp;v) { for (vector&lt;Teacher&gt;::iterator it = v.begin(); it != v.end() ; ++it) { if (!filter_Cat(*i)) { std::cout &lt;&lt; i-&gt;getName() &lt;&lt;"\t" &lt;&lt; i-&gt;getCategory() &lt;&lt; std::endl; } } } </code></pre> <p>It seems in your code, <code>getName()</code> prints the name which ideally it should not do, instead it should return name. So I would suggest you to change it to make it return name. And do the same for <code>getCategory</code> as well. Choose your name correctly. If it is <code>getName()</code>, you should <em>get</em> you name by returning it; if it is <code>printName()</code>, then it should <em>print</em> name.</p> <hr> <p>Also, the code which you've written isn't good:</p> <ul> <li>You should avoid global variables.</li> <li>You should avoid if-else as much as possible. Learn better ways.</li> <li>You should learn about function objects (or functor)</li> <li>You should learn about <code>const</code> member function.</li> <li>You should understand the difference between <code>iterator</code> and <code>const_iterator</code>, and their usage.</li> <li>You should understand the difference between const reference, and non-const reference. And try using them appropriately.</li> </ul> <p>So I would write your code as:</p> <pre><code>//this is functor, not a function struct filter_cat { std::string m_cat; //use member data, avoid global variable filter_cat(std::string const &amp; cat) : m_cat(cat) {} bool operator()(Teacher const &amp; t) const //const member function { return (t.getCat() != m_cat); //getCat should be const member function } }; //pass vector by const reference void filterCat (vector&lt;Teacher&gt; const &amp; v, filter_cat filter) { //use const_iterator here, instead of iterator for (vector&lt;Teacher&gt;::const_iterator it = v.begin(); it != v.end() ; ++it) { if (!filter(*i)) { //getName and getCategory should be const member function std::cout &lt;&lt; i-&gt;getName() &lt;&lt;"\t" &lt;&lt; i-&gt;getCategory() &lt;&lt; std::endl; } } } void filterTutorCat(vector&lt;Teacher&gt; const &amp;t) { int choice; cout &lt;&lt; "No\tCategory" &lt;&lt; endl &lt;&lt; "1\tEnglish" &lt;&lt; endl &lt;&lt; "2\tMath" &lt;&lt; endl &lt;&lt; "3\tScience" &lt;&lt; endl &lt;&lt; "Choose the category you wish to filter :"; cin &gt;&gt; choice; getline(cin, debug); //avoid if-else as much as possible, learn better ways! std::string cats[] = {"English", "Math", "Science"}; if(choice &lt;= 3 &amp;&amp; choice &gt; 0) { filterCat(v, filter_cat(cats[choice-1])); } else { cout &lt;&lt; "Invalid Option" &lt;&lt; endl; } } </code></pre> <p>As noted in the comments: <code>getCat</code>, <code>getName</code> and <code>getCategory</code> should be const member functions. In fact, if <code>getCategory</code> returns category, then <code>getCat</code> isn't even needed.</p> <p>Solved my issue.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload