السلام عليكــم ورحمـة الله وبركاتــه
لا أعلم إن سبق التطرق لموضوع STL في هذا القسم أم لا و لكن ... في الإعادة إفادة كما يقولون.
في هذا الموضوع أقدم لكم مثال يحتوي أمثلة تشمل مختلف دوال STL و كيفية إستدعائها و إستخدامها.
لم يسمح لي الوقت بكتابة موضوع شامل و مزيد من التفاصيل و إن شاء الله أعود لهذا الموضوع بمزيد من الشرح.
Vector
- #include <vector>
- void STLVectorDemo()
- {
- std::vector<int> V1; // Just define a vector.
- std::vector<int> V2(5,60); // Define a vector of 5 elements and initalize all its value to 60.
- std::vector<int> V3(5); // Just define a vector of 5 elements.
- // Insert 4, 2 and 5 integer in the last emty indexes respectively.
- V1.push_back(4);
- V1.push_back(2);
- V1.push_back(5);
- // Browse the vector V1 by iterators or indexes
- std::cout << "V1= ";
- for(std::size_t i=0;i<V1.size();++i)
- {std::cout << V1[i] << ' ';}
- std::cout << std::endl;
- // Populate V3
- V3[0] = 2;
- V3[1] = 4;
- V3[2] = 5;
- V3[3] = 1;
- V3[4] = 8;
- // Last value in V3 is 8 we will try to push back a new value.
- V3.push_back(10);
- // Show the value in the last index of V3.
- std::cout<<"V3[4]="<<V3[4]<<std::endl;
- // The instruction 'V3.push_back(10)' hasn't effect because the last value of V3 isn't empty.
- }
List
- #include <list>
- void STLListDemo()
- { // Define the list
- std::list<int> myliste;
- // Populate the list
- std::cout<<"std::list<int> myliste isn't initialized. Default size= "<<myliste.size()<<std::endl;
- std::cout << "myliste=";
- for(std::size_t i=0;i<10;++i)
- // Define the iterator
- std::list<int>::const_iterator
- // Start reading
- Read(myliste.begin()),
- // Show the list content: can be performed by iterator or by inedx.
- for(;Read!=lend;++Read)
- {std::cout << *Read << ' ';}
- // New line
- std::cout << std::endl;
- }
Set
- #include <set>
- void STLSetDemo()
- {
- // Define a new set.
- std::set<int> mySet; // equivalent to std::set<int,std::less<int> >
- // Populate the defined set
- mySet.insert(10);
- mySet.insert(5);
- mySet.insert(7);
- mySet.insert(10); // Again we will insert the value 10 to the set.
- // Define an iterator and copy the set content in it.
- std::set<int>::const_iterator
- sit (mySet.begin()),
- // Show the content of the set.
- std::cout<<"The Set content is: ";
- for(;sit!=send;++sit)
- {std::cout << *sit << ' ';}
- // End of the output text.
- std::cout << std::endl;
- // Awesome!!! STL SET don't allow duplicated values and in addition to this, its content will be sorted.
- }
Map
- #include <map>
- #include <string>
- void STLMapDemo()
- {
- // Define the map
- std::map<std::string,unsigned> myMapIndexes;
- // Populate the map
- myMapIndexes["Hazzoum"] = 1;
- myMapIndexes["time1"] = 2;
- myMapIndexes["BMAbedelAziz"] = 3;
- myMapIndexes["HGB"] = 4;
- // Define an iterator
- std::map<std::string,unsigned>::const_iterator
- // Copy the map content to iterator
- mit (myMapIndexes.begin()),
- // Show the map content
- for(;mit!=mend;++mit)
- {std::cout<<mit->first<< 't' <<mit->second<< std::endl;}
- }
Pair
- #include <vector> // pair is defined in the header file pair.h, witch is defined too in vector
.h - #include <string>
- void STLPairDemo()
- {
- std::pair<int,std::string> myPair = std::make_pair(5,"Demo String");
- std::cout << myPair.first << ' ' << myPair.second << std::endl;
- }
أترككم مع المثال.

