string:string_constructors

C++ Reference

String constructors

Syntax:

    #include <string>
    string();
    string( const string& s );
    string( size_type length, const char& ch );
    string( const char* str );
    string( const char* str, size_type length );
    string( const string& str, size_type index, size_type length );
    string( input_iterator start, input_iterator end );
    ~string();

The string constructors create a new string containing:

  • nothing; an empty string,
  • a copy of the given string s,
  • length copies of ch,
  • a duplicate of str (optionally up to length characters long),
  • a substring of str starting at index and length characters long
  • a string of characters denoted by the start and end iterators

For example,

     string str1( 5, 'c' );
     string str2( "Now is the time..." );
     string str3( str2, 11, 4 );
     cout << str1 << endl;
     cout << str2 << endl;
     cout << str3 << endl;

displays

     ccccc
     Now is the time...
     time

The string constructors usually run in linear time, except the empty constructor, which runs in constant time.