Thread: vector delete not functioning...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    vector delete not functioning...

    My delete is not working. Why?

    Code:
    struct MatchName
    {
        MatchName(string& searchName) : s_(searchName) {}
        bool operator()(const clsStudent* student) const
        {
            return student->getName() == s_;
        }
        private:
        string s_;
    };
    
    void deleteStudent(vector <clsStudent*>& student)
    {
        string nameToDelete;
        if (!(student.empty()))
        {
            cout << "\nEnter name of student to delete: " << endl;
            cin >> nameToDelete;
            remove_if(student.begin(), student.end(), MatchName(nameToDelete));
            cout << "Student " << nameToDelete << " deleted.";
        }
        else{
            cout << "\nUnable to perform selected action.";
            cout << "\nThere is " << student.size() << " of students to delete\n" ;
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to call the erase member function to erase the element(s) removed. Otherwise, the element(s) will be removed from the vector's range of elements to be used, but will remain as an element of the vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    On a side note shouldn't you verify the student does indeed exist within the vector before attempting to delete it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ewiv
    shouldn't you verify the student does indeed exist within the vector before attempting to delete it?
    That's effectively what remove_if does when used in conjunction with erase.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Quote Originally Posted by laserlight View Post
    That's effectively what remove_if does when used in conjunction with erase.
    Ok, I just looked up erase to expand my own understanding in C++. I see what your saying now. Erase will fix his problem as well as what I was seeing as a possible problem in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete objects from a vector
    By SimplyEvert in forum C++ Programming
    Replies: 11
    Last Post: 02-17-2012, 02:24 AM
  2. Vector Delete Problem
    By tarheelfan_08 in forum C++ Programming
    Replies: 1
    Last Post: 04-18-2010, 01:05 AM
  3. seg fault in vector delete function
    By tytelizgal in forum C Programming
    Replies: 3
    Last Post: 10-18-2008, 04:45 PM
  4. Delete and vector pointer
    By 6tr6tr in forum C++ Programming
    Replies: 18
    Last Post: 04-15-2008, 11:03 AM
  5. Using delete to clear a STL vector
    By starkhorn in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2005, 02:59 AM

Tags for this Thread