CIS-255 Home http://www.c-jump.com/bcc/c255c/c255syllabus.htm

std::string


  1. The basic_string template
  2. The "position and count" system
  3. Assignment
  4. Append
  5. Character access
  6. The dual operator[] idiom
  7. Information
  8. Conversion to char*
  9. Search
  10. Comparison
  11. Non-members
  12. Advice

The basic_string template

  • std::basic_string owns its own memory

  • Uses "position and count" system

  • STL provides

    typedef basic_string< char > string;
  • Construction:


#include <string>
using std::string;

// Variety of construction options:
string s1;              // empty
string s2 = s1;         // copy constructor
string s3 = "Hello";    // from C-string
string s4( "Hello", 4 );// first 4 chars
string s5( s3, 2, 5 );  // "llo"
string s6( 6, 'a' );    // "aaaaaa"

The "position and count" system


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" );
}

Append


#include <cassert>
#include <string>
using std::string;
void main() {
    string s1;    // empty
    string s2;    // empty
    s1 += "Fred"; // From null terminated char*
    s1 += 'a';    // Becomes "Freda"
    s2 += s1;     // Of course, s2 becomes "Freda"
    // Additional append functions corresponding
    // to existing constructors:
    s1.append( "Hello", 4);
    assert( s1 == "FredaHell" );
    s1.append( 6, 'a' );
    assert( s1 == "FredaHellaaaaaa" );
    // Append portion of s2 that begins at the position 2
    // and up to 5 characters. However, it takes less
    // than 5 because the end of s2 is reached sooner:
    s1.append( s2, 2, 5 );  // appends "eda"
    assert( s1 == "FredaHellaaaaaaeda" );
}

Character access


The dual operator[] idiom


Information


    #include <cassert>
    #include <string>
    using std::string;
    void main()
    {
        string s1 = "Hello";
        assert( s1.length() == 5 );
        assert( s1.size() == 5 ); // same as length
        assert( s1.empty() == false ); // it isn't, so false
    }

Conversion to char*


Search


    #include <cassert>
    #include <string>
    using std::string;
    void main()
    {
        string s1 = "Hello world";
        // Find a substring (from front or back)
        int pos = s1.find("worl");  // returns 6:
                                    assert( pos == 6 );
        pos = s1.find("foo");       // returns string::npos
                                    assert( pos == string::npos );
        pos = s1.find('o');         // returns 4:
                                    assert( pos == 4 );
        pos = s1.rfind('o');        // returns 7:
                                    assert( pos == 7 );
        // Find any character (from front or back)
        pos = s1.find_first_of( "aeiou" );  // returns 1;
                                            assert( pos == 1 );
        pos = s1.find_first_not_of( "aeiou" );  // returns 0;
                                                assert( pos == 0 );
    }

Comparison

  • The compare( ) member function returns:

    • 0 if all characters compare equal

    • negative if first char that doesn't match compares to less in the object than in the comparing string

    • positive otherwise.

#include <cassert>
#include <string>
using std::string;
void main()
{
    string s1 = "Hello world";

    // Compare "Hello world" with "Fred":
    int result = s1.compare( "Fred" );

    // Returns positive: H > F
    assert( result > 0 );

    // Compare "world" with "zone":
    result = s1.compare( 6, string::npos, "zone" );

    // Returns negative: w < z
    assert( result < 0 );
}
  • When string::npos specifies the length of the compared substring, all the rest of the string is assumed.

Non-members


Advice