Thread: erase in vector

  1. #16
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Few ideas

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    class name{
    public:
    	name(const std::string& first = "",//constructor
    		const std::string& mid = "",
    		const std::string& last = ""):
    		lastName(last),firstName(first),middleIni(mid){}		
    		
    	bool operator==(const name& theName_)const{//comparison
    		return lastName == theName_.lastName &&
    			   firstName == theName_.firstName &&
    			   middleIni == theName_.middleIni;
    	}
    	
    private:	
    	std::string lastName,//data
    				firstName,
    				middleIni;				
    	friend std::ostream& operator<<(std::ostream& ,const name&);
    };
    
    std::ostream& operator<<(std::ostream& out,const name& myName){//to allow printing
    	out << myName.firstName << " " << myName.middleIni << " ";
    	out << myName.lastName;
    	return out;
    }
    
    
    
    int main(void)
    {
    	std::vector<name> vec;
    	
    	vec.push_back(name("Joe","G","Bloggs"));
    	vec.push_back(name("Frank","D","Drebbin"));
    	vec.push_back(name("John","A","Smith"));
    	vec.push_back(name("Tom","P","Jones"));
    	vec.push_back(name("Ed","D","Wood"));
    	
    	std::copy(vec.begin(),vec.end(),//print vector
    		std::ostream_iterator<name>(std::cout, "\n"));
    	std::cout << vec.size() << " elements in vector" << std::endl;
    		
    	std::cout << std::endl << "Delete \"Jim P Duggan\"" << std::endl;
    	
    	vec.erase(std::remove(vec.begin(),vec.end(),
    		name("Jim","P","Duggan")),vec.end());
    	
    	std::copy(vec.begin(),vec.end(),//print vector
    		std::ostream_iterator<name>(std::cout, "\n"));
    	std::cout << vec.size() << " elements in vector" << std::endl;
    		
    	std::cout << std::endl << "Delete \"Tom P Jones\"" << std::endl;
    	
    	vec.erase(std::remove(vec.begin(),vec.end(),
    		name("Tom", "P", "Jones")),vec.end());;
    	
    	std::copy(vec.begin(),vec.end(),
    		std::ostream_iterator<name>(std::cout, "\n"));
    	std::cout << vec.size() << " elements in vector" << std::endl;
    }

  2. #17
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    STONED Nope.... guess that was the reason huh? Tried that piece and it compiled....

    FORDY I'm still learning classes. The program we had to give in didn't require us to have one. But the next step is to include a save and a load function, which I have already implemented. Since I have done the whole thing, I'm going to re-work it and probably will add a class so your advice will come in handy.

    I'm wondering if I should post my end result to ask for comments and suggestions....

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have mixed feelings about you posting your code...I was going to say "Why not, others can certainly learn from your experience?" But then I thought about that and realized that judging by the boards C++ are all part of some secret society and give every student around the world the same assignments. Go ahead and post the code, but don't post your entire program. You wouldn't want to cheat someone else out of the learning experience that you have been having.

  4. #19
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    MASTER the power you feel when it WORKS!!!! And you (with a little, ok, a LOT of help from some friends) made it work!!!


    I was going to say "Why not, others can certainly learn from your experience?"
    You know? I hope somebody learned something from this thread too.... I know I learn a hell of a lot by searching the threads in here.... answers to questions.... without ever posting..... Imagine that!!!!


    give every student around the world the same assignments
    Why screw with

    Code:
    cout << "Hello World"; ???


    I think the basics will always be the basics.... the assignments will always be very similar.... just a thought...

    Anyway, I understand what you mean about me not posting the code... but if I would like the whole thing to be looked at, and judged, and then flamed....... where would I go???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM