开发者

string::insert at end of string

开发者 https://www.devze.com 2023-03-11 19:15 出处:网络
The following two lines do the same thing in Visual Studio 2005: 开发者_如何学Go myString.insert(myString.size(),1,myNewChar);

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

精彩评论

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

关注公众号