fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. struct T {
  6. T() = default;
  7. T(int i, double d, const std::string& s) : i_(i), d_(d), s_(s) {}
  8. int i_;
  9. double d_;
  10. std::string s_;
  11. friend std::ostream& operator << (std::ostream& to, const T& t) {
  12. return to << '[' << t.i_ << ',' << t.d_ << ',' << t.s_ << ']';
  13. }
  14. };
  15.  
  16. using TVec = std::vector<T>;
  17.  
  18. void status(const char* label, const TVec& tv)
  19. {
  20. std::cout << label << ":\n";
  21. std::cout << "tv.empty() == " << (tv.empty() ? "true" : "false") << "\n";
  22. if (tv.empty() == false) {
  23. std::cout << "+ tv.front() == " << tv.front() << "\n";
  24. std::cout << "+ tv.back() == " << tv.back() << "\n";
  25. }
  26. std::cout << "tv.capacity() == " << tv.capacity() << "\n";
  27. std::cout << "tv.size() == " << tv.size() << "\n";
  28. std::cout << "\n";
  29. }
  30.  
  31.  
  32. int main() {
  33. TVec tv;
  34.  
  35. status("start", tv);
  36.  
  37. tv.reserve(10); // pre-allocate room for 10.
  38. status("after reserve", tv);
  39.  
  40. tv.emplace_back(0, 42., "first");
  41. status("after first", tv);
  42.  
  43. {
  44. T temp { 1, 42., "second" };
  45. tv.push_back(temp); // copy temp to new entry in tv.
  46. }
  47. status("after second", tv);
  48.  
  49. tv.emplace_back(2, 50., "third");
  50. status("after third", tv);
  51.  
  52. std::cout << "tv[1].s_ = " << tv[1].s_ << "\n";
  53.  
  54. // C++'s standard library heavily uses something called "iterators",
  55. // these are variables that reference entries in a container like a
  56. // vector. An iterator lets you ask a question like "find me an element
  57. // with this value" or "tell me the start and end of a range".
  58. // The most natural range is the start and end of the container.
  59. // These are `begin` and `end`. `begin` references the first element
  60. // while `end` references *beyond* the last element. This is an
  61. // important distinction because it lets you determine if something is
  62. // *not* in a container.
  63.  
  64. std::cout << "Contents of tv using iterators:\n";
  65. for (auto it = tv.begin(); it != tv.end(); ++it) {
  66. // "it" is the iterator itself, like a pointer,
  67. // "*it" is the entry the iterator references.
  68. std::cout << " " << *it << "\n";
  69. }
  70.  
  71. // there's also insertion: here we'll insert infront of the first entry
  72. tv.insert(tv.begin(), T { -1, -1., "sneaky" });
  73. status("after sneaky", tv);
  74.  
  75. std::cout << "Contents of tv using range-based for:\n";
  76. for (auto&& t : tv) {
  77. // 'auto t' would *copy* every T in tv into `t`, which is wasteful,
  78. // 'auto&& t' will use a reference if it can, which saves cpu
  79. std::cout << " " << t << "\n"; // note no need to do '*'.
  80. }
  81.  
  82. // now lets remove the element *after* sneaky
  83. tv.erase(tv.begin() + 1);
  84. status("after erase", tv);
  85.  
  86. // finally, one last magical function
  87. tv.clear();
  88. status("after clear", tv);
  89. }
  90.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
start:
tv.empty() == true
tv.capacity() == 0
tv.size() == 0

after reserve:
tv.empty() == true
tv.capacity() == 10
tv.size() == 0

after first:
tv.empty() == false
+  tv.front() == [0,42,first]
+  tv.back() == [0,42,first]
tv.capacity() == 10
tv.size() == 1

after second:
tv.empty() == false
+  tv.front() == [0,42,first]
+  tv.back() == [1,42,second]
tv.capacity() == 10
tv.size() == 2

after third:
tv.empty() == false
+  tv.front() == [0,42,first]
+  tv.back() == [2,50,third]
tv.capacity() == 10
tv.size() == 3

tv[1].s_ = second
Contents of tv using iterators:
  [0,42,first]
  [1,42,second]
  [2,50,third]
after sneaky:
tv.empty() == false
+  tv.front() == [-1,-1,sneaky]
+  tv.back() == [2,50,third]
tv.capacity() == 10
tv.size() == 4

Contents of tv using range-based for:
  [-1,-1,sneaky]
  [0,42,first]
  [1,42,second]
  [2,50,third]
after erase:
tv.empty() == false
+  tv.front() == [-1,-1,sneaky]
+  tv.back() == [2,50,third]
tv.capacity() == 10
tv.size() == 3

after clear:
tv.empty() == true
tv.capacity() == 10
tv.size() == 0