开发者

string::insert at end of string

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

开发者_如何学Go
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?


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 <.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