assign [C++ Reference]
Keywords: string,assign
Date: 2009-10-10T20:03:47-0700

[[assign]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## assign

Syntax:

        #include <string>
        string& assign( input_iterator start, input_iterator end );
        string& assign( const string& str );
        string& assign( const Char* str );
        string& assign( const Char* str, size_type num );
        string& assign( const string& str, size_type index, size_type len );
        string& assign( size_type num, Char ch );

The default assign method gives the current string the values from `start` to `end`, or gives it `num` copies of `ch`.

In addition to the normal assign functionality that all C++ containers have, strings possess an assign method that also allows them to:

- assign `str` to the current string,
- assign the first `num` characters of `str` to the current string,
- assign a substring of `str` starting at `index` that is `len` characters long to the current string,

For example, the following code:

         string str1, str2 = "War and Peace";
         str1.assign( str2, 4, 3 );
         cout << str1 << endl;

displays

         and

This function will destroy the previous contents of the string.

Related Topics: [] operator

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •