Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created May 19, 2015 15:04
Show Gist options
  • Save krysseltillada/1548322e177c43cb10f2 to your computer and use it in GitHub Desktop.
Save krysseltillada/1548322e177c43cb10f2 to your computer and use it in GitHub Desktop.
accessing vectors using iterators
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v1 = {1};
auto it1 = v1.begin();
std::cout << *it1 << std::endl;
std::vector<int> v2(10, 2);
for(std::vector<int>::iterator it2 = v2.begin(); it2 != v2.end(); ++it2)
std::cout << *it2 << std::endl;
std::vector<int> v3(10, 42);
for(auto it3 = v3.begin(); it3 != v3.end(); ++it3)
std::cout << *it3 << std::endl;
std::vector<int> v4{10};
for(std::vector<int>::iterator it4 = v4.begin(); it4 != v4.end(); ++it4)
std::cout << *it4 << std::endl;
std::vector<int> v5{10, 42};
for(auto it5 = v5.begin(); it5 != v5.end(); ++it5)
std::cout << *it5 << std::endl;
std::vector<int> v6{10};
for(std::vector<int>::iterator it6 = v6.begin(); it6 != v6.end(); ++it6)
std::cout << *it6 << std::endl;
std::vector<std::string> v7{10, "hi"};
for(std::vector<std::string>::iterator it7 = v7.begin(); it7 != v7.end(); ++it7)
std::cout << *it7 << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment