Ginsutime

Cherno Iterator Vector Setup Main CPP

Feb 23rd, 2022
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include "Vector.h"
  4.  
  5. // How to write an iterator for a vector class, don't need this since you can just increment a pointer instead
  6. // This concept translates to any data structure ever though. Just copy this and change what the operators do (such as ++)
  7. // Try implementing an iterator for the array class (check back to code Cherno put in his array class on this video)
  8. int main()
  9. {
  10.     Vector<std::string> values;
  11.     values.EmplaceBack("1");
  12.     values.EmplaceBack("2");
  13.     values.EmplaceBack("3");
  14.     values.EmplaceBack("4");
  15.     values.EmplaceBack("5");
  16.  
  17.     std::cout << "Not using iterators:\n";
  18.     for (int i = 0; i < values.Size(); i++)
  19.     {
  20.         std::cout << values[i] << std::endl;
  21.     }
  22.  
  23.     std::cout << "Range-based for loop:\n";
  24.     for (auto& value : values)
  25.         std::cout << value << std::endl;
  26.  
  27.     std::cout << "Iterator:\n";
  28.     for (Vector<std::string>::Iterator it = values.begin();
  29.         it != values.end(); it++)
  30.     {
  31.         std::cout << *it << std::endl;
  32.     }
  33.  
  34. #if 0
  35.     std::vector<int> values = { 1, 2, 3, 4, 5 };
  36.  
  37.     for (int i = 0; i < values.size(); i++)
  38.     {
  39.         std::cout << values[i] << std::endl;
  40.     }
  41.  
  42.     for (int value : values)
  43.         std::cout << value << std::endl;
  44.  
  45.     for (std::vector<int>::iterator it = values.begin();
  46.         it != values.end(); it++)
  47.     {
  48.         std::cout << *it << std::endl;
  49.     }
  50. #endif
  51.  
  52.     std::cin.get();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment