D String Klasa

download D String Klasa

of 16

Transcript of D String Klasa

  • 7/23/2019 D String Klasa

    1/16

    Dodatak D Standardna string klasa 1

    Dodatak D Standardna string klasa

    from: C++ annotations, by Frank C. Brokken, University of Groningen, ISBN 90 367 0470.

    The C programming language offers rudimentary string support: theASCII-Zterminated series ofcharacters is the foundation on which a large amount of code has been built (We define an ASCII-Zstring as a series of ASCII-characters terminated by the ASCII-character zero (hence -Z), which has

    the value zero, and should not be confused with character ' 0' , which usually has the value 0x30).Standard C++ now offers a st r i ngtype of its own. In order to use st r i ng-type objects, the headerfile st r i ngmust be included in sources.

    Actually, st r i ngobjects are class typevariables,

    str i ng str i ngObj ect;

    In this section the operators that are available for strings and some other operations are discussed. Theoperations that can be performed on strings take the form

    st r i ngObj ect . oper at i on( ar gument Li st )

    For example, if s t r1and s t r2are variables of type st r i ng, then

    st r 1. compar e( st r 2)

    can be used to compare both strings.

    The st r i ngclass offers a large number of member functions, as well as extensions of some well-known operators, like the assignment (=), the comparison operators (==, ! =, , . . ) andconcatenation (+). These operators and functions are discussed in the following sections.

    C.1: Operations on s trings

    Some of the operations that can be performed on strings return indices within the strings. Whenever

    such an operation fails to find an appropriate index, the valuest r i ng: : npos is returned. This valueis a (symbolic) value of type str i ng: : s i ze_type, which is (for all practical purposes) an i nt .

    Note that in all operations with str i ngs both st r i ngobjects and char const * values andvariables can be used.

    Some st r i ng-member functions use iterators. Iterators will be covered in section 17.2. The memberfunctions that use iterators are listed in the next section (C.2), they are not further illustrated below.The following operations can be performed on strings:

    Initialization: String objects can be initialized. For the initialization a plain ASCI I - Zstring,

    another st r i ngobject, or an implicit initialization can be used. In the example, note that the implicitinitialization does not have an argument, and may not use an argument list. Not even empty.

    #i ncl ude

    i nt mai n( ){

    str i ngst r i ngOne( "Hel l o Wor l d") , / / usi ng pl ai n asci i - Zst r i ngTwo( st r i ngOne) , / / usi ng anot her st r i ng obj ects t r i ngThree; / / i mpl i ci t i ni t i al i zat i on to ""

    / / do not use: st r i ngThr ee( ) ;r et ur n 0;

    }

  • 7/23/2019 D String Klasa

    2/16

    Dodatak D Standardna string klasa 2

    Assignment: String objects can be assigned to each other. For this the assignment operator (i.e., the

    =operator) can be used, which accepts both a s t r i ngobject and a C-style characterstring as its right-hand argument:

    #i ncl ude

    i nt mai n( ){

    str i ngst r i ngOne( "Hel l o Wor l d") ,st r i ngTwo;

    st r i ngTwo = st r i ngOne; / / assi gn st r i ngOne t o st r i ngTwost r i ngTwo = "Hel l o wor l d"; / / assi gn a C- st r i ng t o St r i ngTwo

    r et ur n 0;}

    String to ASCII-Z conversion: In the previous example a standard C-string (an ASCII-Z string) was

    implicitly converted to a st r i ng-object. The reverse conversion (converting a st r i ngobject to astandard C-string) is not performed automatically. In order to obtain the C-string that is stored withinthe st r i ngobject itself, the member function c_s t r ( ) , which returns a char const * , can beused:

    #i ncl ude

    i nt mai n( ){

    st r i ng st r i ngOne( "Hel l o Wor l d") ;char const *Cst r i ng = st r i ngOne. c_st r ( ) ;

    cout

  • 7/23/2019 D String Klasa

    3/16

    Dodatak D Standardna string klasa 3

    r et ur n 0;}

    When an illegal index is passed to the at ( ) member function, the program aborts.

    Comparisons: Two strings can be compared for (in)equality or ordering, using the ==, ! =, =operators or the st r i ng: : compar e( ) member function. The compare( ) member function comes in several flavors e.g.:

    i nt st r i ng: : compar e( st r i ng const &ot her ) ;

    this variant offers a bit more information than the comparison-operators do. The return value of the

    st r i ng: : compar e( ) member function may be used for lexicographical ordering: a negative valueis returned if the string stored in the string object using the compare( ) member function (in theexample: st r i ngOne) is located earlier in theASCII collating sequencethan the string stored in thestring object passed as argument.

    #i ncl ude

    #i ncl ude

    usi ng namespace st d;

    i nt mai n( ){

    st r i ng st r i ngOne( "Hel l o Wor l d") , st r i ngTwo;

    i f ( st r i ngOne ! = st r i ngTwo)st r i ngTwo = st r i ngOne;

    i f ( st r i ngOne == st r i ngTwo)st r i ngTwo = "Somethi ng el se";

    i f ( st r i ngOne. compar e( st r i ngTwo) > 0)cout

  • 7/23/2019 D String Klasa

    4/16

    Dodatak D Standardna string klasa 4

    More variants of st r i ng: : compar e( ) are available. As stated, refer to section C.2.4for details.

    Appending: A st r i ngcan be appended to another string. For this the +=operator can be used, as

    well as the st r i ng &st r i ng: : append( ) member function.

    Like the compare( ) function, the append( ) member function may have extra arguments. The first

    argument is the string to be appended, the second argument specifies the index position of the firstcharacter that will be appended. The third argument specifies the number of characters that will be

    appended. If the first argument is of type char const * , only a second argument may be specified.In that case, the second argument specifies the number of characters of the first argument that are

    appended to the st r i ngobject. Furthermore, the +operator can be used to append two strings withinan expression:

    #i ncl ude #i ncl ude

    usi ng namespace st d;

    i nt mai n( ){

    str i ngstr i ngOne( "Hel l o") ,st r i ngTwo( "Wor l d") ;

    st r i ngOne += " " + st r i ngTwo;

    st r i ngOne = "hel l o";st r i ngOne. append( " wor l d") ;

    / / append 5 char act er s:st r i ngOne. append( " ok. >Thi s i s not used

  • 7/23/2019 D String Klasa

    5/16

    Dodatak D Standardna string klasa 5

    #i ncl ude

    i nt mai n( ){

    str i ngstr i ngOne( "Hel l ok. ") ;

    / / I nser t "o " at posi t i on 4str i ngOne. i nser t ( 4, "o ") ;

    str i ngwor l d( "The Wor l d of C++") ;

    / / i nser t "Wor l d" i nt o st r i ngOnestr i ngOne. i nser t ( 6, wor l d, 4, 5) ;

    cout

  • 7/23/2019 D String Klasa

    6/16

    Dodatak D Standardna string klasa 6

    i f ( i dx == st r i ng: : npos)br eak;

    l i ne. repl ace( i dx, sear ch. s i ze( ) , repl ace) ;}cout

  • 7/23/2019 D String Klasa

    7/16

    Dodatak D Standardna string klasa 7

    Substrings: To extract a substring from a st r i ngobject, the member function str i ngst r i ng: : subst r ( ) is available. The returned st r i ngobject contains a copy of the substring inthe st r i ng-object calling substr() The substr() member function has two optionalarguments:

    Without arguments, a copy of the st r i ngitself is returned.

    The first argument may be used to specify the offset of the first character to be returned. The second argument may be used to specify the number of characters that are to be returned. Forexample:

    #i ncl ude

    i nt mai n( ){

    st r i ng st r i ngOne( "Hel l o Wor l d") ;

    cout

  • 7/23/2019 D String Klasa

    8/16

    Dodatak D Standardna string klasa 8

    ( pos = l i ne. f i nd_f i r st _not _of ( "1234567890") )! = st r i ng: : npos ?

    l i ne. substr ( pos):

    " *** not f ound *** ")

  • 7/23/2019 D String Klasa

    9/16

    Dodatak D Standardna string klasa 9

    C.2: Overview of operations on strings

    In this section the available operations on strings are summarized. There are four subparts here: the

    st r i ng-initializers, the s t r i ng-iterators, the st r i ng-operators and the st r i ng-memberfunctions.

    The member functions are ordered alphabetically by the name of the operation. Below, obj ect is a

    st r i ng-object, and ar gument is either a st r i ngor a char const * , unless overloadedversions tailored to st r i ngand char const * parameters are explicitly mentioned. Obj ect isused in cases where a st r i ngobject is initialized or given a new value. Ar gument remainsunchanged.With member functions the types of the parameters are given in a function-prototypical way. Withseveral member functions iteratorsare used. At this point in the Annotations it's a bit premature todiscuss iterators, but for referential purposes they have to be mentioned nevertheless. So, a forwardreference is used here: see section 17.2for a more detailed discussion of iterators.

    Finally, note that all st r i ng-member functions returning indices in obj ect return the predefinedconstant st r i ng: : npos if no suitable index could be found.

    C.2.1: The string initializersThe following st r i ngconstructors are available:

    st r i ng obj ect ;Initializes obj ect to an empty string.

    st r i ng obj ect( st r i ng: : si ze_t ype n, char c); Initializes obj ect with ncharacters c.

    st r i ng obj ect ( st r i ng ar gument ) ; Initializes obj ect with ar gument .

    st r i ng obj ect( st r i ng ar gument , st r i ng: : si ze_t ype i dx, )t t ( st r i ng: : si ze_t ype n = pos) ;Initializes obj ect with ar gument , using ncharacters of ar gument , starting at index i dx.

    st r i ng obj ect( I nput I t er at or begi n, I nput I t er at or end) ; Initializes obj ect with the range of characters implied by the provided I nput I t er at or s .

    C.2.2: The string iterators

    Forward iterators returned by the members:

    str i ng: : begi n( )

    str i ng: : end( )

    Reverse iterators returned by the members:

    str i ng: : r begi n( )

    str i ng: : r end( )

    C.2.3: The string operators

    The following string operators are available:

    obj ect = ar gument .Assignment of ar gument to obj ect . May also be used for initializing st r i ngobjects.

    obj ect = c.Assignment of char cto obj ect . May notbe used for initializing st r i ngobjects.

    obj ect += ar gument .Appends ar gument to obj ect . Ar gument may also be a char value.

    ar gument 1 + ar gument 2.Within expressions, str i ngs may be added. At least one term of the expression (the left-hand termor the right-hand term) should be a st r i ngobject. The other term may be a st r i ng, a charconst * value or a char value, as illustrated by the following example:

  • 7/23/2019 D String Klasa

    10/16

    Dodatak D Standardna string klasa 10

    voi d f un( ){

    char const*asci i z = "hel l o";

    str i ngf i r st = " f i r st " ,

    second;

    / / al l expr essi ons compi l e ok:second = f i r st + asci i z;second = asci i z + f i r st ;second = f i r st + ' a' ;second = ' a' + f i r st ;

    }

    obj ect[ st r i ng: : si ze_t ype pos] .The subscript-operator may be used to assign individual characters of obj ect or to retrieve thesecharacters. There is no range-checking. If range checking is required, use the at ( ) member function,summarized earlier.

    ar gument 1 == ar gument 2.The equality operator ( operat or==( ) ) may be used to compare a st r i ngobject to anotherst r i ngor char const * value. The oper at or ! =( ) is available as well. The return value for

    both is a bool . For two identical strings operat or==( ) returns t rue, and oper at or ! =( ) returns f al se.

    ar gument 1 < ar gument 2.The less-than operator may be used to compare the ordering within the Ascii-character set of

    ar gument 1and ar gument 2. The operators and >=are available as well.

    ost r eam st r eam; st r eam > obj ect .

    The extraction-operator may be used with st r i ngobjects. It operates analogously to the extractionof characters into a character array, but obj ect is automatically resized to the required number ofcharacters.

    C.2.4: The string member functions

    The string member functions are listed in alphabetical order. The member name, prefixed by the

    st r i ng-class is given first. Then the full prototype and a description are given. Values of the typestr i ng: : s i ze_typerepresent index positions within a st r i ng. For all practical purposes, thesevalues may be interpreted as i nt . The special value st r i ng: : npos is defined to represent a non-existing index.

    char &st r i ng: : at ( si ze_t ype pos) ;

    The character (reference) at the indicated position is returned (it may be reassigned). The member functionperforms range-checking, aborting the program if an invalid index is passed.

    st r i ng &st r i ng: : append( I nput I t er at or begi n, I nput I t er at or end) ;

    Using this member function the range of characters implied by the begi nand end I nput I t er at or sareappended to the s t r i ngobject.

  • 7/23/2019 D String Klasa

    11/16

    Dodatak D Standardna string klasa 11

    st r i ng &st r i ng: : append( st r i ng ar gument , si ze_t ype pos, si ze_t ype n) ;

    If only ar gument is given, it is appended to the s t r i ngobject.

    If posis specified as well, ar gument is appended from index position posuntil the end of ar gument .

    If all three arguments are provided, ncharacters of ar gument , starting at index position posare appended to

    the st r i ngobject. If ar gument is of type char const * , parameter poscannot be specified. So, withchar const * arguments, either allcharacters or an initial subsetof the characters of the provided charconst * argument are appended to the s t r i ngobject.

    st r i ng &st r i ng: : append( si ze_t ype n, char c) ;

    Using this member function, ncharacters ccan be appended to the st r i ngobject.

    st r i ng &st r i ng: : assi gn( st r i ng ar gument , si ze_t ype pos, si ze_t ype n) ;

    If only ar gument is given, it is assigned to the st r i ngobject.

    If posis specified as well, the s t r i ngobject is assigned from index position posuntil the end of ar gument .

    If all three arguments are provided, ncharacters of ar gument , starting at index position posare assigned tothe st r i ngobject. If ar gument is of type char const * , no parameter posis available. So, with charconst * arguments, either allcharacters or an initial subsetof the characters of the provided char const * argument are assigned to the st r i ngobject.

    st r i ng &st r i ng: : assi gn( si ze_t ype n, char c) ; Using this member function, ncharacters ccan be assigned to the s t r i ngobject.

    si ze_t ype str i ng: : capaci t y( ) ; returns the number of characters that can currently be stored inside the st r i ngobject.

    i nt st r i ng: : compar e( st r i ng ar gument ) ;

    This member function can be used to compare (according to the ASCII-character set) the text stored in the

    st r i ngobject and in ar gument . The ar gument may also be a (non-0) char const * . 0 is returned ifthe characters in the st r i ngobject and in ar gument are the same; a negative value is returned if the text inst r i ngis lexicographically beforethe text in ar gument ; a positive value is returned if the text in st r i ngislexicographically beyondthe text in ar gument .

    i nt st r i ng: : compar e( si ze_t ype i dx, si ze_t ype l en, st r i ng ar gument ) ;

    This member function can be used to compare a substring of the text stored in the st r i ngobject with the textstored in ar gument . At most l encharacters, starting at offset i dx, are compared with the text in ar gument .If i dxis or exceeds the number of characters in the s t r i ngobject, an (out _of _r ange) exception is thrown.The ar gument may also be a (non-0) char const * .

  • 7/23/2019 D String Klasa

    12/16

    Dodatak D Standardna string klasa 12

    i nt st r i ng: : compar e( si ze_t ype i dx, si ze_t ype l en, st r i ng ar gument ,si ze_t ype ar g_i dx, si ze_t ype ar g_l en) ;

    This member function can be used to compare a substring of the text stored in the st r i ngobject with asubstring of the text stored in ar gument . At most l encharacters of the s t r i ngobject, starting at offset i dx,are compared with at most ar g_l encharacters of ar gument , starting at offset ar g_i dx. If i dxor

    ar g_i dxis or exceeds the number of characters in their respective

    s t r i ngobjects, an (

    out _of _r ange)

    exception is thrown. Note that ar gument mustalso be a st r i ngobject.

    i nt st r i ng: : compar e( si ze_t ype i dx, si ze_t ype l en, char const *ar gument ,si ze_t ype ar g_l en) ;

    This member function can be used to compare a substring of the text stored in the st r i ngobject with asubstring of the text stored in ar gument . At most l encharacters of the s t r i ngobject, starting at offset i dx,are compared with at most ar g_l encharacters of ar gument . If i dxis or exceeds the number of characters inthe tt (string) object, an (out _of _r ange) exception is thrown. Argument must have at least ar g_l encharacters. However, the characters may have arbitrary values: the ASCII-Z value has no special meaning.

    si ze_t ype st r i ng: : copy(char const *argument , si ze_t ype n, si ze_t ype pos) ;

    If the third argument is omitted, the first ncharacters of the st r i ngobject are copied to ar gument . If thethird argument is given, copying starts from element posof the st r i ngobject. Following the copying, noASCI I - Zis appended to the copied string. If nexceeds the st r i ngobject's l engt h( ) , at most l engt h( ) characters are copied. The actual number of characters that were copied is returned.

    char const *str i ng: : c_str ( ) ;

    the member function returns the contents of the st r i ngobject as an ASCI I - ZC-string.

    char const *st r i ng: : dat a( ) ;

    returns the raw text stored in the st r i ngobject.

    bool st r i ng: : empt y( ) ;

    returns trueif the st r i ngobject contains no data.

    st r i ng &st r i ng: : er ase( si ze_t ype pos; si ze_t ype n) ;

    This member function can be used to erase (a sub)string of the st r i ngobject. The basic form erases thest r i ngobject completely. The working of other forms of er ase( ) depend on the specification of extraarguments:

    If posis specified, the contents of the s t r i ngobject are erased from index position posuntil the end of the

    st r i ngobject.

    If posand nare provided, ncharacters of the s t r i ngobject, starting at index position posare erased.

    i t erator str i ng: : erase( i t er at or p) ;

    The contents of the st r i ngobject are erased until (iterator) position p. The iterator pis returned.

    i t erator s t r i ng: : erase( i terator f , i t erator l ) ;

    The range of characters of the st r i ngobject, implied by the i t erator s f and l are erased. The iterator fis returned.

  • 7/23/2019 D String Klasa

    13/16

    Dodatak D Standardna string klasa 13

    si ze_t ype st r i ng: : f i nd( st r i ng ar gument , si ze_t ype pos) ;

    Returns the index in the st r i ngobject where ar gument is found. If posis omitted, the search starts at thebeginning of the st r i ngobject. If posis provided, it refers to the index in the st r i ngobject where thesearch for ar gument should start.

    si ze_t ype st r i ng: : f i nd( char const *ar gument , si ze_t ype pos, si ze_t ype n) ; Returns the index in the st r i ngobject where ar gument is found. The parameter nindicates the number ofcharacters of ar gument that should be used in the search: it defines a partial string starting at the beginning ofar gument . If omitted, all characters in ar gument are used. The parameter posrefers to the index in thest r i ngobject where the search for ar gument should start. If the parameter posis omitted as well, thest r i ngobject is scanned completely.

    si ze_t ype st r i ng: : f i nd( char c, s i ze_t ype pos);

    Returns the index in the st r i ngobject where cis found. If the argument posis omitted, the search starts atthe beginning of the st r i ngobject. If provided, it refers to the index in the st r i ngobject where the search

    for the st r i ngobject should start.

    si ze_t ype st r i ng: : f i nd_f i r st_of( str i ng ar gument , s i ze_t ype pos);

    Returns the index in the st r i ngobject where any character in ar gument is found. If the argument posisomitted, the search starts at the beginning of the st r i ngobject. If provided, it refers to the index in the

    st r i ngobject where the search for ar gument should start.

    si ze_t ype st r i ng: : f i nd_f i r st _of ( char const * ar gument , si ze_t ype pos,si ze_t ype n) ;

    Returns the index in the st r i ngobject where a character of ar gument is found, no matter which character.The parameter nindicates the number of characters of the st r i ngobject that should be used in the search: itdefines a partial string starting at the beginning of the st r i ngobject. If omitted, all characters in the st r i ngobject are used. The parameter posrefers to the index in the st r i ngobject where the search for ar gument should start. If the parameter posis omitted as well, the st r i ngobject is scanned completely.

    si ze_t ype str i ng: : f i nd_f i r st_of( char c, s i ze_t ype pos);

    Returns the index in the st r i ngobject where character cis found. If the argument posis omitted, the searchstarts at the beginning of the s t r i ngobject. If provided, it refers to the index in the s t r i ngobject where thesearch for cshould start.

    si ze_t ype st r i ng: : f i nd_f i r st _not _of ( st r i ng ar gument , si ze_t ype pos) ;

    Returns the index in the st r i ngobject where a character not appearing in ar gument is found. If theargument posis omitted, the search starts at the beginning of the st r i ngobject. If provided, it refers to theindex in the st r i ngobject where the search for ar gument should start.

    si ze_t ype st r i ng: : f i nd_f i r st _not _of ( char const *ar gument , si ze_t ype pos,si ze_t ype n) ;

    Returns the index in the st r i ngobject where any character not appearing in ar gument is found. Theparameter nindicates the number of characters of the s t r i ngobject that should be used in the search: itdefines a partial string starting at the beginning of the st r i ngobject. If omitted, all characters in the st r i ngobject are used. The parameter posrefers to the index in the st r i ngobject where the search for ar gument should start. If the parameter posis omitted as well, the st r i ngobject is scanned completely.

  • 7/23/2019 D String Klasa

    14/16

    Dodatak D Standardna string klasa 14

    si ze_t ype str i ng: : f i nd_f i r st_not _of ( char c, s i ze_t ype pos);

    Returns the index in the st r i ngobject where another character than cis found. If the argument posisomitted, the search starts at the beginning of the st r i ngobject. If provided, it refers to the index in thest r i ngobject where the search for cshould start.

    si ze_t ype st r i ng: : f i nd_l ast _of ( st r i ng ar gument , si ze_t ype pos) ;

    Returns the last index in the st r i ngobject where a character in ar gument is found. If the argument posisomitted, the search starts at the beginning of the st r i ngobject. If provided, it refers to the index in the

    st r i ngobject where the search for ar gument should start.

    si ze_t ype str i ng: : f i nd_l ast _of ( char const * ar gument , si ze_t ype pos,si ze_t ype n) ;

    Returns the last index in the st r i ngobject where a character of ar gument is found. The parameter nindicates the number of characters of the st r i ngobject that should be used in the search: it defines a partialstring starting at the beginning of the s t r i ngobject. If omitted, all characters in the st r i ngobject are used.The parameter posrefers to the index in the st r i ngobject where the search for ar gument should start. If the

    parameter posis omitted as well, the s t r i ngobject is scanned completely.

    si ze_t ype str i ng: : f i nd_l ast_of( char c, s i ze_t ype pos);

    Returns the last index in the st r i ngobject where character cis found. If the argument posis omitted, thesearch starts at the beginning of the st r i ngobject. If provided, it refers to the index in the st r i ngobjectwhere the search for cshould start.

    si ze_t ype st r i ng: : f i nd_l ast _not _of ( st r i ng ar gument , si ze_t ype pos) ;

    Returns the last index in the st r i ngobject where any character not appearing in ar gument is found. If theargument posis omitted, the search starts at the beginning of the st r i ngobject. If provided, it refers to theindex in the st r i ngobject where the search for ar gument should start.

    si ze_t ype st r i ng: : f i nd_l ast _not _of ( char const *argument , si ze_t ype pos,si ze_t ype n) ;

    Returns the last index in the st r i ngobject where any character not appearing in ar gument is found. Theparameter nindicates the number of characters of the s t r i ngobject that should be used in the search: itdefines a partial string starting at the beginning of the st r i ngobject. If omitted, all characters in the st r i ngobject are used. The parameter posrefers to the index in the st r i ngobject where the search for ar gument should start. If the parameter posis omitted as well, all of the st r i ngobject is scanned.

    si ze_t ype st r i ng: : f i nd_l ast_not _of ( char c, s i ze_t ype pos);

    Returns the last index in the st r i ngobject where another character than cis found. If the argument posisomitted, the search starts at the beginning of the st r i ngobject. If provided, it refers to the index in thest r i ngobject where the search for cshould start.

    i str eam &get l i ne( i str eam i nstr eam, str i ng obj ect, char del i mi t er ) ;

    This member function can be used to read a line of text (up to the first delimiter or the end of the stream) from

    i nstr eam. The delimiter has a default value ' \ n' . It is removed from i nstr eam, but it is not stored in the

    st r i ngobject.

  • 7/23/2019 D String Klasa

    15/16

    Dodatak D Standardna string klasa 15

    st r i ng &st r i ng: : i nser t ( si ze_t ype t _pos, st r i ng ar gument , si ze_t ype pos;si ze_t ype n) ;

    This member function can be used to insert (a sub)string of ar gument into the s t r i ngobject, atthe st r i ngobject's index position t _pos . The basic form inserts ar gument completely at index t _pos. The way otherforms of i nsert ( ) work depend on the specification of extra arguments:

    If posis specified, ar gument is inserted from index position posuntil the end of ar gument .

    If posand nare provided, ncharacters of ar gument , starting at index position posare inserted into thest r i ngobject. If ar gument is of type char const * , no parameter posis available. So, with charconst * arguments, either allcharacters or an initial subsetof the characters of the provided char const * argument are inserted into the st r i ngobject.

    str i ng &str i ng: : i nser t ( si ze_t ype t_pos, si ze_t ype n, char c); Using this memberfunction, ncharacters ccan be inserted to the st r i ngobject.

    i terator s t r i ng: : i nsert ( i terator p, char c) ;

    The character cis inserted at the (iterator) position pin the st r i ngobject. The iterator pis returned.

    i t erator st r i ng: : i nsert ( i t erator p, s i ze_type n, char c) ; Ncharacters care inserted at the (iterator) position pin the st r i ngobject. The iterator pis returned.

    i terator s t r i ng: : i nsert ( i terator p, I nput I t erat or f i rs t , I nput I teratorl ast ) ;

    The range of characters implied by the I nput I terators f i rs t and l ast are inserted at the (iterator)position pin the st r i ngobject. The iterator pis returned.

    si ze_t ype str i ng: : l engt h( ) ;

    returns the number of characters stored in the st r i ngobject.

    si ze_t ype st r i ng: : max_si ze( ) ;

    returns the maximum number of characters that can be stored in the st r i ngobject.

    st r i ng& st r i ng: : r epl ace( si ze_t ype pos1, si ze_t ype n1, const st r i ngargument , si ze_t ype pos2, si ze_t ype n2) ;

    The substring of n1characters of the s t r i ngobject, starting at position pos1is replaced by ar gument . Ifn1is set to 0, the member function insertsar gument into the st r i ngobject.The basic form uses ar gument completely. The way other forms of r epl ace( ) work depends on thespecification of extra arguments:

    If pos2is specified, ar gument is inserted from index position pos2until the end of ar gument .

    If pos2and n2are provided, n2characters of ar gument , starting at index position pos2are inserted into thest r i ngobject. If ar gument is of type char const * , no parameter pos2is available. So, with char

    const * arguments, either allcharacters or an initial subsetof the characters of the provided char const * argument are replaced in the st r i ngobject.

    st r i ng &st r i ng: : r epl ace( si ze_t ype pos, si ze_t ype n1, si ze_t ype n2, char c);

    This member function can be used to replace n1characters of the st r i ngobject, starting at index positionpos, by n2 c-characters. The argument n2may be omitted, in which case the string to be replaced is replaced

    by just one character c.

    str i ng& str i ng: : r epl ace ( i t er at or i 1, i t er at or i 2, str i ng ar gument ) ;

    Here, the string implied by the iterators i 1and i 2are replaced by the string st r . If ar gument is a charconst * , an extra argument nmay be used, specifying the number of characters of ar gument that are used inthe replacement.

  • 7/23/2019 D String Klasa

    16/16

    Dodatak D Standardna string klasa 16

    i t erator st r i ng: : r epl ace( i t er at or f , i t erat or l , str i ng ar gument ) ;

    The range of characters of the st r i ngobject, implied by the i t erator s f and l are replaced byar gument . If ar gument is a char const * , an extra argument nmay be used, specifying the number ofcharacters of ar gument that are used in the replacement. The string the st r i ngobject is returned.

    i t erator str i ng: : r epl ace( i t er at or f , i t erat or l , s i ze_type n, char c) ;

    The range of characters of the st r i ngobject, implied by the i t erator s f and l are replaced by n c-characters. The iterator fis returned.

    st r i ng s t r i ng: : repl ace( i t erat or i 1, i t erator i 2, I nput I t erat or j 1,I nput I t erator j 2) ;

    Here the range of characters implied by the iterators i 1and i 2is replaced by the range of characters implied bythe I nput I t er at or s j 1andj 2.

    voi d str i ng: : r esi ze( si ze_t ype n, char c);

    The string stored in the st r i ngobject is resized to ncharacters. The second argument is optional. If providedand the string is enlarged, the extra characters are initialized to c.

    si ze_t ype st r i ng: : r f i nd( st r i ng ar gument , si ze_t ype pos) ;

    Returns the index in the st r i ngobject where ar gument is found. Searching proceeds either from the end ofthe st r i ngobject or from offset posback to the beginning. If the argument posis omitted, searching starts atthe end of the st r i ngobject. If posis provided, it refers to the index in the st r i ngobject where the searchfor ar gument should start.

    si ze_t ype st r i ng: : r f i nd( char const *ar gument , si ze_t ype pos, si ze_t ype n) ;

    Returns the index in the st r i ngobject where ar gument is found. Searching proceeds either from the end ofthe st r i ngobject or from offset posback to the beginning. The parameter nindicates the number ofcharacters of ar gument that should be used in the search: it defines a partial string starting at the beginning ofar gument . If omitted, all characters in ar gument are used. If the argument posis omitted as well, searching

    starts at the end of the st r i ngobject. If posis provided, it refers to the index in the st r i ngobject where thesearch for (a substring of) ar gument should start.

    si ze_t ype str i ng: : r f i nd( char c, s i ze_t ype pos);

    Returns the index in the st r i ngobject where cis found. Searching proceeds either from the end of thest r i ngobject or from offset posback to the beginning.

    s i ze_type str i ng: : s i ze( ) ;

    returns the number of characters stored in the st r i ngobject.

    str i ng str i ng: : substr ( si ze_t ype pos, si ze_t ype n) ;

    Returns a substring of the s t r i ngobject. The parameter nmay be used to specify the number of characters of

    ar gument that are returned. The parameter posmay be used to specify the index of the first character ofar gument that is returned. Either nor both arguments may be omitted.

    si ze_t ype st r i ng: : swap( st r i ng ar gument ) ;

    swaps the contents of the string object and argument. In this case, argument must be a string and cannot be a charconst *.