الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كل ما يتعلق بال Stl

بدأه بن العيد في 15 فبراير 2009 · 5 رد · 1,430 مشاهدة · في المواضيع والدروس
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه

لا أعلم إن سبق التطرق لموضوع STL في هذا القسم أم لا و لكن ... في الإعادة إفادة كما يقولون.

في هذا الموضوع أقدم لكم مثال يحتوي أمثلة تشمل مختلف دوال STL و كيفية إستدعائها و إستخدامها.

لم يسمح لي الوقت بكتابة موضوع شامل و مزيد من التفاصيل و إن شاء الله أعود لهذا الموضوع بمزيد من الشرح.

Vector


  1. #include <vector>
  2. void STLVectorDemo()
  3. {
  4. std::vector<int> V1; // Just define a vector.
  5. std::vector<int> V2(5,60); // Define a vector of 5 elements and initalize all its value to 60.
  6. std::vector<int> V3(5); // Just define a vector of 5 elements.
  7.  
  8. // Insert 4, 2 and 5 integer in the last emty indexes respectively.
  9. V1.push_back(4);
  10. V1.push_back(2);
  11. V1.push_back(5);
  12. // Browse the vector V1 by iterators or indexes
  13. std::cout << "V1= ";
  14. for(std::size_t i=0;i<V1.size();++i)
  15. {std::cout << V1[i] << ' ';}
  16. std::cout << std::endl;
  17. // Populate V3
  18. V3[0] = 2;
  19. V3[1] = 4;
  20. V3[2] = 5;
  21. V3[3] = 1;
  22. V3[4] = 8;
  23. // Last value in V3 is 8 we will try to push back a new value.
  24. V3.push_back(10);
  25. // Show the value in the last index of V3.
  26. std::cout<<"V3[4]="<<V3[4]<<std::endl;
  27. // The instruction 'V3.push_back(10)' hasn't effect because the last value of V3 isn't empty.
  28. }
  29.  

List

  1.  
  2. #include <list>
  3. void STLListDemo()
  4. { // Define the list
  5. std::list<int> myliste;
  6. // Populate the list
  7. std::cout<<"std::list<int> myliste isn't initialized. Default size= "<<myliste.size()<<std::endl;
  8. std::cout << "myliste=";
  9. for(std::size_t i=0;i<10;++i)
  10. {myliste.push_back(rand());}
  11. // Define the iterator
  12. std::list<int>::const_iterator
  13. // Start reading
  14. Read(myliste.begin()),
  15. lend(myliste.end());
  16. // Show the list content: can be performed by iterator or by inedx.
  17. for(;Read!=lend;++Read)
  18. {std::cout << *Read << ' ';}
  19. // New line
  20. std::cout << std::endl;
  21. }
  22.  

Set

  1.  
  2. #include <set>
  3. void STLSetDemo()
  4. {
  5. // Define a new set.
  6. std::set<int> mySet; // equivalent to std::set<int,std::less<int> >
  7. // Populate the defined set
  8. mySet.insert(10);
  9. mySet.insert(5);
  10. mySet.insert(7);
  11. mySet.insert(10); // Again we will insert the value 10 to the set.
  12.  
  13. // Define an iterator and copy the set content in it.
  14. std::set<int>::const_iterator
  15. sit (mySet.begin()),
  16. send(mySet.end());
  17. // Show the content of the set.
  18. std::cout<<"The Set content is: ";
  19. for(;sit!=send;++sit)
  20. {std::cout << *sit << ' ';}
  21. // End of the output text.
  22. std::cout << std::endl;
  23. // Awesome!!! STL SET don't allow duplicated values and in addition to this, its content will be sorted.
  24. }
  25.  

Map

  1. #include <map>
  2. #include <string>
  3. void STLMapDemo()
  4. {
  5. // Define the map
  6. std::map<std::string,unsigned> myMapIndexes;
  7. // Populate the map
  8. myMapIndexes["Hazzoum"] = 1;
  9. myMapIndexes["time1"] = 2;
  10. myMapIndexes["BMAbedelAziz"] = 3;
  11. myMapIndexes["HGB"] = 4;
  12. // Define an iterator
  13. std::map<std::string,unsigned>::const_iterator
  14. // Copy the map content to iterator
  15. mit (myMapIndexes.begin()),
  16. mend(myMapIndexes.end());
  17. // Show the map content
  18. for(;mit!=mend;++mit)
  19. {std::cout<<mit->first<< 't' <<mit->second<< std::endl;}
  20. }
  21.  

Pair

  1. #include <vector> // pair is defined in the header file pair.h, witch is defined too in vector
    .h
  2. #include <string>
  3. void STLPairDemo()
  4. {
  5. std::pair<int,std::string> myPair = std::make_pair(5,"Demo String");
  6. std::cout << myPair.first << ' ' << myPair.second << std::endl;
  7. }
  8.  

أترككم مع المثال.

#2

حياك الله أخوي بن عيد

هذا موضوع قديم كنت كتبته عن الSTL يمكن تلقى فيه اللي يفيدك أو تكمل عليه ....

الطريق إلى إحتراف التعامل مع STL

#3

السلام عليكم و رحمة الله و بركاته

مرحبا b.m.s

كيف حالك، و ما كل هذا الغياب!!!

شكرا على الرابط سأعود للموضوع إن شاء الله و أمزج بين الموضوعين

#4

السلام عليكم ,,

هذا مثال قمت بكتابته :)

std::string namesList;
std::vector<std::string> sortedNames;
std::istringstream namesStream;

std::getline( std::cin, namesList );
namesStream.str( namesList );

std::istream_iterator<std::string> begin( namesStream ), end;

while( begin != end )
	sortedNames.push_back( *begin++ );
std::sort( sortedNames.begin(), sortedNames.end() );
std::cout << std::endl;
for( std::vector<std::string>::iterator i = sortedNames.begin(); i != sortedNames.end(); ++i )
	std::cout << *i << std::endl;
std::cin.get();

على سبيل المثال لو قمنا بإدخال التالي من خلال الـ Console :

khaled mohammed saleem omar jameel layla jomana

فسوف يقوم بترتيب الأسماء, ما أريد الإشارة إليه هنا هو سهولة كتابة هذا الأمر باستخدام STL, حاول مثلاً كتابة الكود من الصفر :)

تحياتي ,,

تم تعديل هذه المشاركة بواسطة Khaled.Alshaya في 21 فبراير 2009 في 01:06

#5

السلام عليكــم ورحمـة الله وبركاتــة ،،

اقتباس
khaled mohammed saleem omar jameel layla jomana

كل هذا إسمك يا خالد !!!! :blink:

:lol:

#6

:huh:

:lol:

مواضيع مشابهة