1. Basic operations of Vector
(1) add, delete, and obtain dynamic array elements
The Code is as follows:
# Include <iostream> # include <vector> using namespace STD; // add and delete array elements and obtain int main (void) {vector <int> V1; v1.push _ back (1); v1.push _ back (2); v1.push _ back (3); cout <"Len:" <v1.size () <Endl; cout <"Get Header element:" <v1.front () <Endl; // modify the value of header and tail elements. // when the return value of the function is left, a reference should be returned; v1.front () = 11; v1.back () = 55; while (v1.size ()> 0) {cout <v1.back () <""; // obtain the tail element; v1.pop _ back (); // Delete the tail element} cout <Endl ;}
(2) vector Initialization
The Code is as follows:
# Include <iostream> # include <vector> using namespace STD; void printv (vector <int> & V) {for (INT I = 0; I <v. size (); I ++) {cout <V [I] <"" ;}cout <Endl ;}int main (void) {// vector-like vector <int> V1; v1.push _ back (1); v1.push _ back (3); v1.push _ back (5); v1.push _ back (7 ); vector <int> v2 = V1; // initialization vector <int> V3 (v1.begin (), v1.begin () + 2 ); // object initialization/* // traversal of the vector for (I = A 0; I <v1.size (); I ++) {v1 [I] = I + 1 ;} int I; for (I = 0; I <v1.size (); I ++) {cout <V1 [I] <"" ;}cout <Endl; */printv (V1); // Enhanced memory of push_back (); vector <int> V5 (10); // The first 10 elements are initialized to 0; v5.push _ back (100); v5.push _ back (200); printv (V5); Return 0 ;}
(3) forward and reverse traversal of the iterator in the vector
The Code is as follows:
# Include <iostream> # include <vector> using namespace STD; int main (void) {vector <int> V1 (10); // The value of the initialization space is 0; for (INT I = 0; I <10; I ++) {v1 [I] = I + 1;} // iterator // 1 (BEGIN), 3, 5, (end) when it = v1.end (), it indicates that the container has been traversed; // The end () position should be behind 5; // forward traversal vector <int>: iterator it; for (IT = v1.begin (); it! = V1.end (); It ++) {cout <* It <";}cout <Endl; // reverse traversal vector <int >:: reverse_iterator rit; for (RIT = v1.rbegin (); rit! = V1.rend (); rit ++) {cout <* rit <"" ;}cout <Endl; return 0 ;}
(4) Delete elements in the vector
The Code is as follows:
# Include <iostream> # include <vector> using namespace STD; void printv (vector <int> & V) {for (INT I = 0; I <v. size (); I ++) {cout <V [I] <"" ;}cout <Endl ;}// Delete int main (void) {vector <int> V1 (10); For (INT I = 0; I <10; I ++) {v1 [I] = I + 1 ;} // interval Delete v1.erase (v1.begin (), v1.begin () + 3); // Delete the start three elements // Delete v1.erase (v1.begin () at the specified (element) position ()); // delete a header // V1 [1] = 2; V1 [3] = 2; vector <int>: iterator it; for (IT = V1.begin (); it! = V1.end (); It ++) {If (* It = 2) {v1.erase (it); // deletes elements, parameter: iterator} v1.insert (v1.begin (), 100); v1.insert (v1.end (), 200); printv (V1); Return 0 ;}
2. Basic deque operations
Basic operations on double-ended arrays;
The Code is as follows:
# Include <iostream> # include <deque> # include <algorithm> using namespace STD; void printd (deque <int> & D) {deque <int>: iterator it; for (IT = D. begin (); it! = D. end (); It ++) {cout <* It <"" ;}cout <Endl ;}int main (void) {deque <int> d1; d1.push _ back (1); d1.push _ back (3); d1.push _ back (5); d1.push _ Front (-11); d1.push _ Front (-33 ); d1.push _ Front (-55); cout <"Header element:" <d1.front () <Endl; cout <"tail element:" <d1.back () <Endl; printd (D1); d1.pop _ Front (); d1.pop _ back (); printd (D1); // search-33 subscript deque in the array <int>:: iterator it; it = find (d1.begin (), d1.end (),-33); If (it! = D1.end () {cout <"-33 array Subscript:" <distance (d1.begin (), It) <Endl; // search subscript ;} else {cout <"unfound value:-33" <Endl;} return 0 ;}
3. Basic stack operations
The Code is as follows:
# Include <iostream> # include <stack> using namespace STD; class teacher {public: void prints () {cout <"Age:" <age <Endl ;} public: int age; char name [15] ;}; int main (void) {teacher T1, T2, T3; t1.age = 31; t2.age = 32; t3.age = 33; stack <teacher *> S; S. push (& T1); S. push (& T2); S. push (& T3); While (! S. empty () {teacher * TMP = S. top (); TMP-> prints (); S. pop ();} return 0;}/* int main (void) {teacher T1, T2, T3; t1.age = 31; t2.age = 32; t3.age = 33; stack <teacher> S; S. push (T1); S. push (T2); S. push (T3); While (! S. empty () {teacher TMP = S. top (); TMP. prints (); S. pop ();} return 0;} * // * int main (void) {stack <int> S; // inbound stack for (INT I = 0; I <10; I ++) {S. push (I + 1);} cout <S. size () <Endl; // stack size // the size of the outbound stack while (! S. empty () {int TMP = S. top (); // obtain the cout <TMP <""; S. pop () ;}cout <Endl; return 0 ;}*/
4. Basic queue operations
The Code is as follows:
# Include <iostream> # include <queue> using namespace STD; class teacher {public: int age; char name [25]; public: void printq () {cout <"Age:" <age <Endl ;}; int main (void) {teacher Q1, q2, Q3; q1.age = 31; q2.age = 32; q3.age = 33; queue <teacher *> q; q. push (& Q1); q. push (& q2); q. push (& Q3); While (! Q. empty () {teacher * TMP; TMP = Q. front (); // get the queue Header element TMP-> printq (); q. pop ();} return 0;}/* // basic data type in the queue, int main (void) {queue <int> q; q. push (1); q. push (2); q. push (3); cout <"Origin element:" <q. front () <Endl; cout <"queue size" <q. size () <Endl; while (! Q. Empty () {cout <q. Front () <""; q. Pop () ;}cout <Endl; return 0 ;}*/
5. Basic priority_queue operations
The Code is as follows:
# Include <iostream> # include <queue> using namespace STD; int main (void) {priority_queue <int> P1; // by default, it is the maximum priority queue; priority_queue <int, vector <int>, less <int> P2; priority_queue <int, vector <int>, greater <int> P3; // is the smallest priority queue p1.push (33); p1.push (11); p1.push (22); p1.push (77); p1.push (55); p1.push (99 ); cout <"Header element:" <p1.top () <Endl; cout <"queue size:" <p1.size () <Endl; while (! P1.empty () {cout <p1.top () <""; // top (); p1.pop () ;}cout <Endl; cout <"-------------------- test the minimum priority queue -------------------" <Endl; p3.push (33); p3.push (11); p3.push (22); p3.push (77); p3.push (55 ); p3.push (99); cout <"Header element:" <p3.top () <Endl; cout <"queue size:" <p3.size () <Endl; while (! P3.empty () {cout <p3.top () <""; // top (); p3.pop () ;}cout <Endl; return 0 ;}
6. Basic list operations
(1) List Traversal
The Code is as follows:
# Include <iostream> # include <list> using namespace STD; void printl (list <int> & L) {list <int>: iterator it; for (IT = L. begin (); it! = L. end (); It ++) {cout <* It <";}cout <Endl ;}// begin the iterator () and end () meaning: Begin indicates the position of the first element, end indicates the position of the next element> element of the last element, and INT main (void) {list <int> L; cout <"list size:" <L. size () <Endl; For (INT I = 0; I <10; I ++) {L. push_back (I);} printl (l); // list cannot be accessed immediately; List <int >:: iterator it = L. begin (); It ++; it ++; L. insert (it, 100); // insert in STL is preinserted by default; printl (L ); // 1. The index of the list linked list node starts from the 0 position. // 2. The insert method is default. Return 0 ;}
(2) Delete a list
The Code is as follows:
# Include <iostream> # include <list> using namespace STD; void printl (list <int> & L) {list <int>: iterator it; for (IT = L. begin (); it! = L. end (); It ++) {cout <* It <";}cout <Endl ;}// list delete int main (void) {list <int> L; For (INT I = 0; I <10; I ++) {L. push_back (I) ;}list <int >:: iterator it1 = L. begin (); List <int >:: iterator it2 = L. begin (); it2 ++; it2 ++; it2 ++; L. erase (it1, it2); // deletion is left-closed and right-open; [0, 3) printl (l); L. erase (L. begin (); printl (l); L. insert (L. begin (), 100); L. insert (L. begin (), 100); L. insert (L. begin (), 100); L. insert (L. begin (), 100); L. insert (L. begin (), 100); printl (l); L. remove (100); // Method for deleting an element. All elements with a value of 100 are deleted; printl (l); Return 0 ;}
This article is from the "wait0804" blog, please be sure to keep this source http://wait0804.blog.51cto.com/11586096/1875528
Vector, deque, list related operations