fork download
  1. //
  2. // main.cpp
  3. // Exercise 12.19
  4. //
  5. // Created by Steven Wiseman on 19/03/2025.
  6. //
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. #include <memory>
  11.  
  12. class StrBlob {
  13.  
  14. friend class StrBlobPtr;
  15.  
  16. public:
  17.  
  18. std::vector<std::string> word;
  19.  
  20. // constructor
  21.  
  22. StrBlob (std::vector <std::string> input) {
  23.  
  24. for (auto i : input)
  25. word.push_back(i);
  26. }
  27.  
  28. void write () {
  29.  
  30. for (auto i : word)
  31. std::cout << i << std::endl;
  32. }
  33.  
  34. // size operations
  35.  
  36. size_t size () {
  37.  
  38. return word.size();
  39.  
  40. }
  41.  
  42. // add and remove elements
  43.  
  44. void push_back (std::string more) {
  45.  
  46. word.push_back(more);
  47.  
  48. }
  49.  
  50. void pop_back () {}
  51.  
  52. // element access
  53.  
  54. std::string front ();
  55. std::string back ();
  56.  
  57. private:
  58. // interface to StrBlobPtr
  59.  
  60. };
  61.  
  62. class StrBlobPtr {
  63.  
  64. public:
  65.  
  66. StrBlobPtr () {}
  67.  
  68. void push_back (StrBlob* b) {
  69.  
  70. blobContainer.push_back(b);
  71.  
  72. }
  73.  
  74. //std::weak_ptr<std::vector<std::string>> wptr;
  75.  
  76. std::vector <StrBlob*> blobContainer;
  77.  
  78. };
  79.  
  80.  
  81. int main(int argc, const char * argv[]) {
  82.  
  83. std::vector <std::string> Wiseman_1;
  84. Wiseman_1.push_back("Sabbath");
  85. Wiseman_1.push_back("Bloody");
  86. Wiseman_1.push_back("Sabbath");
  87. Wiseman_1.push_back("You");
  88. Wiseman_1.push_back("Ozzy");
  89.  
  90. StrBlob* Kay = new StrBlob (Wiseman_1);
  91.  
  92. //auto Kay = std::make_shared<StrBlob>(Wiseman_1);
  93.  
  94. //Kay->write();
  95.  
  96. std::cout << Kay->size() << std::endl;
  97.  
  98. Kay->push_back("love");
  99.  
  100. //Kay->write();
  101.  
  102. // StrBlobPtr
  103.  
  104. //StrBlob* b = new StrBlob (Wiseman);
  105.  
  106. //StrBlobPtr* wisey = new StrBlobPtr (b);
  107.  
  108. std::vector <std::string> Wiseman_2;
  109. Wiseman_2.push_back("Hallowed");
  110. Wiseman_2.push_back("Be");
  111. Wiseman_2.push_back("Thy");
  112. Wiseman_2.push_back("Name");
  113.  
  114. StrBlob* b1 = new StrBlob (Wiseman_2);
  115.  
  116. //auto b1 = std::make_shared<StrBlob>(Wiseman_2);
  117.  
  118. //b1->write();
  119.  
  120. std::vector <std::string> Wiseman_3;
  121. Wiseman_3.push_back("The");
  122. Wiseman_3.push_back("Trooper");
  123.  
  124. StrBlob* b2 = new StrBlob (Wiseman_3);
  125.  
  126. //auto b2 = std::make_shared<StrBlob>(Wiseman_3);
  127.  
  128. //b2->write();
  129.  
  130. //auto wisey = std::make_shared<StrBlobPtr>();
  131.  
  132. auto wisey = std::make_shared<StrBlobPtr>();
  133.  
  134. wisey->push_back(Kay);
  135. wisey->push_back(b1);
  136. wisey->push_back(b2);
  137.  
  138. for (auto i : wisey->blobContainer) {
  139.  
  140. i->write();
  141.  
  142. }
  143.  
  144.  
  145.  
  146. return 0;
  147. }
  148.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
5
Sabbath
Bloody
Sabbath
You
Ozzy
love
Hallowed
Be
Thy
Name
The
Trooper