ВНИМАНИЕ
Âåëè÷èía npOs яâëяåòñя ñòaòè÷åñêèì ÷ëåíîì êëañña string è ïðåäñòaâëяåò ñîáîé ñaìîå áîëüøîå ïîëîæèòåëüíîå ÷èñëî òèïa size_type (âñå åäèíèöû â áèòîâîì ïðåäñòaâëåíèè).
Î÷èñòêó âñåé ñòðîêè ìîæíî âûïîëíèòü ñ ïîìîùüþ ôóíêöèè Clear: vOid Clear();
Äëя çaìåíû ÷añòè ñòðîêè ñëóæèò ôóíêöèя replaCe: replaCe(size_type pOs1, size_type n1, COnst string& str);
replaCe(size_type pOs1, size_type n1, COnst string& str, size_type pOs2,
size_type n2);
Çäåñü pOs1 — ïîçèöèя âûçûâaþùåé ñòðîêè, ía÷èíaя ñ êîòîðîé âûïîëíяåòñя çaìå- ía, n1 — êîëè÷åñòâî óäaëяåìûõ ýëåìåíòîâ, pOs2 — ïîçèöèя ñòðîêè str, ía÷èíaя ñ êîòîðîé îía âñòaâëяåòñя â âûçûâaþùóþ ñòðîêó, n2 — êîëè÷åñòâî âñòaâëяåìûõ ýëåìåíòîâ ñòðîêè str. Åñëè pOs1 èëè pOs2 áîëüøå äëèíû ñîîòâåòñòâóþùåé ñòðîêè, ïîðîæäaåòñя èñêëþ÷åíèå Out_Of_range. Åñëè äëèía ðåçóëüòaòa áîëüøå ìaêñè- ìaëüíî äîïóñòèìîé äëèíû ñòðîêè, ïîðîæäaåòñя èñêëþ÷åíèå length_errOr.
Òðåòüя ôîðìa ôóíêöèè çaìåíû ïîçâîëяåò çaìåíèòü n1 ñèìâîëîâ âûçûâaþùåé ñòðîêè ía n2 ñèìâîëîâ ñòðîêè ñòaðîãî ñòèëя s:
replaCe(size_type pOs1, size_type n1, COnst Char* s, size_type n2);
Äëя îáìåía ñîäåðæèìîãî äâóõ ñòðîê ñëóæèò ôóíêöèя swap: swap(string& s);
Äëя âûäåëåíèя ÷añòè ñòðînè ñëóæèò ôóíêöèя substr:
string substr(size_type pOs = 0, size_type n = npOs) COnst;
Ýòa ôóíêöèя âîçâðaùaåò ïîäñòðîêó âûçûâaåìîé ñòðîêè äëèíîé n, ía÷èíaя ñ ïî- çèöèè pOs. Åñëè pOs áîëüøå äëèíû ñòðîêè, ïîðîæäaåòñя èñêëþ÷åíèå Out_Of_range. Åñëè n áîëüøå äëèíû ñòðîêè, âîçâðaùaåòñя âåñü îñòaòîê ñòðîêè.
Èíîãäa òðåáóåòñя ïðåîáðaçîâûâaòü îáúåêòû òèïa string â ñòðînè ñòaðîãî ñòèëя. Äëя ýòîãî ïðåäíaçía÷åía ôóíêöèя C_str:
COnst Char* C_str() COnst;
Îía âîçâðaùaåò êîíñòaíòíûé óêaçaòåëü ía îêaí÷èâaþùóþñя íóëü-ñèìâîëîì ñòðîêó. Ýòó ñòðîêó íåëüçя ïûòaòüñя èçìåíèòü. Óêaçaòåëü, êîòîðûé ía íåå ññûëa- åòñя, ìîæåò ñòaòü íåêîððåêòíûì ïîñëå ëþáîé îïåðaöèè íaä ñòðîêîé-èñòî÷íèêîì.
Àíaëîãè÷íî ðaáîòaåò ôóíêöèя data, ça òåì èñêëþ÷åíèåì, ÷òî íå äîáaâëяåò â êî- íåö ñòðîêè íóëü-ñèìâîë:
COnst Char* data() COnst;
Ôóíêöèя COpy êîïèðóåò â ìaññèâ s n ýëåìåíòîâ âûçûâaþùåé ñòðîêè, ía÷èíaя ñ ïîçèöèè pOs. Íóëü-ñèìâîë â ðåçóëüòèðóþùèé ìaññèâ íå çaíîñèòñя. Ôóíêöèя âîçâðaùaåò êîëè÷åñòâî ñêîïèðîâaííûõ ýëåìåíòîâ:
size_type COpy(Char* s, size_type n, size_type pOs = 0) COnst;
Ïðèìåð èñïîëüçîâaíèя ôóíêöèé èçìåíåíèя ñîäåðæèìîãî ñòðîê:
inClude inClude using namespaCe std; int main (){
string s1("ïðåêðañíaя êîðîëåâa"), s2("ëå"), s3("êîðîâa"); COut << "s1= "<< s1 << endl;
COut << "s2= "<< s2 << endl; COut << "s3= "<< s3 << endl;
// Ïðèìåíåíèå ôóíêöèè insert:
COut << "ïîñëå insert:" << endl;
COut << "s3= " << s3.insert(4, s2) << endl; COut << "s3= " << s3.insert(7, "ê") << endl;
// Ïðèìåíåíèå ôóíêöèè erase: s1.erase(0,3);
COut << "ïîñëå erase:" << endl;
COut << "s1= " << s1.erase(12,2) << endl;
// Ïðèìåíåíèå ôóíêöèè replaCe:
COut << "ïîñëå replaCe:" << endl;
COut << "s1= " << s1.replaCe(0,3, s3, 4,2) << endl;
}
Ðåçóëüòaò ðaáîòû ïðîãðaììû:
s1= ïðåêðañíaя êîðîëåâa s2= ëå
s3= êîðîâa ïîñëå insert:
s3= êîðîëåâa s3= êîðîëåâêa ïîñëå erase:
s1= êðañíaя êîðîâa ïîñëå replaCe:
s1= ëåñíaя êîðîâa
Do'stlaringiz bilan baham: |