string类 构造函数

string (const char * s);
string (size_type n,char c);
string (const char * s,size_type n);
string (const string & str,size_tpye n=npos);
string ();
template<class Iter>
string(Iter begin,Iter end);

string::npos  (通常为最大unsigned int值 比最大索引大1)

 

 

////////////////////////////////////////////
//
//string1.cpp
//
////////////////////////////////////////////
#include <iostream>
#include <string>
// using string constructors

int main()
{
    using namespace std;
    string one("Lottery Winner!");    // ctor #1
    cout << one << endl;              // overloaded <<
    string two(20, '$');              // ctor #2
    cout << two << endl;
    string three(one);                // ctor #3
    cout << three << endl;
    one += " Oops!";                  // overloaded +=
    cout << one << endl;
    two = "Sorry! That was ";
    three[0] = 'P';
    string four;                      // ctor #4
    four = two + three;               // overloaded +, =
    cout << four << endl;
    char alls[] = "All's well that ends well";
    string five(alls,20);             // ctor #5
    cout << five << "!\n";
    string six(alls+6, alls + 10);    // ctor #6
    cout << six  << ", ";
    string seven(&five[6], &five[10]);// ctor #6 again
    cout << seven << "...\n";

    return 0;
}

posted @ 2007-03-13 22:20  Edward Xie  阅读(6060)  评论(0)    收藏  举报