Erase characters in std::string

This is a discussion on Erase characters in std::string within the C++ Programming forums, part of the General Programming Boards category; All i'm trying to do is erase the first occurance of a character in a std::string. str::erase(iterator) takes an iterator ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    35

    Erase characters in std::string

    All i'm trying to do is erase the first occurance of a character in a std::string.

    str::erase(iterator) takes an iterator as an argument, but std::find() returns size_type.

    This is the best i can come up with.
    There's has got to be an easier way right?

    Code:
    int main(int argc, char *argv[])
    {
        string str = "seller";
        char c = 'l';
       	string::iterator pos;
       	pos = str.begin();
    
            //seek first occurence
       	while(pos != str.end() && *pos !=
     c)
       	          pos++;
        
        //erase first occurence 
        str.erase(pos);
        
    	
    	cout << str << endl;
    	
        return 0;
    }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    Code:
    str.erase(std::find(str.begin(), str.end(), 'c'));
    or

    Code:
    std::string::iterator found = std::find(str.begin(), str.end(), 'c');
    if( found != str.end())
        str.erase(found);
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can do begin() + loc to get an iterator where loc is the size_type location of the character returned by find.

    Mario F., as the OP stated, find returns an index not an iterator.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    Ah gotcha. Skipped that. Sorry.

    Well it's just a matter of replacing std:string::iterator with size_type

    EDIT: Unless the erase function is not overloaded for size_type...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    Well... actually...

    If find() returns a size_type is because you are using the string::find() member function, not the algorithm one. Use the algorithm one defined under <algorithm>.

    http://www.cppreference.com/cppalgorithm/find.html
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Daved
    Mario F., as the OP stated, find returns an index not an iterator.
    He's using std::find, not std::string::find.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Right. The OP mentioned std::find as well, but my guess is that was a typo (see similar typo with str::erase). Either way, I would use the member function version and add the size_type to the begin() iterator as I showed. Prefer the member function over the generic algorithm in general, and the addition to the iterator isn't a big enough deal to outweigh that advice in this case.

    >> Unless the erase function is not overloaded for size_type.
    The other option is s.erase(s.find(...), 1). The 1 is to indicate a single character at the specified location. This might actually be the best option.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    35
    Yeah, those were typos....i do it all the time with 'string' and 'str'....

    i'm liking the begin()+index idea..haven't tried it yet, but i'm sure it works..

    thanks Dave .... ("good effort " to the rest of you )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. erase all characters in a character array?
    By diddy02 in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:30 PM
  3. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 08:08 PM

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