Thread: error from string insert

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    60

    Question error from string insert

    I try to insert a character into a string at every position that modulus 3 == 0.

    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::endl;
    using std::string;
    
    int main( )
    {
    	string s = "01020304050607080910";
    	string::iterator it;
    	string::iterator begin = s.begin();
    	string::const_iterator end = s.end();
    	int cnt = 0;
    	
    	for( ; it != end; ++it ) {
    		++cnt;
    		cout << cnt << endl;
    		if( cnt % 3 == 0 )
    			it = s.insert( it, ' ' ); // <-- what's wrong here?
    
    	}
    	cout << "done scan" << endl;
    	cout << s << endl;
    
    	return 0;
    }
    I've got "Segmentation fault".
    What's wrong with my insert?

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    I'm not sure what is wrong, maybe because you are modifying the iterator while traversing it ? but I would just do it like this ->

    Code:
            int len = s.length();
    	for(int i = 0; i < len; ++i)
    	{
    		if(i % 3 == 0){
    			s.insert(i," ");
    			len = s.length();
    		}
    	}
    Spidey out!

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you intend the iterator it to iterate over any particular string? If so, you should probably make it refer somewhere, rather than pointing into space.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    you never initialize your it

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    60
    @all, thanks, I've got it.

    When I insert a character, the string size (or capacity) is increased; however, my iterator at end and beginning is const, that is, it causes conflict in memory between const iterator and the new inserted character in string.Finally, it crashes, "Segmentation fault"

    I fixed like this and it works well.
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::endl;
    using std::string;
    
    int main( )
    {
    	string s = "01020304050607080910";
    	string::iterator it;
    
    	int cnt = 0;
    	
    	for( it = s.begin(); it != s.end(); ++it ) {
    		++cnt;
    		if( cnt % 4 == 0 )
    			it = s.insert( it, ' ' ); 
    
    	}
    	cout << s << endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string insert breaks iterator
    By AndrewL in forum C++ Programming
    Replies: 15
    Last Post: 12-12-2008, 08:16 AM
  2. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Unable to insert a string into another string
    By Jacquiline in forum C Programming
    Replies: 0
    Last Post: 04-05-2003, 09:15 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM