This time the C + + stl Some of the things to practice, STL is called the Standard Template Library, which is standardized templates, to use the STL, to understand the following basic concepts:
container: It can be understood as the place where the data is stored, some of the commonly used containers have a list stack (stack) dynamic array (vector), double-ended queue (deque) queue mapping (map)
cursor (iterator): You can interpret it as a pointer type, and many of the functions in the STL need to use them as parameters
algorithms: They usually need to be used in conjunction with containers and cursors, and you can easily perform common operations on the data in the container, such as sorting operations, finding the maximum elements of operations, etc.
I simply practice the container, this first list of two-way linked list code, but also just a simple practice, afraid to forget it.
#include <iostream> #include <list> #include <cstdlib>using namespace std;int main (int argc, char** argv) { int i; list<char>lst; for (i=0;i<10;i++) lst.push_back (' A ' + (rand ()%26)); cout<< "Original contents:"; List<char>::iterator p = lst.begin (); while (P! = Lst.end ()) { cout<<*p; p++; } cout<<endl; Sort the list lst.sort (); cout<< "Sorted contents:"; p = Lst.begin (); while (P! = Lst.end ()) { cout<<*p; p++; } return 0;}
Use of Map
#include <iostream> #include <map> #include <string>using namespace std;int main (int argc, char** argv) { int i; map<string,string>m; M.insert (pair<string,string> ("Yes", "no")); M.insert (pair<string,string> ("Up", "down"); M.insert (Pair<string,string> ("left", "right")); M.insert (pair<string,string> ("Cool", "Tong")); string S; cout<< "Enter word:"; cin>>s; Map<string,string>::iterator p; p = m.find (s); if (P! = m.end ()) cout<< "opposite:" <<p->second; else cout<< "Word not in map\n"; return 0;}
Application of queue
#include <iostream> #include <queue> #include <string>using namespace std;int main (int argc, char** argv) { queue<string>str_queue; Str_queue.push ("string1"); Str_queue.push ("string2"); Str_queue.push ("String3"); cout<< "The size of the queue is:" <<str_queue.size () <<endl; cout<< "The front One: " <<str_queue.front () <<endl; cout<< "The Back One: " <<str_queue.back () <<endl; Str_queue.pop (); Str_queue.pop (); Str_queue.pop (); if (Str_queue.empty ()) cout<< "The queue is empty" <<endl;return 0;
Use of vectors
#include <iostream> #include <vector>using namespace std;int main (int argc, char** argv) { vector< char>v; int i; for (i=0;i<10;i++) v.push_back (' A ' +i); for (i=0;i<10;i++) cout<<v[i]<< ""; cout<<endl; Vector<char>::iterator p = v.begin (), while (p!= v.end ()) {cout<<*p<< "";p + +;} return 0;}
Use of string
#include <iostream> #include <string>using namespace std;int main (int argc, char** argv) {string A= " ABCEDJNMBVNBM, cnm,bncm,bnm, NM,CNMM,KLNDSLKFL sdnsdklnf ls "; Cout<<a.length () <<endl;cout<<a.size () <<endl;cout<<a.capacity () <<endl;cout<<a.max_size () <<endl;return 0;} #include <iostream> #include <string> Using namespace Std;int main (int argc, char** argv) {string a= "Abcdefghi"; string b= "123456789"; string c= "cheer"; cout<& lt; " Compare strings A and B "<<endl;cout<<a.compare (b) <<endl; A.swap (b); cout<< "Output of A and B after swapping" <<endl<< "output a=" <<a<<endl<< "Output b=" <<b<<endl; cout<< "Find" <<endl; Cout<<b.find (' E ', 0) <<endl; cout<< "familiar with Replace" <<endl;cout<<b.replace (0,5,a) <<endl;cout<< "inserting insert" << Endl Cout<<b.insert (5,c) <<endl; return 0;} ******#include <iostream> #include <string> Using namespace Std;int main (int argc, char** argv) {string str ("This was an example sencetence."); cout<<str<<endl; Str.erase (10,8); cout<<str<<endl; Str.erase (Str.begin () +9); Delete 9th cout<<str<<endl; Str.erase (Str.begin () +5,str.end ()-9); Cout<<str<<endl;return 0;} Defines two string strings, and then calls Erase (), which is mostly a parameter issue. Only one parameter represents the deletion of the characters from the first position, begin () and end () are just the start and end positions, **************************************************************** #include <iostream> #include <string>using namespace std;int main (int argc, Char * * argv) {string str ("There is both needles in this haystack with needles."); String str2 ("needle"); int Found=str.find (STR2); if (found!=-1) cout<< "first ' neeedle ' found at:" <<found<<endl; Found=str.find ("Needle is small", found+1,6);/starting from POS find the position of the first n characters in the string s in the current string if (Found!=-1) cout<< "second ' needle ' found at:" <<found<<endl; Found=str.find ("haystack"); cout << ' haystack ' also found at: "<< found <<endl; Found=str.find ('. '); if (found!=-1) cout << "Period found at:" << found << Endl; From Str find the first place in the string str2 and delete, then add the following String Str.replace (Str.find (STR2), Str2.length (), "preposition"); Cout<<str<<endl;return 0;} #include <iostream> #include <string>using namespace std;int main (int argc, char** argv) {string base= "This is a TES T string. "; String str2= "n Example"; string str3= "sample phrase"; string str4= "useful."; String Str=base;cout<<str.replace (9,5,STR2) <<endl;cout<<str.replace (19,6,str3,7,6) << Endl;cout<<str.replace (8,10, "just a") <<ENDL;COUT<<STR.REplace (8,6, "A Shorty", 7) <<endl;cout<<str.replace (22,1,3, '! ') <<endl;cout<<str<<endl;return 0;} The result of the run is a example string. This is a example phrase. This is just a phrase. This is a short phrase. Hrase!!!. This was a short phrase!!!. --------------------------------Process exited with return value 0Press any key to continue ... *********************** #include <iostream> #include <string>using namespace std;int main (int argc, char** argv) {char changan[20]={'% '}; String str ("Xi ' an University of Post & Telecommunication"); int length =str.copy (changan,10,6);cout<< "Changan Contains: "<<changan<<endl;return 0;} Copy starts from the 6th position of the string and then copies 10 characters to the array *********************************************************************************** #include <iostream> #include <String>using namespace Std;int Main (int argc, char** argv) {string Firstlevel ("cn"); String Secondlevel ("Edu"); String Thirdlevel ("Xupt"); String Scheme ("http://"); string hostname; string URL; Hostname= "www." +thirdlevel+ '. ' +secondlevel+ '. ' +firstlevel; Url=scheme+hostname; cout<<url<<endl; return 0;} In C + +, you can concatenate strings ********************************************************************************************** #include <iostream> #include <string>int main () {string str1 ("Green Apple "); String str2 ("Red apple"); if (Str1.compare (str2)! = 0) cout << str1 << "is not" << str2 << ' \ n '; if (Str1.compare (6,5, "apple") = = 0) cout << "Still," << str1 << "is an apple\n"; if (Str2.compare (Str2.size () -5,5, "apple") = = 0) cout << "and" << str2 << "is also an apple\n"; if (Str1.compare (6,5,str2,4,5) = = 0) cout << "Therefore, both is apples\n"; return 0;
Some of the STL code in C + +