Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include "Vector.h"
- // How to write an iterator for a vector class, don't need this since you can just increment a pointer instead
- // This concept translates to any data structure ever though. Just copy this and change what the operators do (such as ++)
- // Try implementing an iterator for the array class (check back to code Cherno put in his array class on this video)
- int main()
- {
- Vector<std::string> values;
- values.EmplaceBack("1");
- values.EmplaceBack("2");
- values.EmplaceBack("3");
- values.EmplaceBack("4");
- values.EmplaceBack("5");
- std::cout << "Not using iterators:\n";
- for (int i = 0; i < values.Size(); i++)
- {
- std::cout << values[i] << std::endl;
- }
- std::cout << "Range-based for loop:\n";
- for (auto& value : values)
- std::cout << value << std::endl;
- std::cout << "Iterator:\n";
- for (Vector<std::string>::Iterator it = values.begin();
- it != values.end(); it++)
- {
- std::cout << *it << std::endl;
- }
- #if 0
- std::vector<int> values = { 1, 2, 3, 4, 5 };
- for (int i = 0; i < values.size(); i++)
- {
- std::cout << values[i] << std::endl;
- }
- for (int value : values)
- std::cout << value << std::endl;
- for (std::vector<int>::iterator it = values.begin();
- it != values.end(); it++)
- {
- std::cout << *it << std::endl;
- }
- #endif
- std::cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment