Skip to content

Instantly share code, notes, and snippets.

Created January 28, 2018 00:34
Show Gist options
  • Save anonymous/78b4f257e60d55537b36e2e4f3c6de9d to your computer and use it in GitHub Desktop.
Save anonymous/78b4f257e60d55537b36e2e4f3c6de9d to your computer and use it in GitHub Desktop.
#include <iterator>
#include <iostream>
void
int_squares(int c[5])
{
// won't work because the size of the array is not available
// when passed to a function because it is treated as a pointer
std::cerr << "auto it : ..." << '\n';
int acc = 0;
for (auto it : c) {
acc += it;
std::cerr << it << ' ' << acc << '\n';
}
}
int
main()
{
int c_array[] = {1,3,5,7,9};
int acc = 0;
std::cerr << "auto it : with range..." << '\n';
for (auto it : c_array) {
acc += it;
std::cerr << it << ' ' << acc << '\n';
}
std::cerr << "auto it = std::begin ..." << '\n';
acc = 0;
for (auto it = std::begin(c_array); it != std::end(c_array); it++) {
acc += *it;
std::cerr << *it << ' ' << acc << '\n';
}
std::cerr << "old school ..." << '\n';
acc = 0;
for (int* it = &c_array[0]; it != &c_array[sizeof c_array / sizeof(int)]; it++) {
acc += *it;
std::cerr << *it << ' ' << acc << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment