Mavzu: satrlar va kengaytirilgan belgilar


Download 71.88 Kb.
bet31/31
Sana25.11.2021
Hajmi71.88 Kb.
#177287
1   ...   23   24   25   26   27   28   29   30   31
Bog'liq
10- Amaliyot

#include

#inclde

using namespace std;

int main(){

string s1=”01234567893456ab2csef”,s2 =”456”,s3=”ghk2”;

int i,j; i=s1.find(s2); j=s1.rfind(s2);

cout << i; // 4

cout << j; // 11

cout << s1.find(‘3’) <

cout << s1.rfind(‘3’) <

cout << s1.find_first_of (s3) <

cout << s1.find_last_of (s3) <

cout << s1.find_first_not_of (s2) <

cout << s1.find_last_not_of (s2) <

getch();

}
Satrni qismini solishtirish

Satrlarni to‘liq solishtirish uchun solishtirish amallari ishlatiladi, qismini solishtirish uchun compare funksiyasi ishlatiladi:

int compare ( const string &str) const;

int compare (unsigned int pos1, unsigned int n1, const string & str) const;

int compare (unsigned int pos1, unsigned int n1, const string & str, unsigned int pos2, unsigned int n2) const;

Funksiya ni birinchi shakli ikki satrni butunlay solishtiradi va manfiy son qaytaradi chaqiruvchi satr str dan kichik bo‘lsa, 0 qaytaradi agar ular teng bo‘lsa va musbat son qaytaradi chaqiruvchi satr katta bo‘lsa.

Ikkinchi shaklda xudii oldingiday amallar bajariladi faqat chaqiruvchi satrni pos1 o‘rnidan boshlab n1 ta belgili satr str satr bilan solishtiriladi.

Uchinchi shaklda chaqiruvchi satrdan pos1 o‘rnidan boshlab n1 ta belgi va str satrdan ros2 o‘rnidan boshlab n2 ta belgi o‘zaro solishtiriladi.

Yangi va eski turdagi satrlarni solishtirish yuqorida ko‘rsatilganday funksiyalar mavjud. Misol:

#include

#inclde

using namespace std;

int main(){

string s1=”01234567893456ab2csef”,s2 =”456”,s3=”ghk”;

cout << “s1=”<

cout << “s2=”<

cout << “s3=”<

if (s2.compare(s3)>0) cout << “s2>s3”<

if (s2.compare(s3)==0) cout << “s2=s3”<

if (s2.compare(s3)<0) cout << “s2

if (s1.compare(4,6,s2)>0) cout << “s1[4-9]>s2”<

if (s1.compare(5,2,s2,1,2)==0) cout << “s1[5-6]=s2[1-2]”<

getch();

}

Satrni xususiyatlarini aniqlash funksiyalari

String sinfida satrini uzunligi, bo‘shligi va xotira xajmini aniqlaydigan funksiyalar bor.

unsigned int size() const; // chaqiruvchi satrni xotiradagi xajmi

unsigned int length() const; // chaqiruvchi satrni elementlar soni

unsigned int max_size() const; // satrni max uzunligi(4294967295)

unsigned int capacity() const; // satr egallagan xotira xajmi



bool empty() const; // Rost agar satr bo‘sh bo‘lsa
Download 71.88 Kb.

Do'stlaringiz bilan baham:
1   ...   23   24   25   26   27   28   29   30   31




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling