Thread: string methods hard time

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    string methods hard time

    Hello people,
    can anyone explain me why output from this program
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string str = "This is, you shouldn't see this, just a test!";
    	string :: size_type start, end;
    	string delim = ",";
    
    	start = str.find(delim);
    	if (start == string::npos)
    	{
    		return 0;
    	}
    	end = str.find(delim, start+delim.length());
    	str.erase(start, end+delim.length());
    	cout << str;
    	return 0;
    }
    is: "This is test!"(without quotes)?
    I expected that characters between ',' (including them) would be removed.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The reason is that the erase() member function of std::string usually uses iterators. In your case, you are using the version that is index-based. This version takes a starting index, followed by the number of characters to erase.

    You could simply switch to the iterator version:
    Code:
    str.erase(str.begin() + start, str.begin() + end + delim.length());
    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 Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Or to use this:
    Code:
    str.erase(start, end+delim.length()-start);
    Thanks laserlight, I didn't pay attention when reading documentation.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  2. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. How to decrease a time string by a given value
    By Ruchikar in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:08 PM