Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #include "pch.h" //Include standard C++ includes & set namespace
  2.  
  3.  
  4.  
  5. #include "Item.h" //This will allow us to use item
  6. #include "ContainerTest.h"
  7.  
  8. void TestMap() {
  9. map<string, int> tNumbers = { {"richard",5000}
  10. ,{"fred",500}
  11. ,{"fred",6500}
  12. ,{"Abby",9500}
  13. ,{"Samantha",7550}
  14. ,{"Gail",7590}
  15. ,{"Sue",4400}
  16. ,{"Dave",6700}
  17. ,{"Tammie",17500}
  18. ,{"John",227500}
  19. };
  20. for (auto tI = tNumbers.begin(); tI != tNumbers.end(); ++tI) {
  21. cout << "Key:" << tI->first << " Value:" << tI->second << " @0x" << hex << &(*tI) << endl;
  22. }
  23. }
  24.  
  25.  
  26.  
  27. void TestList() {
  28. list<int> tNumbers = { 5,4,99,7,4,11,45,33,17,0 }; //Make a list of 10 integers & initalise
  29. for (auto tI = tNumbers.begin(); tI != tNumbers.end(); ++tI) {
  30. cout << *tI << " @0x" << hex << &(*tI) << endl;
  31. }
  32. }
  33.  
  34.  
  35. void TestVector() {
  36.  
  37. vector<int> tNumbers = { 5,4,99,7,4,11,45,33,17,0 }; //Make a a vector of 10 integers & initalise
  38.  
  39. for (vector<int>::iterator tI = tNumbers.begin(); tI != tNumbers.end(); ++tI) {
  40. cout << *tI << " @0x" << hex << &(*tI) << endl;
  41. }
  42.  
  43. for (auto tI = tNumbers.begin(); tI != tNumbers.end(); ++tI) {
  44. cout << *tI << " @0x" <<hex << &(*tI) << endl;
  45. }
  46. }
  47.  
  48. void RandomAccess() {
  49. vector<int> tVectorNumbers = { 5,4,99,7,4,11,45,33,17,0 }; //Make a a vector of 10 integers & initalise
  50. cout << tVectorNumbers[5] << " @0x" << hex << &tVectorNumbers[5] << endl;
  51.  
  52. list<int> tListNumbers = { 5,4,99,7,4,11,45,33,17,0 }; //Make a list of 10 integers & initalise
  53. int tIndex = 0;
  54. for (auto tI = tListNumbers.begin(); tI != tListNumbers.end(); ++tI) {
  55. if (tIndex == 5) { //Are we at 5th index
  56. cout << (dec) << *tI << " @0x" << hex << &(*tI) << endl;
  57. break;
  58. }
  59. tIndex++;
  60. }
  61.  
  62. }
  63.  
  64. void PrintItems(vector<int> vData) //Print the items
  65. {
  66. for (size_t tI = 0; tI < vData.size(); tI++)
  67. {
  68. cout << vData[tI] << endl; //Uses [] index operator to get access to item at a specific index
  69. }
  70. }
  71.  
  72.  
  73. vector<int> BubbleSort(vector<int> vData) //Template, some code missing
  74. {
  75. bool tSwap;
  76. do
  77. {
  78. tSwap = false;
  79. for (size_t tI = 0; tI < vData.size() - 1; tI++) //Same as above
  80. {
  81. if (vData[tI] > vData[tI + 1])
  82. {
  83. int tTempData = vData[tI + 1]; //We swap by using a tamp variable
  84. vData[tI + 1] = vData[tI]; //As here we are overwriting it
  85. vData[tI] = tTempData;
  86. tSwap = true; //We have swpped so not yet done
  87. }
  88. }
  89. } while (tSwap); //If we have not made any swaps then we are done
  90. return vData; //Return sorted items
  91. }
  92.  
  93. void TestBubble() {
  94. vector<int> tVectorNumbers = { 5,4,99,7,4,11,45,33,17,0 }; //Make a a vector of 10 integers & initalise
  95. cout << "Before" << endl;
  96. PrintItems(tVectorNumbers);
  97. cout << "Sorted" << endl;
  98. tVectorNumbers = BubbleSort(tVectorNumbers);
  99. PrintItems(tVectorNumbers);
  100. }
  101.  
  102.  
  103.  
  104.  
  105. void PrintItems(std::list<std::shared_ptr<Item>> &tItems)
  106. {
  107. for (auto tI = tItems.begin(); tI != tItems.end(); ++tI) {
  108. cout << (*tI)->ToString() << endl;
  109. }
  110. }
  111.  
  112.  
  113. void SortByWeight(list<shared_ptr<Item>>& tItems)
  114. {
  115. tItems.sort([](shared_ptr<Item> vFirst, shared_ptr<Item> vSecond) {
  116. bool tSwap = ((*vFirst).GetWeight() < (*vSecond).GetWeight()); //Sort ascending
  117. return tSwap;
  118. }
  119. );
  120. }
  121.  
  122. int main() {
  123.  
  124. list<shared_ptr<Item>> tItems; //make a list of shared pointers to our Item from last week
  125.  
  126. tItems.emplace_back(make_shared<Item>("Axe", 50.0f, 0.2f, 0.0f));
  127. tItems.emplace_back(make_shared<Item>("Sword", 20.0f, 0.1f, 0.0f));
  128. tItems.emplace_back(make_shared<Item>("Hammer", 75.0f, 0.3f, 0.0f));
  129. tItems.emplace_back(make_shared<Item>("Magic Popsicle", 1.0f, 0.02f, 0.0f));
  130.  
  131. cout << "Before" << endl;
  132. PrintItems(tItems);
  133.  
  134. if (tItems.size() > 0) { //Make sure there is an item in the list
  135. shared_ptr<Item> tFirstItem = tItems.front(); //Get first item in list
  136. cout << "Removing:" << (*tFirstItem).ToString() << endl;
  137. tItems.remove(tFirstItem); //Remove item from list
  138. }
  139.  
  140. SortByWeight(tItems);
  141.  
  142. cout << "After" << endl;
  143. PrintItems(tItems);
  144.  
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement