Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:22:47+00:00 2026-05-28T14:22:47+00:00

My remove_if seems to be overwriting the elements that are not filtered out with

  • 0

My remove_if seems to be overwriting the elements that are not filtered out with values of filtered out elements. The purpose of these code is to allow user to filter and display only teacher from a certain category. (Not deleting any element)
Here are some of the code

static string compare;
static string debug;

bool filter_Cat (Teacher &t) 
{ 
    return (t.getCat() != compare); 
}

void filterCat (vector<Teacher> &t)
{
   vector<Teacher>::iterator i;
   vector<Teacher>::iterator newedited = remove_if(t.begin(), t.end(), filter_Cat);
   for (i = t.begin(); i != newedited; ++i)
   {
     Teacher& te = *i;
     te.getName();
     cout << "\t";
     te.getCategory();
     cout << "\t";
     te.getLocation();
   }
 }

 void filterTutorCat(vector<Teacher> &t)
 {
    int choice;
    cout << "No\tCategory" << endl
         << "1\tEnglish" << endl
         << "2\tMath" << endl
         << "3\tScience" << endl
         << "Choose the category you wish to filter :";
    cin >> choice;
    getline(cin, debug);

    if(choice <= 3 && choice > 0)
    {
        if (choice == 1)
        {
          compare = "English";
          filterCat(t);
        }
        if (choice == 2)
        {
          compare = "Math";
          filterCat(t);
        }
        if (choice == 3)
        {
          compare = "Science";
          filterCat(t);
        }

    }
    else
    {
        cout << "Invalid Option" << endl;
    }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:22:48+00:00Added an answer on May 28, 2026 at 2:22 pm

    remove_if 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.

    This reads,

    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.

    Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten. 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.

    So what you want to do should be expressed as:

    void filterCat (vector<Teacher> &v)
    {
       for (vector<Teacher>::iterator it = v.begin(); it != v.end() ; ++it)
       {
          if (!filter_Cat(*i))
          {
               std::cout << i->getName() <<"\t" << i->getCategory() << std::endl;
          }
       }
     }
    

    It seems in your code, getName() 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 getCategory as well. Choose your name correctly. If it is getName(), you should get you name by returning it; if it is printName(), then it should print name.


    Also, the code which you’ve written isn’t good:

    • You should avoid global variables.
    • You should avoid if-else as much as possible. Learn better ways.
    • You should learn about function objects (or functor)
    • You should learn about const member function.
    • You should understand the difference between iterator and const_iterator, and their usage.
    • You should understand the difference between const reference, and non-const reference. And try using them appropriately.

    So I would write your code as:

    //this is functor, not a function
    struct filter_cat
    {
       std::string m_cat; //use member data, avoid global variable
       filter_cat(std::string const & cat) : m_cat(cat) {}
       bool operator()(Teacher const & t) const  //const member function
       { 
         return (t.getCat() != m_cat); //getCat should be const member function
       }
    };
    
    //pass vector by const reference
    void filterCat (vector<Teacher> const & v, filter_cat filter)
    {
       //use const_iterator here, instead of iterator 
       for (vector<Teacher>::const_iterator it = v.begin(); it != v.end() ; ++it)
       {
          if (!filter(*i))
          {
               //getName and getCategory should be const member function
               std::cout << i->getName() <<"\t" << i->getCategory() << std::endl;
          }
       }
    }
    
    void filterTutorCat(vector<Teacher> const &t)
    {
        int choice;
        cout << "No\tCategory" << endl
             << "1\tEnglish" << endl
             << "2\tMath" << endl
             << "3\tScience" << endl
             << "Choose the category you wish to filter :";
        cin >> choice;
        getline(cin, debug);
    
        //avoid if-else as much as possible, learn better ways!
        std::string cats[] = {"English", "Math", "Science"};
    
        if(choice <= 3 && choice > 0)
        {
              filterCat(v, filter_cat(cats[choice-1]));
        }
        else
        {
            cout << "Invalid Option" << endl;
        }
    }
    

    As noted in the comments: getCat, getName and getCategory should be const member functions. In fact, if getCategory returns category, then getCat isn’t even needed.

    Solved my issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remove some elements from a vector and am using remove_if algorithm
So here's a snippet of my code. void RoutingProtocolImpl::removeAllInfinity() { dv.erase(std::remove_if(dv.begin(), dv.end(), hasInfCost), dv.end());
I've read a number of articles that point out how to do this, namely:
I'm running into an odd failure. It seems that if I have both an
I'm supposed to implement a function that erases a range of values from containers.
Any ideas regarding why the WPF XAML code I have is not working. I'm
I would like to disable client side validation on certain fields in my user
I am using the split view template to create a simple split view that
I am creating a class that derives from the WPF RichTextBox control and I
I have several scripts that take as input a directory name, and my program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.