20
basic_string<charT, traits, Alloc> Category: containers Component type: type Description The basic_string class represents a Sequence of characters. It contains all the usual operations of a Sequence , and, additionally, it contains standard string operations such as search and concatenation. The basic_string class is parameterized by character type, and by that type's Character Traits . Most of the time, however, there is no need to use the basic_string template directly. The types string and wstring are typedefs for, respectively, basic_string<char> and basic_string<wchar_t>. Some of basic_string's member functions use an unusual method of specifying positions and ranges. In addition to the conventional method using iterators, many of basic_string's member functions use a single value pos of type size_type to represent a position (in which case the position is begin() + pos, and many of basic_string's member functions use two values, pos and n, to represent a range. In that case pos is the beginning of the range and n is its size. That is, the range is [begin() + pos, begin() + pos + n). Note that the C++ standard does not specify the complexity of basic_string operations. In this implementation, basic_string has performance characteristics very similar to those of vector : access to a single character is O(1), while copy and concatenation are O(N). By contrast, rope has very different performance characteristics: most rope operations have logarithmic complexity. Note also that, according to the C++ standard, basic_string has very unusual iterator invalidation semantics. Iterators may be invalidated by swap, reserve, insert, and erase (and by functions that are equivalent to insert and/or erase, such as clear, resize, append, and replace). Additionally, however, the first call to any nonconst member function, including the nonconst version of begin() or operator[], may invalidate iterators. (The intent of these iterator invalidation rules is to give implementors greater freedom in implementation techniques.) In this implementation, begin(), end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators. In this implementation, iterators are only invalidated by member functions that explicitly change the string's contents. Example int main() { string s(10u, ' '); // Create a string of ten blanks. const char* A = "this is a test"; s += A; cout << "s = " << (s + '\n'); cout << "As a null‐terminated sequence: " << s.c_str() << endl; cout << "The sixteenth character is " << s[15] << endl; reverse(s.begin(), s.end()); s.push_back('\n'); cout << s;

Basic String

Embed Size (px)

DESCRIPTION

Documentatie String

