Many online introductions are about STL in the console. I think this is always suitable for learning theory knowledge and its practice. When you really use it, you are not suitable. I also have a look. Divided into two cases. 1) Application of console 2) Write a simple drawing program with MFC (I am afraid this should be more paying more attention)
OK, first talk about the console:
The following source code can be run directly (vc6 console) (About the entire project file can go to www.confach.533.net to Download), if you can't find it, you can press the contact information directly with me Mil, I will be as soon as possible Send you past, everyone study together)
#ifdef _MSC_VER # ENDIF
#include "stdafx.h" #pragma Warning (Disable: 4786) #pragma Warning (Disable: 4514) #include #include
Using namespace std;
Class Printint {public: void Operator () (int elem) const {std :: cout <
<
}
}
// define a class
Class StudentID
{
PUBLIC:
StudentId (int ID = 0)
{
Value = ID;
Std :: cout << "Assigning Student ID:" <
<
}
~ StudentId ()
{
Std :: Cout << "Destructing ID:" <
<
}
Private:
Int value;
}
Class Student
{
PUBLIC:
Student (char * pname = "noname", int ssid = 0): ID (SSID)
{
Std :: cout << "Constructing Student:" <
<
STRNCPY (Name, PNAME, SIZEOF (NAME);
Name [SizeOf (Name) -1] = 0;
StudentID ID (SSID);
}
Private:
Char Name [20];
StudentID ID;
}
Void testmmap ()
{
TypedEf MultiMap
INTSTRINGMAP;
INTSTRINGMAP COLL;
// Coll.Insert (std :: make_pair (5, "tagged"));
// Coll.Insert (Make_Pair (2, "A"));
// Coll.insert (make_pair (1, "this"));
// Coll.Insert (Make_Pair (4, "of"));
// Coll.Insert (make_pair (6, "strings"));
// Coll.Insert (Make_Pair (1, "IS"));
// Coll.Insert (make_pair (3, "multimap"); //
/ * Print All Element Values
* - Iterage over all Elements
* - Element Member Second Is The Value
* /
Std :: cout << "========================================" <
// IntStringMap :: item POS;
// for (POS = Coll.begin (); POS! = COLL.End (); POS)
// {
// std :: cout << POS-> Second << "
//}
Std :: cout << Endl;
}
void testset ()
{
Typedef std :: set
> Int set;
INTSET SET1;
Set1.insert (1);
Set1.insert (3);
Set1.insert (5);
Set1.insert (1);
Set1.insert (4);
Set1.insert (6);
Std :: cout << "========================================" <
INTSET :: Const_Iterator POS;
For (POS = set1.begin (); POS! = set1.end (); POS)
{
Std :: cout << * POS << "<<" set "<
}
}
Void testalgorithm ()
{
Vector
Mvector;
Vector
:: Iterator Pos;
MVector.push_back (1); MVector.push_Back (2); MVector.push_Back (5); MVector.Push_Back (76); MVector.push_Back (4);
Std :: cout << "========================================
For (POS = mvector.begin (); pOS! = mvector.end (); POS)
{
Std :: cout << * POS <
}
POS = min_Element (MVector.begin (), MVector.end ()); std :: cout << "min:" << * POS Std :: cout << "max:" << * POS < std :: cout << "====================================== Std :: cout << "sort" < std :: cout << "====================================== Std :: sort (mvector.begin (), mvector.end ()); For (POS = mvector.begin (); pOS! = mvector.end (); POS) { Std :: cout << * POS < } std :: cout << "====================================== Std :: cout << "reverse" < std :: cout << "====================================== Std :: REVERSE (MVector.begin (), MVector.end ()); For (POS = mvector.begin (); pOS! = mvector.end (); POS) { Std :: cout << * POS < } } Void testiteratorradapter () { List COLL1; // INSERT Elements from 1 to 9 INTO THE FIRST COLLECTION For (INT i = 1; i <= 9; i) { COLL1.PUSH_BACK (i); } // Copy the elements of coll1 into coll2 by appending Vector COLL2; Copy (coll1.begin (), coll1.end (), // sourceback_inserter (COLL2)); // Destination // copy the elements of coll1 int3 by Inserting Them Front // - REVERSES The Order of the Elements // Remarks: if use front_inserter, The Container Must Provide Push_Front Method DEQUE COLL3; Copy (coll1.begin (), coll1.end (), // Source Front_inserter (COLL3)); // Destination // COPY Elements of coll1 INTO COLL4 // - Only Inserter That Works for Associative Collectes set COLL4; Copy (coll1.begin (), coll1.end (), // Source INSERTER (COLL4, COLL4.BEGIN ())); // destination Std :: cout << "===================== List ==========" < List :: Iterator Pos; For (POS = COLL1.BEGIN (); POS! = coll1.end (); POS) { Std :: cout << * POS < } Std :: cout << "========================================" < Vector :: item POS1; For (POS1 = COLL2.BEGIN (); POS1! = COLL2.END (); POS1) { Std :: cout << * POS1 < } Std :: cout << "======================================" < DEQUE :: Iterator POS2; For (POS2 = COLL3.BEGIN (); POS2! = Coll3.end (); POS2) { Std :: cout << * POS2 < } Std :: cout << "===================== set (List use inserter) =========== :: Iterator POS3; For (POS3 = COLL4.BEGIN (); POS3! = COLL4.END (); POS3) { Std :: cout << * POS3 < } } void testStreamiterator () {vector COLL; / * Read All Words from The Standard Input * - Source: All strings Until End-of-file (or error) * - DESTINATION: COLL (ISERTING) * / COPY (ISTREAM_ITERATOR (cin), // start of source iStream_iterator (), // end of source Back_inserter (COLL)); // Destination // Sort Elements Sort (Coll.begin (), COLL.End ()); / * Print All Elements without duplicates * - Source: Coll * - Destination: Standard Output (with newline badeen elements) * / Unique_copy (coll.begin (), coll.end (), // source ostream_iterator (cout, "/ n")); // Destination } Template Inline Void Print_Elements (Const T & Coll, Const Char * Optcstr = ") { TypeName T :: const_iterator pos; Std :: Cout << Optcstr < For (POS = Coll.begin (); POS! = COLL.END (); POS) { Std :: cout << * POS << '' } Std :: cout << std :: endl; } // void main () // {// const INT size = 100; // int Array [size]; // array [0] = 0; // array [1] = 1; // array [1000] = 1000; //// cout < << "," < < // //} Typedef struct tagintegerNode { INT IVALUE; TagintegerNode * pnextintegernode; INTEGERNODE; Class a {public: // Made public to simplify traction // would Normal BE PROTECTED: Static IntegerNode * pfirstintegerNode; // Static Data Members Must Be Initialized At File Scope, Even // IF Private.INTEGERNODE * A :: PfirstintegerNode; Class B: Public a {public: b (void); Class C: public a {public: c (void); B :: b (void) {pfirstintegerNode = new integerNode; pfirstintegernode-> iValue = 1; pfirstintegernode-> pnextintegernode = null;}; C :: C (void) {pFirstIntegerNode-> pNextIntegerNode = new INTEGERNODE; pFirstIntegerNode-> pNextIntegerNode-> iValue = 2; pFirstIntegerNode-> pNextIntegerNode-> pNextIntegerNode = NULL;}; / / -------------------------------------------------------------------------------------------- ---- // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------- Typedef List Listelement; Typedef List OwningList; Void OutputList (Const Listelement & EntireListelement, Int ListNumber) { Ostream_IstRerator OUT (cout, "); COUT << "list" < << ":"; << ":"; Copy (entireListelement.begin (), entireListelement.end (), out); Cout < } void testowningList () {oowninglist listoflistelements; for (int i = 0; i <3; i) {listElement entireListelement; for (int J = 0; j <5; J) {entireListelement.push_back (i * 4 J);} OutputList (EntireListelement, i 1); listoflistelements.push_back (entireListelement);} Cout < OwningList :: item it = listoflistelements.begin (); For (int J = 1; it! = Listoflistelements.end (); IT, J) { Const Listelement & entireListelement1 = * IT; OutputList (entireListelement1, J); } } TypedEf Vector IntVector; Void TestVectorAttr1 () {// Dynamically Allocated Vector Begins with 0 Elements. intVector thevector; // add one element to the end of the vector, an int with the value 42. Thevector.push_back (42); // show statistics about vector. Cout << "Thevector's size is:" << thevector.size () < } void testorcreate () {cout << "========================================== ===== "< Cout << "=======================================" INTVECTOR C1; C1.push_back (1); C1.PUSH_BACK (2); C1.PUSH_BACK (3); C1.PUSH_BACK (4); INTVECTOR :: Iterator IT; Cout << "--- crete a empty vector without any element" < For (it = c1.begin (); it! = c1.end (); IT) { COUT << * IT << ","; } Cout < COUT << "- CREATE a COPY vector" < INTVECTOR C2 (C1); For (it = c2.begin (); it! = c2.end (); IT) { COUT << * IT << ","; } Cout < Cout << "--- crete a vector with n elements < INTVECTOR C3 (8); C3.Assign (8, 1); C3.PUSH_BACK (10); C3.PUSH_BACK (12); C3.PUSH_BACK (14); C3.PUSH_BACK (16); C3.push_back (18); For (it = c3.begin (); it! = c3.end (); it) {cout << * IT < } Cout << "--- Create a Vector with n copies of elements < INTVECTOR C4 (5, 10); For (it = c4.begin (); it! = c4.end (); IT) { COUT << * IT < } INT NARR [] = {1, 2, 3, 4, 5}; intVector C5 (Narr, Narr Sizeof (Narr) / sizeof (int)); cout << "--- create a vector with a array" < For (it = c5.begin (); it! = c5.end (); IT) { COUT << * IT < } COUT << "- Test end ----------" < < } Void TestVectorIpector () {Vector Ages (15); Ages.push_back (15); Vector :: const_iterator p = Ages.begin (); for (int i = 0; i <10; i ) { Ages.push_back (i); } Cout << ================================== //p@ages.begin (); Cout << "The first age is:" << * P < COUT << "=============== ===================== < } Class Shape { PUBLIC: Virtual void Draw () { COUT << "Draw It in Shape Class" < } Virtual void getsize () = 0; } Class Rectangle: Public Shape: PUBLIC Shape { PUBLIC: Virtual void Draw () { COUT << "Draw It in Rectangle Class" < } Void getsize () { COUT << "Get Size in Rectangle Class" < } } Void testpointer () { INT i = 100; CHAR C = 'a'; FLOAT F = I; Double d = f; BOOL B = true; INT * PI = & I; INT ** PPI = & PI; COUT << "Address of i: 0x" << & i < Printf ("Address Of C: 0x% P / N", & C); COUT << "Address of f: 0x" << & f < COUT << "Address of D: 0x" << & D < COUT << "Address of B: 0x" << & B < COUT << "Address of Pi: 0x" << & Pi < COUT << "Value of Pi: 0x" < < COUT << "Address of PPI: 0x" << & PPI < COUT << "Value of PPI: 0x" < < } void main () {TestPointer (); return; Shape * pShape = NULL; Rectangle a; pShape = & a; pShape-> Draw (); pShape-> GetSize (); return; TestVectorCreate (); TestVectorAttr1 (); TestVectorIterator ( Return; TestownList (); B Instanceb; C Instancec; cout Cout < PNEXTINTEGERNODE-> iValue < Short IT IARRAY [1000]; For (int I1 = 0; I1 <1000; i1 ) { IARRAY [I1] = I1; } Student S ("Confach Zhang", 99471136); Vector COL1; DEQUE DEQ1; List List1; Std :: cout << "=========================================== ======= "< Std :: Cout << "INDEX VALUE TYPE" < Std :: cout << "=========================================== ======= "< For (int i = 0; i <100; i ) {IF (i% 2 == 0) COL1.PUSH_BACK (i); } For (i = 0; i { Std :: cout < << "? Vector? < } Std :: cout << "=============== Object function ===================" < For_each (col1.begin (), col1.end (), printint ()); Std :: cout << "=========================================== ======= " Std :: cout << "=========================================== ======= "< For (i = 0; i <100; i ) { IF (i% 2 == 0) { DEQ1.PUSH_FRONT (i); } Else { DEQ1.PUSH_BACK (i); } } For (i = 0; i { Std :: cout < << "?? Deve? < } Std :: cout << "=========================================== ======= "< Std :: Cout << "INDEX VALUE TYPE" < Std :: cout << "=========================================== ======= "< For (char c = 'a'; c <= 'z'; C ) { List1.push_back (c); } While (! list1.empty ()) { Std :: cout < List1.pop_front (); } Std :: cout << "=========================================== ======= " Std :: cout << "=========================================== ======= "< For (c = 'a'; c <= 'z'; c ) { List1.push_front (c); } std :: cout << "============== User define function ========================== " Print_Elements (List1, "Confach"); List :: Iterator Pos; // List :: Const_Iterator // for Read_only // List :: Iterator // for Read / Write For (POS = List1.begin (); POS! = List1.end (); POS) // CAUTION: // Note That The Preincrement Operator (Prefix ) is buy here. This is because it might has better // Performance Than The PostIncrement Operator. The Latter Involves a Temporary Object Because IT // must return the old position of the iterator. For this really is best to prefer POS. IS BEST TO POS // Over POS . thus, you shouth the FOLLOWING VERSION: // for (POS = COLL.BEGIN (); POS! = coll.end (); POS ) { // ^^^^^ // OK, But Slower // ... //} // for this Reason, I Recommend Using The Preincrement and pre-Decrement Operators in General. { Std :: cout < << "? Ipalator? < } TestSet (); testmmap (); testalgorithm (); std :: cout < << "=============== Test iterator adapter = '========" < }