String constructors [C++ Reference]
Keywords: string,string_constructors
Date: 2009-10-10T19:59:06-0700

[[String constructors]]
C++ Reference
Translations of this page?:

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

## String constructors

Syntax:

        #include <string>
        string();
        string( const string& s );
        string( size_type length, 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.

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