Citation preview

  • basic_string

    Category:containers Componenttype:type

    Description

    Thebasic_stringclassrepresentsaSequenceofcharacters.ItcontainsalltheusualoperationsofaSequence,and,additionally,itcontainsstandardstringoperationssuchassearchandconcatenation.

    Thebasic_stringclassisparameterizedbycharactertype,andbythattype'sCharacterTraits.Mostofthetime,however,thereisnoneedtousethebasic_stringtemplatedirectly.Thetypesstringandwstringaretypedefsfor,respectively,basic_stringandbasic_string.

    Someofbasic_string'smemberfunctionsuseanunusualmethodofspecifyingpositionsandranges.Inadditiontotheconventionalmethodusingiterators,manyofbasic_string'smemberfunctionsuseasinglevalueposoftypesize_typetorepresentaposition(inwhichcasethepositionisbegin()+pos,andmanyofbasic_string'smemberfunctionsusetwovalues,posandn,torepresentarange.Inthatcaseposisthebeginningoftherangeandnisitssize.Thatis,therangeis[begin()+pos,begin()+pos+n).

    NotethattheC++standarddoesnotspecifythecomplexityofbasic_stringoperations.Inthisimplementation,basic_stringhasperformancecharacteristicsverysimilartothoseofvector:accesstoasinglecharacterisO(1),whilecopyandconcatenationareO(N).Bycontrast,ropehasverydifferentperformancecharacteristics:mostropeoperationshavelogarithmiccomplexity.

    Notealsothat,accordingtotheC++standard,basic_stringhasveryunusualiteratorinvalidationsemantics.Iteratorsmaybeinvalidatedbyswap,reserve,insert,anderase(andbyfunctionsthatareequivalenttoinsertand/orerase,suchasclear,resize,append,andreplace).Additionally,however,thefirstcalltoanynonconstmemberfunction,includingthenonconstversionofbegin()oroperator[],mayinvalidateiterators.(Theintentoftheseiteratorinvalidationrulesistogiveimplementorsgreaterfreedominimplementationtechniques.)Inthisimplementation,begin(),end(),rbegin(),rend(),operator[],c_str(),anddata()donotinvalidateiterators.Inthisimplementation,iteratorsareonlyinvalidatedbymemberfunctionsthatexplicitlychangethestring'scontents.

    Example

    intmain(){strings(10u,'');//Createastringoftenblanks.

    constchar*A="thisisatest";s+=A;cout

  • }Definition

    Definedinthestandardheaderstring.

    Templateparameters

    Parameter Description DefaultcharT Thestring'svaluetype:thetypeofcharacteritcontains. traits TheCharacterTraitstype,whichencapsulatesbasiccharacteroperations. char_traitsAlloc Thestring'sallocator,usedforinternalmemorymanagement. alloc

    Modelof

    RandomAccessContainer,Sequence.

    Typerequirements

    InadditiontothetyperequirementsimposedbyRandomAccessContainerandSequence:

    charTisaPOD("plainol'data")type.traitsisaCharacterTraitstypewhosevaluetypeischarT

    Publicbaseclasses

    None.

    Members

    Member Wheredefined Description

    value_type Container Thetypeofobject,CharT,storedinthestring.

    pointer Container PointertoCharT.reference Container ReferencetoCharTconst_reference Container ConstreferencetoCharTsize_type Container Anunsignedintegraltype.difference_type Container Asignedintegraltype.staticconstsize_typenpos basic_string Thelargestpossiblevalueof

    typesize_type.Thatis,size_type(1).

    iterator Container Iteratorusedtoiteratethroughastring.Abasic_stringsuppliesRandomAccessIterators.

    const_iterator Container Constiteratorusedtoiteratethroughastring.

  • reverse_iterator ReversibleContainer

    Iteratorusedtoiteratebackwardsthroughastring.

    const_reverse_iterator ReversibleContainer

    Constiteratorusedtoiteratebackwardsthroughastring.

    iteratorbegin() Container Returnsaniteratorpointingtothebeginningofthestring.

    iteratorend() Container Returnsaniteratorpointingtotheendofthestring.

    const_iteratorbegin()const Container Returnsaconst_iteratorpointingtothebeginningofthestring.

    const_iteratorend()const Container Returnsaconst_iteratorpointingtotheendofthestring.

    reverse_iteratorrbegin() ReversibleContainer

    Returnsareverse_iteratorpointingtothebeginningofthereversedstring.

    reverse_iteratorrend() ReversibleContainer

    Returnsareverse_iteratorpointingtotheendofthereversedstring.

    const_reverse_iteratorrbegin()const ReversibleContainer

    Returnsaconst_reverse_iteratorpointingtothebeginningofthereversedstring.

    const_reverse_iteratorrend()const ReversibleContainer

    Returnsaconst_reverse_iteratorpointingtotheendofthereversedstring.

    size_typesize()const Container Returnsthesizeofthestring.size_typelength()const basic_string Synonymforsize().size_typemax_size()const Container Returnsthelargestpossible

    sizeofthestring.size_typecapacity()const basic_string Seebelow.boolempty()const Container trueifthestring'ssizeis0.referenceoperator[](size_typen) Random

    AccessContainer

    Returnsthen'thcharacter.

    const_referenceoperator[](size_typen)const RandomAccessContainer

    Returnsthen'thcharacter.

    constcharT*c_str()const basic_string Returnsapointertoanullterminatedarrayofcharactersrepresentingthestring'scontents.

    constcharT*data()const basic_string Returnsapointertoanarrayofcharacters(notnecessarilynullterminated)representingthestring'scontents.

  • basic_string() Container Createsanemptystring.basic_string(constbasic_string&s,size_typepos=0,size_typen=npos)

    Container,basic_string

    Generalizationofthecopyconstructor.

    basic_string(constcharT*) basic_string Constructastringfromanullterminatedcharacterarray.

    basic_string(constcharT*s,size_typen) basic_string Constructastringfromacharacterarrayandalength.

    basic_string(size_typen,charTc) Sequence Createastringwithncopiesofc.

    templatebasic_string(InputIteratorfirst,InputIteratorlast)

    Sequence Createastringfromarange.

    ~basic_string() Container Thedestructor.basic_string&operator=(constbasic_string&) Container Theassignmentoperatorbasic_string&operator=(constcharT*s) basic_string Assignanullterminated

    characterarraytoastring.basic_string&operator=(charTc) basic_string Assignasinglecharactertoa

    string.voidreserve(size_t) basic_string Seebelow.voidswap(basic_string&) Container Swapsthecontentsoftwo

    strings.iteratorinsert(iteratorpos,constT&x)

    Sequence Insertsxbeforepos.

    templatevoidinsert(iteratorpos,InputIteratorf,InputIteratorl)

    Sequence Insertstherange[first,last)beforepos.

    voidinsert(iteratorpos,size_typen,constT&x)

    Sequence Insertsncopiesofxbeforepos.

    basic_string&insert(size_typepos,constbasic_string&s) basic_string Insertssbeforepos.basic_string&insert(size_typepos,constbasic_string&s,size_typepos1,size_typen)

    basic_string Insertsasubstringofsbeforepos.

    basic_string&insert(size_typepos,constcharT*s) basic_string Insertssbeforepos.basic_string&insert(size_typepos,constcharT*s,size_typen)

    basic_string Insertsthefirstncharactersofsbeforepos.

    basic_string&insert(size_typepos,size_typen,charTc) basic_string Insertsncopiesofcbeforepos.

    basic_string&append(constbasic_string&s) basic_string Appendsto*this.basic_string&append(constbasic_string&s,size_typepos,size_typen)

    basic_string Appendasubstringofsto*this.

    basic_string&append(constcharT*s) basic_string Appendsto*this.basic_string&append(constcharT*s,size_typen) basic_string Appendthefirstncharacters

    ofsto*this.basic_string&append(size_typen,charTc) basic_string Appendncopiesofcto

    *this.templatebasic_string&append(InputIteratorfirst,InputIteratorlast)

    basic_string Appendarangeto*this.

  • voidpush_back(charTc) basic_string Appendasinglecharacterto*this.

    basic_string&operator+=(constbasic_string&s) basic_string Equivalenttoappend(s).basic_string&operator+=(constcharT*s) basic_string Equivalenttoappend(s)basic_string&operator+=(charTc) basic_string Equivalenttopush_back(c)iteratorerase(iteratorp) Sequence Erasesthecharacterat

    positionpiteratorerase(iteratorfirst,iteratorlast) Sequence Erasestherange[first,

    last)

    basic_string&erase(size_typepos=0,size_typen=npos) basic_string Erasesarange.voidclear() Sequence Erasestheentirecontainer.voidresize(size_typen,charTc=charT()) Sequence Appendscharacters,orerases

    charactersfromtheend,asnecessarytomakethestring'slengthexactlyncharacters.

    basic_string&assign(constbasic_string&) basic_string Synonymforoperator=basic_string&assign(constbasic_string&s,size_typepos,size_typen)

    basic_string Assignsasubstringofsto*this

    basic_string&assign(constcharT*s,size_typen) basic_string Assignsthefirstncharactersofsto*this.

    basic_string&assign(constcharT*s) basic_string Assignsanullterminatedarrayofcharactersto*this.

    basic_string&assign(size_typen,charTc) Sequence Erasestheexistingcharactersandreplacesthembyncopiesofc.

    templatebasic_string&assign(InputIteratorfirst,InputIteratorlast)

    Sequence Erasestheexistingcharactersandreplacesthemby[first,last)

    basic_string&replace(size_typepos,size_typen,constbasic_string&s)

    basic_string Replacesasubstringof*thiswiththestrings.

    basic_string&replace(size_typepos,size_typen,constbasic_string&s,size_typepos1,size_typen1)

    basic_string Replacesasubstringof*thiswithasubstringofs.

    basic_string&replace(size_typepos,size_typen,constcharT*s,size_typen1)

    basic_string Replacesasubstringof*thiswiththefirstn1charactersofs.

    basic_string&replace(size_typepos,size_typen,constcharT*s)

    basic_string Replacesasubstringof*thiswithanullterminatedcharacterarray.

    basic_string&replace(size_typepos,size_typen,size_typen1,charTc)

    basic_string Replacesasubstringof*thiswithn1copiesofc.

    basic_string&replace(iteratorfirst,iteratorlast,constbasic_string&s)

    basic_string Replacesasubstringof*thiswiththestrings.

    basic_string&replace(iteratorfirst,iteratorlast,constcharT*s,size_typen)

    basic_string Replacesasubstringof*thiswiththefirstncharactersofs.

    basic_string&replace(iteratorfirst,iteratorlast,constcharT*s)

    basic_string Replacesasubstringof*thiswithanullterminated

  • characterarray.basic_string&replace(iteratorfirst,iteratorlast,size_typen,charTc)

    basic_string Replacesasubstringof*thiswithncopiesofc.

    templatebasic_string&replace(iteratorfirst,iteratorlast,InputIteratorf,InputIteratorl)

    basic_string Replacesasubstringof*thiswiththerange[f,l)

    size_typecopy(charT*buf,size_typen,size_typepos=0)const

    basic_string Copiesasubstringof*thistoabuffer.

    size_typefind(constbasic_string&s,size_typepos=0)const

    basic_string Searchesforsasasubstringof*this,beginningatcharacterposof*this.

    size_typefind(constcharT*s,size_typepos,size_typen)const

    basic_string Searchesforthefirstncharactersofsasasubstringof*this,beginningatcharacterposof*this.

    size_typefind(constcharT*s,size_typepos=0)const basic_string Searchesforanullterminatedcharacterarrayasasubstringof*this,beginningatcharacterposof*this.

    size_typefind(charTc,size_typepos=0)const basic_string Searchesforthecharacterc,beginningatcharacterpositionpos.

    size_typerfind(constbasic_string&s,size_typepos=npos)const

    basic_string Searchesbackwardforsasasubstringof*this,beginningatcharacterpositionmin(pos,size())

    size_typerfind(constcharT*s,size_typepos,size_typen)const

    basic_string Searchesbackwardforthefirstncharactersofsasasubstringof*this,beginningatcharacterpositionmin(pos,size())

    size_typerfind(constcharT*s,size_typepos=npos)const basic_string Searchesbackwardforanullterminatedcharacterarrayasasubstringof*this,beginningatcharactermin(pos,size())

    size_typerfind(charTc,size_typepos=npos)const basic_string Searchesbackwardforthecharacterc,beginningatcharacterpositionmin(pos,size().

    size_typefind_first_of(constbasic_string&s,size_typepos=0)const

    basic_string Searcheswithin*this,beginningatpos,forthefirstcharacterthatisequaltoanycharacterwithins.

    size_typefind_first_of(constcharT*s,size_typepos,size_typen)const

    basic_string Searcheswithin*this,beginningatpos,forthefirstcharacterthatisequaltoanycharacterwithinthefirstncharactersofs.

    size_typefind_first_of(constcharT*s,size_typepos=0)const

    basic_string Searcheswithin*this,beginningatpos,forthefirst

  • characterthatisequaltoanycharacterwithins.

    size_typefind_first_of(charTc,size_typepos=0)const basic_string Searcheswithin*this,beginningatpos,forthefirstcharacterthatisequaltoc.

    size_typefind_first_not_of(constbasic_string&s,size_typepos=0)const

    basic_string Searcheswithin*this,beginningatpos,forthefirstcharacterthatisnotequaltoanycharacterwithins.

    size_typefind_first_not_of(constcharT*s,size_typepos,size_typen)const

    basic_string Searcheswithin*this,beginningatpos,forthefirstcharacterthatisnotequaltoanycharacterwithinthefirstncharactersofs.

    size_typefind_first_not_of(constcharT*s,size_typepos=0)const

    basic_string Searcheswithin*this,beginningatpos,forthefirstcharacterthatisnotequaltoanycharacterwithins.

    size_typefind_first_not_of(charTc,size_typepos=0)const basic_string Searcheswithin*this,beginningatpos,forthefirstcharacterthatisnotequaltoc.

    size_typefind_last_of(constbasic_string&s,size_typepos=npos)const

    basic_string Searchesbackwardwithin*this,beginningatmin(pos,size()),forthefirstcharacterthatisequaltoanycharacterwithins.

    size_typefind_last_of(constcharT*s,size_typepos,size_typen)const

    basic_string Searchesbackwardwithin*this,beginningatmin(pos,size()),forthefirstcharacterthatisequaltoanycharacterwithinthefirstncharactersofs.

    size_typefind_last_of(constcharT*s,size_typepos=npos)const

    basic_string Searchesbackward*this,beginningatmin(pos,size()),forthefirstcharacterthatisequaltoanycharacterwithins.

    size_typefind_last_of(charTc,size_typepos=npos)const basic_string Searchesbackward*this,beginningatmin(pos,size()),forthefirstcharacterthatisequaltoc.

    size_typefind_last_not_of(constbasic_string&s,size_typepos=npos)const

    basic_string Searchesbackwardwithin*this,beginningatmin(pos,size()),forthefirstcharacterthatisnotequaltoanycharacterwithins.

    size_typefind_last_not_of(constcharT*s,size_typepos,size_typen)const

    basic_string Searchesbackwardwithin*this,beginningatmin(pos,size()),forthefirstcharacterthatisnotequaltoany

  • characterwithinthefirstncharactersofs.

    size_typefind_last_not_of(constcharT*s,size_typepos=npos)const

    basic_string Searchesbackward*this,beginningatmin(pos,size()),forthefirstcharacterthatisnotequaltoanycharacterwithins.

    size_typefind_last_not_of(charTc,size_typepos=npos)const

    basic_string Searchesbackward*this,beginningatmin(pos,size()),forthefirstcharacterthatisnotequaltoc.

    basic_stringsubstr(size_typepos=0,size_typen=npos)const

    basic_string Returnsasubstringof*this.

    intcompare(constbasic_string&s)const basic_string Threewaylexicographicalcomparisonofsand*this.

    intcompare(size_typepos,size_typen,constbasic_string&s)const

    basic_string Threewaylexicographicalcomparisonofsandasubstringof*this.

    intcompare(size_typepos,size_typen,constbasic_string&s,size_typepos1,size_typen1)const

    basic_string Threewaylexicographicalcomparisonofasubstringofsandasubstringof*this.

    intcompare(constcharT*s)const basic_string Threewaylexicographicalcomparisonofsand*this.

    intcompare(size_typepos,size_typen,constcharT*s,size_typelen=npos)const

    basic_string Threewaylexicographicalcomparisonofthefirstmin(len,traits::length(s)charactersofsandasubstringof*this.

    templatebasic_stringoperator+(constbasic_string&s1,constbasic_string&s2)

    basic_string Stringconcatenation.Aglobalfunction,notamemberfunction.

    templatebasic_stringoperator+(constcharT*s1,constbasic_string&s2)

    basic_string Stringconcatenation.Aglobalfunction,notamemberfunction.

    templatebasic_stringoperator+(constbasic_string&s1,constcharT*s2)

    basic_string Stringconcatenation.Aglobalfunction,notamemberfunction.

    templatebasic_stringoperator+(charTc,constbasic_string&s2)

    basic_string Stringconcatenation.Aglobalfunction,notamemberfunction.

    templatebasic_stringoperator+(constbasic_string&s1,charTc)

    basic_string Stringconcatenation.Aglobalfunction,notamemberfunction.

    templatebooloperator==(constbasic_string&s1,constbasic_string&s2)

    Container Stringequality.Aglobalfunction,notamemberfunction.

    templatebooloperator==(constcharT*s1,constbasic_string&s2)

    basic_string Stringequality.Aglobalfunction,notamemberfunction.

  • templatebooloperator==(constbasic_string&s1,constcharT*s2)

    basic_string Stringequality.Aglobalfunction,notamemberfunction.

    templatebooloperator!=(constbasic_string&s1,constbasic_string&s2)

    Container Stringinequality.Aglobalfunction,notamemberfunction.

    templatebooloperator!=(constcharT*s1,constbasic_string&s2)

    basic_string Stringinequality.Aglobalfunction,notamemberfunction.

    templatebooloperator!=(constbasic_string&s1,constcharT*s2)

    basic_string Stringinequality.Aglobalfunction,notamemberfunction.

    templatebooloperator

  • size_typecapacity()const Numberofelementsforwhichmemoryhasbeenallocated.Thatis,thesizetowhichthestringcangrowbeforememorymustbereallocated.capacity()isalwaysgreaterthanorequaltosize().

    constcharT*c_str()const Returnsapointertoanullterminatedarrayofcharactersrepresentingthestring'scontents.Foranystringsitisguaranteedthatthefirsts.size()charactersinthearraypointedtobys.c_str()areequaltothecharacterins,andthats.c_str()[s.size()]isanullcharacter.Note,however,thatitnotnecessarilythefirstnullcharacter.Characterswithinastringarepermittedtobenull.

    constcharT*data()const Returnsapointertoanarrayofcharacters,notnecessarilynullterminated,representingthestring'scontents.data()ispermitted,butnotrequired,tobeidenticaltoc_str().Thefirstsize()charactersofthatarrayareguaranteedtobeidenticaltothecharactersin*this.Thereturnvalueofdata()isneveranullpointer,evenifsize()iszero.

    basic_string(constbasic_string&s,size_typepos=0,size_typen=npos) Constructsastringfromasubstringofs.Thesubstringbeginsatcharacterpositionposandterminatesatcharacterpositionpos+norattheendofs,whichevercomesfirst.Thisconstructorthrowsout_of_rangeifpos>s.size().Notethatwhenposandnhavetheirdefaultvalues,thisisjustacopyconstructor.

    basic_string(constcharT*s) Equivalenttobasic_string(s,s+traits::length(s)).

    basic_string(constcharT*s,size_typen) Equivalenttobasic_string(s,s+n).

    basic_string&operator=(constcharT*s) Equivalenttooperator=(basic_string(s)).

    basic_string&operator=(charTc) Assignsto*thisastringwhosesizeis1andwhose

  • contentsisthesinglecharacterc.

    voidreserve(size_tn) Requeststhatthestring'scapacitybechangedthepostconditionforthismemberfunctionisthat,afteritiscalled,capacity()>=n.Youmayrequestthatastringdecreaseitscapacitybycallingreserve()withanargumentlessthanthecurrentcapacity.(Ifyoucallreserve()withanargumentlessthanthestring'ssize,however,thecapacitywillonlybereducedtosize().Astring'ssizecanneverbegreaterthanitscapacity.)reserve()throwslength_errorifn>max_size().

    basic_string&insert(size_typepos,constbasic_string&s) Ifpos>size(),throwsout_of_range.Otherwise,equivalenttoinsert(begin()+pos,s.begin(),s.end()).

    basic_string&insert(size_typepos,constbasic_string&s,size_typepos1,size_typen)

    Ifpos>size()orpos1>s.size(),throwsout_of_range.Otherwise,equivalenttoinsert(begin()+pos,s.begin()+pos1,s.begin()+pos1+min(n,s.size()pos1)).

    basic_string&insert(size_typepos,constcharT*s) Ifpos>size(),throwsout_of_range.Otherwise,equivalenttoinsert(begin()+pos,s,s+traits::length(s))

    basic_string&insert(size_typepos,constcharT*s,size_typen) Ifpos>size(),throwsout_of_range.Otherwise,equivalenttoinsert(begin()+pos,s,s+n).

    basic_string&insert(size_typepos,size_typen,charTc) Ifpos>size(),throwsout_of_range.Otherwise,equivalenttoinsert(begin()+pos,n,c).

    basic_string&append(constbasic_string&s) Equivalenttoinsert(end(),s.begin(),s.end()).

    basic_string&append(constbasic_string&s,size_typepos,size_typen)

    Ifpos>s.size(),throwsout_of_range.Otherwise,equivalenttoinsert(end(),s.begin()+pos,s.begin()+pos+min(n,s.size()pos)).

    basic_string&append(constcharT*s)

  • Equivalenttoinsert(end(),s,s+traits::length(s)).

    basic_string&append(constcharT*s,size_typen) Equivalenttoinsert(end(),s,s+n).

    basic_string&append(size_typen,charTc) Equivalenttoinsert(end(),n,c).

    templatebasic_string&append(InputIteratorfirst,InputIteratorlast)

    Equivalenttoinsert(end(),first,last).

    voidpush_back(charTc) Equivalenttoinsert(end(),c)

    basic_string&operator+=(constbasic_string&s) Equivalenttoappend(s).basic_string&operator+=(constcharT*s) Equivalenttoappend(s)basic_string&operator+=(charTc) Equivalenttopush_back(c)basic_string&erase(size_typepos=0,size_typen=npos) Ifpos>size(),throws

    out_of_range.Otherwise,equivalenttoerase(begin()+pos,begin()+pos+min(n,size()pos)).

    basic_string&assign(constbasic_string&s) Synonymforoperator=basic_string&assign(constbasic_string&s,size_typepos,size_typen)

    Equivalentto(butprobablyfasterthan)clear()followedbyinsert(0,s,pos,n).

    basic_string&assign(constcharT*s,size_typen) Equivalentto(butprobablyfasterthan)clear()followedbyinsert(0,s,n).

    basic_string&assign(constcharT*s) Equivalentto(butprobablyfasterthan)clear()followedbyinsert(0,s).

    basic_string&replace(size_typepos,size_typen,constbasic_string&s)

    Equivalenttoerase(pos,n)followedbyinsert(pos,s).

    basic_string&replace(size_typepos,size_typen,constbasic_string&s,size_typepos1,size_typen1)

    Equivalenttoerase(pos,n)followedbyinsert(pos,s,pos1,n1).

    basic_string&replace(size_typepos,size_typen,constcharT*s,size_typen1)

    Equivalenttoerase(pos,n)followedbyinsert(pos,s,n1).

    basic_string&replace(size_typepos,size_typen,constcharT*s)

    Equivalenttoerase(pos,n)followedbyinsert(pos,s).

    basic_string&replace(size_typepos,size_typen,size_typen1,charTc)

    Equivalenttoerase(pos,n)followedbyinsert(pos,n1,c).

    basic_string&replace(iteratorfirst,iteratorlast,constbasic_string&s)

    Equivalenttoinsert(erase(first,last),s.begin(),s.end()).

    basic_string&replace(iteratorfirst,iteratorlast,constcharT*s,size_typen)

    Equivalenttoinsert(erase(first,last),s,s+n).

    basic_string&replace(iteratorfirst,iteratorlast,

  • constcharT*s) Equivalenttoinsert(erase(first,last),s,s+traits::length(s)).

    basic_string&replace(iteratorfirst,iteratorlast,size_typen,charTc)

    Equivalenttoinsert(erase(first,last),n,c).

    templatebasic_string&replace(iteratorfirst,iteratorlast,InputIteratorf,InputIteratorl)

    Equivalenttoinsert(erase(first,last),f,l).

    size_typecopy(charT*buf,size_typen,size_typepos=0)const Copiesatmostncharactersfrom*thistoacharacterarray.Throwsout_of_rangeifpos>size().Otherwise,equivalenttocopy(begin()+pos,begin()+pos+min(n,size()),buf).Notethatthismemberfunctiondoesnothingotherthancopycharactersfrom*thistobufinparticular,itdoesnotterminatebufwithanullcharacter.

    size_typefind(constbasic_string&s,size_typepos=0)const Searchesforsasasubstringof*this,beginningatcharacterpositionpos.Itisalmostthesameassearch,exceptthatsearchtestselementsforequalityusingoperator==orauserprovidedfunctionobject,whilethismemberfunctionusestraits::eq.ReturnsthelowestcharacterpositionNsuchthatpos

  • characterposof*this.Thisisequivalenttofind(basic_string(s),pos).

    size_typefind(charTc,size_typepos=0)const Searchesforthecharacterc,beginningatcharacterpositionpos.Thatis,returnsthefirstcharacterpositionNgreaterthanorequaltopos,andlessthansize(),suchthat(*this)[N]comparesequaltoc.ReturnsnposifnosuchcharacterpositionNexists.

    size_typerfind(constbasic_string&s,size_typepos=npos)const Searchesbackwardforsasasubstringof*this.Itisalmostthesameasfind_end,exceptthatfind_endtestselementsforequalityusingoperator==orauserprovidedfunctionobject,whilethismemberfunctionusestraits::eq.ThismemberfunctionreturnsthelargestcharacterpositionNsuchthatN

  • beginningatpos,forthefirstcharacterthatisequaltoanycharacterwithins.Thisissimilartothestandardalgorithmfind_first_of,butdiffersbecausefind_first_ofcomparescharactersusingoperator==orauserprovidedfunctionobject,whilethismemberfunctionusestraits::eq.ReturnsthesmallestcharacterpositionNsuchthatpos
  • andsuchthat(*this)[N]doesnotcompareequaltoanycharacterin[s,s+n).Returnsnposifnosuchcharacterpositionexists.

    size_typefind_first_not_of(constcharT*s,size_typepos=0)const Equivalenttofind_first_not_of(s,pos,traits::length(s)).

    size_typefind_first_not_of(charTc,size_typepos=0)const ReturnsthesmallestcharacterpositionNsuchthatpos

  • thatisnotequaltoanycharacterwithin[s,s+n).Thatis,returnsthelargestcharacterpositionNsuchthatN
  • charactersofsandasubstringof*this.Equivalenttobasic_string(*this,pos,n).compare(basic_string(s,min(len,traits::length(s)))).

    templatebasic_stringoperator+(constbasic_string&s1,constbasic_string&s2)

    Stringconcatenation.Equivalenttocreatingatemporarycopyofs,appendings2,andthenreturningthetemporarycopy.

    templatebasic_stringoperator+(constcharT*s1,constbasic_string&s2)

    Stringconcatenation.Equivalenttocreatingatemporarybasic_stringobjectfroms1,appendings2,andthenreturningthetemporaryobject.

    templatebasic_stringoperator+(constbasic_string&s1,constcharT*s2)

    Stringconcatenation.Equivalenttocreatingatemporarycopyofs,appendings2,andthenreturningthetemporarycopy.

    templatebasic_stringoperator+(charTc,constbasic_string&s2)

    Stringconcatenation.Equivalenttocreatingatemporaryobjectwiththeconstructorbasic_string(1,c),appendings2,andthenreturningthetemporaryobject.

    templatebasic_stringoperator+(constbasic_string&s1,charTc)

    Stringconcatenation.Equivalenttocreatingatemporaryobject,appendingcwithpush_back,andthenreturningthetemporaryobject.

    templatebooloperator==(constcharT*s1,constbasic_string&s2)

    Stringequality.Equivalenttobasic_string(s1).compare(s2)==0.

    templatebooloperator==(constbasic_string&s1,constcharT*s2)

    Stringequality.Equivalenttobasic_string(s1).compare(s2)==0.

    templatebooloperator!=(constcharT*s1,constbasic_string&s2)

    Stringinequality.Equivalenttobasic_string(s1).compare(s2)!=0.

    templatebooloperator!=(constbasic_string&s1,constcharT*s2)

    Stringinequality.Equivalenttobasic_string(s1).compare(s2)!=0.

    templatebooloperator

  • booloperator>(basic_istream&is,basic_string&s)

    Readssfromtheinputstreamis.Specifically,itskipswhitespace,andthenreplacesthecontentsofswithcharactersreadfromtheinputstream.Itcontinuesreadingcharactersuntilitencountersawhitespacecharacter(inwhichcasethatcharacterisnotextracted),oruntilendoffile,or,ifis.width()isnonzero,untilithasreadis.width()characters.Thismemberfunctionresetsis.width()tozero.

    templatebasic_ostream&operator,doesnotskipwhitespace.Asthenamesuggests,itismostcommonlyusedtoreadanentirelineoftextpreciselyasthelineappearsinaninputfile.

    templatebasic_istream&getline(basic_istream&is,basic_string&s)

    Equivalenttogetline(is,s,is.widen('\n\)).

    Notes

    Seealso

    rope,vector,CharacterTraits

    STLMainPage

  • ContactUs|SiteMap|Trademarks|Privacy|UsingthissitemeansyouacceptitsTermsofUseCopyright20092014SiliconGraphicsInternational.Allrightsreserved.