Created
August 25, 2019 15:51
-
-
Save plusangel/2f3eb72ae1796eb5758fa0b30c6e81ac to your computer and use it in GitHub Desktop.
c++ iterators example #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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