remove() not working ?

This is a discussion on remove() not working ? within the C++ Programming forums, part of the General Programming Boards category; I've included <algorithm>... Code: void operator-(double number){ remove(doublevector.begin(), doublevector.end(), number); } normally it should remove all elements that are equal ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    27

    remove() not working ?

    I've included <algorithm>...

    Code:
    void operator-(double number){
    remove(doublevector.begin(), doublevector.end(), number);
    }
    normally it should remove all elements that are equal to the number from the vector ... I don^t get any compile errors but it seemply doesn't remove them.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    float data type question
    floats and doubles are approximations.
    Getting actual equality is much harder than you might imagine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    yeah but replace()

    with double works...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Post a complete compilable example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    Ok obviously remove() works but my problem is that the vector seems to occupy the same memory as before after remove ..
    for some weird reason doublevector.end() doesn't change after remove()...

    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    
    int main(){
    
      vector <double> doublevector;
      vector <double> :: iterator theiterator;
     
    
    
      doublevector.push_back(5.67); 
      doublevector.push_back(5.67); 
      
      doublevector.push_back(-0.45); 
      doublevector.push_back(-0.45); 
    
      doublevector.push_back(15);
    
      //Prints the vector
      for  (theiterator=doublevector.begin(); theiterator != doublevector.end();theiterator++)
        cout<<"\t"<<*theiterator; 
      cout<<endl;
    
      //should remove all numbers that equal 5.67 from vector
      remove(doublevector.begin(),doublevector.end(), 5.67); 
    
      //replaces all numbers that equal -0.45 with -0.55
      replace(doublevector.begin(),doublevector.end(),-0.45,-0.55); 
    
      //Prints the vector
      for  (theiterator=doublevector.begin(); theiterator != doublevector.end();theiterator++)
        cout<<"\t"<<*theiterator; 
      cout<<endl;
    
    
      return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    You have to use the erase() member function to actually erase the elements removed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    yeah but then remove() is useless ?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    yeah but then remove() is useless ?
    Of course not. Read SGI's Standard Template Library Programmer's Guide. Still as Salem mentioned, simple comparison with floating point numbers can be problematic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    why of course not... if i remove an element and i can access it later what is the use of it... ?

    and you get kinda lost on the site you gave me..

    maybe someone else has used remove() efficiently ?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    if i remove an element and i can access it later what is the use of it
    That's where the erase() member function comes into play. remove() works on a range denoted by an iterator pair, hence the container itself is left unchanged (only the container elements are swapped around).

    You could consider something like:
    Code:
    #include <algorithm>
    #include <functional>
    #include <cmath>
    #include <vector>
    #include <iostream>
    
    struct IsApprox : std::binary_function<double, double, bool> {
    	bool operator()(double lhs, double rhs) const {
    		return fabs(lhs - rhs) < 0.001;
    	}
    };
    
    int main() {
    	std::vector<double> doublevector;
    	doublevector.push_back(5.67);
    	doublevector.push_back(5.67);
    	doublevector.push_back(-0.45);
    	doublevector.push_back(-0.45);
    	doublevector.push_back(15.0);
    
    	for (std::vector<double>::iterator i = doublevector.begin();
    		i != doublevector.end(); ++i) {
    			std::cout << *i << " ";
    	}
    	std::cout << "\n";
    
    	doublevector.erase(
    		std::remove_if(
    			doublevector.begin(),
    			doublevector.end(),
    			std::bind2nd(IsApprox(), 5.67)),
    		doublevector.end());
    
    	for (std::vector<double>::iterator i = doublevector.begin();
    		i != doublevector.end(); ++i) {
    			std::cout << *i << " ";
    	}
    	std::cout << "\n";
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    why of course not... if i remove an element and i can access it later what is the use of it... ?
    Speed. Erasing elements isn't necessary.

    Code:
    vector <double> :: iterator theNewEnd;
    theNewEnd = remove(doublevector.begin(),doublevector.end(), 5.67); 
    
    //Prints the vector
      for  (theiterator=doublevector.begin(); theiterator != theNewEnd; ++theiterator)
        cout<<"\t"<<*theiterator; 
      cout<<endl;
    Last edited by 7stud; 04-30-2006 at 11:55 AM.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    wow i am stunned i didn^t know you could do that in c++ ... !

    Code:
    doublevector.erase(
    		std::remove_if(
    			doublevector.begin(),
    			doublevector.end(),
    			std::bind2nd(IsApprox(), 5.67)),
    		doublevector.end());
    so if erase() didn't find anything to erase how do i check that ?

    let's say i have

    Code:
    while (doublevector.erase(remove(doublevector.begin(), doublevector.end(),9)) != ??? );
    with what do i replace the ???

    thanks a ton guys.. this has been really helpful so far

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    so if erase() didn't find anything to erase how do i check that ?
    The size of the vector would be unchanged. Still, erase() doesnt actually 'find' anything. The comparison is done in remove() or remove_if(), which returns an iterator pointing to one past the end of the non-removed portion of the sequence.

    This is why you may not even need to use erase(), you could just use this new endpoint iterator and ignore the elements in the removed portion of the sequence, as per 7stud's example.

    with what do i replace the ???
    You really have to read the STL Guide on remove(), remove_if() and vector.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    you were right ! that STL Guide is very useful, i will consult it from time to time. I also find out that i can do

    Code:
    doublevector.erase(newend, doublevector.end());
    which is actually combined with what 7stud said and is exactly what i needed !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running remove() gets permission denied error...
    By edomingox in forum C Programming
    Replies: 4
    Last Post: 01-11-2009, 11:55 AM
  2. randomly remove elements
    By joan in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2006, 12:46 PM
  3. randomly remove elements
    By joan in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2006, 11:22 AM
  4. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21