Microsoft KB Archive/168376

= PRB: STL string Class insert Function Does Not Work as Expected =

Article ID: 168376

Article Last Modified on 12/2/2003

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q168376

<div class="symptoms_section">

SYMPTOMS
The string::insert member function does not work as expected. This is because the same string object is used for modification and input. Many data manipulation routines do not check for overlapping or identical arguments, causing unexpected results.

<div class="resolution_section">

RESOLUTION
Create a temporary copy of the string as shown below: s.insert(0, string("b"), 1);

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce Behavior
using namespace std ;
 * 1) include
 * 2) include

void f1 { string s ("abc"); s.insert (0, s, 1, 1); cout << "s (should be 'babc'): " << s << endl << endl; }

int main {   f1;// prints "aabc" instead of "babc" return 0 ; }

Keywords: kbprogramming kbprb KB168376

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.