String :: insert at the end of the string

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

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

and

myString.append(1,myNewChar);

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

+3
source share
1 answer

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

Required pos1 <= size()andpos2 <= str.size()

(where pos1is the index that you pass, and pos2 == nposin the overload that you call) - note that this is <=, not <.

+5
source

All Articles