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?