Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string::insert at end of string

The following two lines do the same thing in Visual Studio 2005:

myString.insert(myString.size(),1,myNewChar);

and

myString.append(1,myNewChar);

Is the first one supposed to throw an out_of_range exception or is this the correct behavior?

like image 342
IronMensan Avatar asked Apr 19 '26 02:04

IronMensan


1 Answers

This is correct behavior -- the index you pass is the index of the position behind the point of insertion of the new characters, not before. In fact, the C++03 standard specifically says (§21.3.5.4/2):

Requires pos1 <= size() and pos2 <= str.size()

(where pos1 is the index you're passing and pos2 == npos in the overload you call) -- note that it's <= rather than <.

like image 51
ildjarn Avatar answered Apr 20 '26 14:04

ildjarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!