string::insert at end of string
The following two lines do the same thing in Visual Studio 2005:
开发者_如何学GomyString.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?
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()
andpos2 <= str.size()
(where pos1
is the index you're passing and pos2 == npos
in the overload you call) -- note that it's <=
rather than <
.
精彩评论