<<<Index>>>

Assignment


#include <cassert>
#include <string>
using std::string;
void main() {
    string s1;   // empty
    string s2;   // empty
    s1 = "Fred"; // From null terminated char*
    s2 = 'a';    // s2 becomes "a"
    assert( s2 == "a" );
    s1 = s2;     // Assign one to another
    // Additional assignments corresponding
    // to existing constructors:
    s1.assign( "Hello", 4 );
    assert( s1 == "Hell" );
    s1.assign( 6, 'a' );
    assert( s1 == "aaaaaa" );
    // Copy portion of s1 that begins at the position 2
    // and up to 5 characters. However, it takes less
    // than 5 because the end of s1 is reached sooner:
    s2.assign( s1, 2, 5 );
    assert( s2 == "aaaa" );
}
<<<Index>>>