0% found this document useful (0 votes)
36 views

Master String Và 1 Số Hàm Thông Dụng c++

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
36 views

Master String Và 1 Số Hàm Thông Dụng c++

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 21
Contents : kich thudc va pham vi cdc kiéu dir ligu..... Chuyén dé String trong C+... Van dé 1: Co ban.. Van dé 2: Cac thao tée voi chudi qua ham 1. “reverse” function :déo nguge mét chudi 2. Substring : Chudi con... 3.erase: X6a ky tw. 4. insert () chen céc ky tyr trong chudi 6 vi tri xe dinh .... 5. Replace : thay thé... 6 So sdnh 2 chudi . 7. sap xép chudi STACK C++.. QUEUE... PRIORITY QUEUE.... gece Min heap :.... priority_queue v: Vector trong C++ .. Khai bao vector. M6t s6 chite nang lién quan dén viée vector :... Vector of Vectors in C++ STL. Chuyén dé cc ham str dung voi mang... 1, std :: sort () tong C ++ STL. 2,Tim min va max trong m6t mang : 3, upper_bound () va Lower_bound () c++... | HUY INIT ee 4, memset in C++... kich thuée va pham vi céc kiéu dir ligu short int 2 -32,768 to 32,767 unsigned short int 2 0 to 65,535 unsigned int 4 0 to 4,294,967,295 int 4 -2,147,483,648 to 2,147,483,647 long int 8 -2,147,483,648 to 2,147,483,647 unsigned long int 4 0 to 4,294,967,295 long long int 8 -(263) to (2463)-1 unsigned long long int 8 0 to 18,446,744,073,709,551,615 signed char 1 -128 to 127 unsigned char i 0 to 255 float 4 double 8 tt/- 1.7e +/- 308 (~15 digits long double is [-/- 1.7e +/- 308 (~15 digits’ wehar_t 2or4 1 wide character ~ Luu ¥ : Céc gid tri trén c6 thé khdc nhau gitta céc trinh bién dich. Trong vi du trén, chung ta da xem xét GCC 64 bit. Chuyén dé String trong C++ Van dé 1: Co ban 1.khai bao ,nhp chudi: HUYINIT [Foe2 | #include #include using namespace std; int mainQ { string s1,s25 cin >> s13 /hello world cout < #include using namespace std; int main() { string Strings String = "A" +"B";// error String = String + "C"; String = "B" + String; cout << String << endl; return 0); } w 3.chuyén chuéi thudng thanh hoa for(int i=0si= a & &sfi]<='2) sfil=slil- chuyén hoa thanh thuong: for(int i=0: if(s[i]>= Z)) slil=slil+' | 4, chuyén chudi sang so for(int i=0;i using namespace std; int main() { string str = "geeksforgeeks" reverse(str.begin(), str.end()); cout << str; // skeegrofskeeg return 0); } 2. Substring : Chudi con ~ #include #include using namespace std; int main() { string s1 = "Geeks"; string r = sl.substr(1, 3); cout << 1; //eek return 0); } Vd2: #include #include using namespace std; int mainQ { string s = "dog:cat"; | INIT Ea int pos = s.find(":"); // Find position of using findQ string sub substr(pos + 1)5 cout << sub; //cat return 0; } 3.erase: Xéa ky tu’ Link : https://www.geeksforgeeks.org/stdstringerase-in-cpp/ Xe | #include #include using namespace std; int mainQ { string str("Hello World!"); str.erase()3 //con cé nhiéu céch khdc cout << str; return 0; } Cu phap 2: X6a tat ca céc ky tr sau vi tri ‘pos’ : str.erase(1); Before erase : Hello World!=> After erase : H Cu phap 3: X6a t6i da cac y ty len ctia * this, bat dau tir chi muc idx. str.erase(1, 4); Before erase : Hello r erase : H World! Cai phap 4: X6a ky tr don tai vi tri trinh lip. str.erase(str.begin() + Before erase : Hello World!=>After erase : Hell World! 4. insert () chén céc ky ty trong chudi 6 vi tri x4e dinh Link : https:/www.geeksforgeeks.org/stdstringinsert-in-c/ #include #include using namespace std; void insertDemo(string str1, string str2) { // Inserts str2 in strl starting from 6th index of strl strl.insert(6, str2); cout << str; // Hello GeeksforGeeks World! | HUY INIT } int main() { string str1("Hello World! ")s string str2("GeeksforGeeks "); insertDemo(strl, str2); return 0; } 5. Replace : thay thé Link : https://www.geeksforgeeks.org/stdstringreplace-stdstringreplace_if-c/ #include using namespace std; void replaceDemo(string s1, string s2, string s3, string s4) { // Replaces 7 characters from Oth index by s2 sl.replace((), 7, s2)3 cout << sl << endl; // Demonstration of replace s4.replace(0, 3, "Hello "); cout << s4 << endl; // Hello World ! s4.replace(6, 5, $3, 0, 5); cout << s4 << endl; //Hello Geeks ! s4.replace(6, 5, "to all’, 6) cout << s4 << endl; // Hello to all ! // Replaces | character from 12th index of s4 with 3 copies of '!’ s4.replace(12, 1, 3,'!)s cout << s4 << endl; // Hello to all!!!! int main() { string s1 = "Example of replace"; string s2 = "Demonstration"; eeksforGeeks"; string s4 = "HeyWorld !"; replaceDemo(s1, s2, s3, s4); return 0); } 6 So sinh 2 chudi #include | INIT [Paes | using namespace std; void relationalOperation(string s1, string s2) { if (s1 != s2) cout << sl << "is not equal to "<< s2 << endl; if (s1 > s2) cout << sl << "is greater than "<< s2 << endl; else cout << s2 << " is greater than "<< s1 << endl; //661 is not equal to 6601 //661 is greater than 6601 int main() { string s1(°661"); string s2("6601"); relationalOperation(s1, s2); return 0); eae ~~ l.sap sip cdc ky tuwk trong mmoagt Gi “N https://www.geeksforgeeks.org/sort-string-characters/ #include using namespace std; int main() { string s = "geeksforgeeks"; sort(s.begin(), s.end()); cout << s3 //eeeefggkkorss return 0; } 2.sap x€p cac chudi trong mot chudi Vd ‘Hay sap dat lai cac phan tr trong mang sao cho khi néi cac sé lai voi nhau ta duoc mét sé Idn nhat. vd :A[] = {54, 546, 548, 60 } =>s6 lon nhat 1a 6054854654, } #include using namespace std; bool cmp(string a,string b){ | HUY INIT Es] string ab=a+b; string ba=b+a; return (ab>ba); int main({ string s[100005]; int njcin>>n; for(int i=0si>sfi]3 sort(s,s+n,cmp); for(int i=0si using namespace std; void showstack(stack s) { while (!s.empty()) { cout<< s.top0<<' 5 s.pop()s } cout << \n; int main () { stack s3 | INIT Pros | sepush(10); s.push(30)s s.push(20); s.push(5)s s.push(1); // Stack becomes 10 30 20 5 1(top) showstack(s); //1 5 20 30 10 cout << s.size()< push (Q ham thém phan ttr'g' vao cudi hang doi, Ham pop () xéa phan tir dau tién cia hang doi. #include #include using namespace std; void showq(queue gq) { queue g = gq; //note while (!g.empty() { cout<< g.front()<<' '3 g-pop03 | HUYINIT [reo | } cout << \n3 int main() { queue gquiz; ‘gquiz.push(10)); gquiz.push(20); gquiz.push(30); showg(gquiz); //10 20 30 cout < #include using namespace std; void showpq(priority_queue gq) { priority_queue g = gq; while (!g.empty() { cout << g.top0<<' 5 g-pop()s } cout << \n'; }op vin; , duoc thi dc bié an tu lon nhat trong tat ca cac phan tu ir tr RhOng tang dan (do dé ching ta c6 thé 6 mic dé uu tién { thir tu c6 dinh}). int main () { priority_queue gquiz; gquiz.push(0); gquiz.push(30)s HUY INIT [roe 10 | ‘gquiz.push(20); gquiz.push(5)3 gquiz.push(1); showpq(gquiz); //30 20 10 5 1 cout << gquiz.size()< #include using namespace std; void showpq(priority_queue , greater > gq) { priority_queue , greater > g = gq; while (!g.empty()) { cout << g.top()<<" "5 g-pop0)s } cout << '\n3 int main Q { priority_queue , greater > gquiz; gquiz.push(10); gquiz.push(30); gquiz.push(20); gquiz.push(5); gquiz.push(1); showpq(gquiz); //1 5 10 20 30 cout<< gquiz.sizeQ)< using namespace std; int main() { priority_queue::value_type AnInt; priority_queue ql; AnInt = 20; ql.push(Anin0); AnInt = 303 ql.push(Anint); cout << ql.topQ) << endl; //30 return 0; } Vi du 2: #include using namespace std; int main() { priority_queue::value_type AString; priority_« queue q2; AString = "geeks for geeks"; AString be"; q2.push(AString); AString = "def"; q2.push(AString); AString = "zhi"; q2.push(AString); while (!q2.empty() { cout << q2.top0<<' 5 q2.pop0)3 }//ghi def abe return 0; Vector trong C++ - Vecto giéng nhu mang d6ng vi kha nang ty dong thay déi kich thude khi mét phan tir duge chén hoac x6a, voi viée liu trit cua chting sé duge ving chita tw dong xt ly. - Vector IA mét chudi cdc phan tir cé ciing kigu dit liéu, cing giéng nhu mang binh thuong trong C++. Vay thi tai sao phai ding né? +Dau tién, vector c6 thé ty tang kich thudc cura né mdi khi ta thuc hién them mét phan tir vao vector. +Thir 2, vector c6 thé ty gidi phong thoat ra khoi scope chtra vector do, delete[] nh con tro. +Thit ba 1a vector cung cap cdc ham can thiét dé chung ta cho thé thao téc voi mang mét céch dé dang. bién di bién'dich Khai bao vector #include /* Vector | chiéu */ vector first; /* tao vgctor Kiet dé ligu int */ vector second (4, tao Vector voi 4 phan tila 100 vector third (gece cond.end()) // lay tir dau dén cudi vector second vector four (thir hé khi ta thc hién xong doan code va ;¢ nay nham trénh rd ri bé nhé khi ta quén copy tir vector third /*Vector 2 chiéu*/ vector < vector > v; /* Tao vector 2 chiéu rong */ vector < vector > v (5, 10) ; /* khai bao vector 5x10 */ vector < vector > v (5) ; /* khai bao 5 vector 1 chidu rong*/ vector < vector > v (5, vector (10,1) ) ; //Khai béo vector 5*10 voi cdc phan tirkhoi tao gid i 1 Mét so chire nang lién quan dén viéc vector : Vong lap 1. begin () - Tra vé mét trinh lap tro dén phan tir dau tién trong vecto 2. end () - Tra vé mét trinh lap tro dén phan tr ly thuyét theo sau phan ti cudi init [Poe | cing trong vecto 3. rbegin () ,rend () 4. cbegin () ,cend () 5. crbegin () ,crend () Note : front :truy cap phai back :truy a end/cend : begin/cbegin : tra ve trinh lap vé dau Modifiers: B6 ngit: Z : : :gdn () Gén gia tri moi cho cdc phan ttr vecto bang cach thay the cac gid tri cit . push_back () -day cae phan tir vao mét vector tir phia sau . pop_back () - xda cac phan tu khoi mét vector tit phia sau. . insert () - chén cae phan tu mdi truéc phan tir 6 vi tri dugc chi dinh . erase () - Xda cac phan tir Khoi ving chua khdi vi tri hodc pham vi duge chi dinh, . swap () - déi ngi dung ctia mét vecto nay véi mét vecto kha cing loai. Kich thude c6 thé khae nhau. clear () loai bo tat ca cae phan tur cua ving chia vector 8. emplace () - mé réng ving chita bing cach chén phan ti mdi vio vi tri 9. emplace_back () - chén mét phan tir méi vao ving chita vecto, vao cudi vecto aD wvEwN Bal #include #include using namespace std; int mainQ) { vector v3 v.assign(5, 10); // fill the array with 10 five times for (int i= 031 < v.size(Q); i++) cout << vfi] << ""; //10 10 10 10 10 y.push_back(15); // inserts 15 to the last position int n = v.sizeQ; cout << "\n" << v[n = 1] //15 pop_back(); // removes last element for (int i = 031 < v.size()s i++) | INIT ieserrcaan cout << v[i] <<" "3 //10 10 10 10 10 v.insert(v.begin(), 5); // inserts 5 at the beginning cout << "\n" << v[0}; //S v.erase(v.begin()); // removes the first element cout << "\n" << v[0} /10 v.emplace(v.begin(), 5); // inserts at the beginning cout << "\n" << v[0}; //S v.emplace_back(2()); // Inserts 20 at the end n= v.size(); cout << "\n" << y[n - 1]; //20 v.clear();_ // erases the vector cout << "\n" << v.sizeQ); //0 // two vector to perform swap /Iv1.swap(v2); // Swaps v1 and v2 } Element access: Quyén truy cap phan tu: 1. reference operator |g stodn tu tham chiéu [g| - Tra vé mét tham chiéu dén phan tir 6 vi tri 'g' trong vecto . at (g) - Tra vé mot tham chiéu dén phan tr 6 vi tri'g' trong vecto . front () - Tra vé phan tur dau tién trong vecto . back () - Tra vé phan tu cudi cling trong vecto . data () - Tra vé mét con tro true tiép dén mang b6 nhé duge vector sir dung bén trong dé huu trir cdc phan tu thude so hiu cua no. UwWN #include using namespace std; int main() { vector gl; for (inti = 1; i <= 10; i++) gl.push_back(i * 10); cout << "\n" << g1[2]; /30 cout << "\n " << gl.at(4); //50 cout << "\n" << gl.front(); //10//*g|.begin() cout << "\n" << gl.back()3 //100//**(g1.end()-1) int® pos I.data(); cout << "\n" << *poss //10 | INIT EB) return 0); } Capacity = Ste chita_ . size ()- Tra vé sd phan tu trong vector. - resize (n) - Thay doi kich thude ving chia dé no chtta cae phan tr 'n . empty() - Tra vé ligu ving chita co trong hay khéng. 7 shrink to _fit() ) - Giam dung luong ctia vung chia dé phu hop voi kich thude cua no va huy tat ca cdc phan tir v uot qua dung lugng. 5. Reserve () - Yéu cau vecto céng suat it nhat du dé chira n phan tu. a Rene #include #include using namespace std; int main() { vector gl; for (int i= 1; i <= 5; i++) gl.push_back(i); cout << gl.size()<> vee; Chén trong Vecto cua Vecto #include #include using namespace std; int main() { vector > vec; // Initializing int num = 1(); // Elements to insert in column for (int i= 03 i < 45 i++) { vector vl; for (int j = 035 < 53 j+4) { vl .push_back(num); num += 5; } vec.push_back(v1); } for (int i = 05 i < vec.size(); i++) { for (int j = 03 j < vec[i].sizeQ; j++) cout << vecfiJfj] << ""; cout << endl; } return (0); 7*10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105*/ } Loai bo hoac Xéa trong mét Vecto Vecto #include #include using namespace std; int main() { vector > vec{ { 1, 2,3 },{4,5,6},{7,8,9}}s // Removing elements from the last row of the vector vec[2].pop_back(); | INIT ee vec[ | ].pop_back()3 for (int i = 03 i < 35 i++) { for (auto it = vec[i].begin()s it != vec[i].end()s it++) cout << #it << "3/123 cout << endl; W45 } W718 return 0); } Thu vién mau chuan (STL) https://www.geeksforgeeks.org/the-c-standard-template-library-stl/ https://www.geeksforgeeks.org/cpp-stl-tutorial/ #include using namespace std; int mainQ) { int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, O}; int n = sizeof(arr)/sizeof(arr[0]); sort(arr, arr+n); // string: sort(s.begin(), s.end() )s for (int i = 03 i < ns ++i) cout << arti] <<" 10123456789 return (0); } Vi dy 2: Lam thé nao dé sap xép theo thtr tu giam dan? #include using namespace std; int main( { int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 25 O}; int n = sizeof(arr)/sizeof(arr[0]); HUYINIT [roe it | sort(arr, arr+n, greater() 3 for (int i = 05 i using namespace std; struct Interval { int start, end; }5 // Compares two intervals according to staring times. bool comparelnterval(Interval il, Interval i2) { return (il.start < i2.start); } int main() { Interval arf] = { {6,5}, {1,9} 2,4}, (4,7) 35 int n = sizeof(arr)/sizeof(arr[0]); sort(arr, arr+n, comparelnterval); for (int i=03 i using namespace std; int main({ int n=3,m=3; int af3 13 }7(9,8,7,6,554535251}5 for(int i=0si()*/); for(int i=03i using namespace std; int mainO{ int n; cin>>n3 for(int i=03i>afi]; N2345 int x= *min_element(a,a+n); cout< using namespace std; int main() { int arr{] = { 10, 20, 30, 40, 50 }s vector v(arr,arr+5); //10 20 30 40 50 // using upper_bound int upper = upper_bound(arr, arr+5, 30) - arr; int lower = lower_bound(arr, arr+5, 30) - arr; | INIT Page 20 cout << (upper)< 40>30 cout << (lower); //2 <=> 30>=30 return 0; } 4, memset in C++ https://www.geeksforgeeks.org/memset-in-cpp/ #include #include using namespace std; int main() { char str[] = "geeksforgeeks"; memset(str, 't', sizeof(str)); cout << str<

You might also like