Skip to content

Instantly share code, notes, and snippets.

@plusangel
Created August 25, 2019 15:51
Show Gist options
  • Save plusangel/2f3eb72ae1796eb5758fa0b30c6e81ac to your computer and use it in GitHub Desktop.
Save plusangel/2f3eb72ae1796eb5758fa0b30c6e81ac to your computer and use it in GitHub Desktop.
c++ iterators example #1
#include <array>
#include <algorithm>
#include <iostream>
class MyType {
public:
MyType() = default;
MyType(int item):item_{item}
{ }
int item_;
};
using MyTypeArray = std::array<MyType, 3>;
MyTypeArray my_array;
void doSomethingWithEachElement(const MyType& element)
{
std::cout << element.item_ << std::endl;
}
int main(int argc, char* argv[])
{
my_array.at(0) = MyType(1);
my_array.at(1) = MyType(2);
my_array.at(2) = MyType(3);
std::for_each(std::begin(my_array), std::end(my_array), doSomethingWithEachElement);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment