Skip to content

Instantly share code, notes, and snippets.

@AndrewRademacher
Created May 5, 2019 17:51
Show Gist options
  • Save AndrewRademacher/c3af049e0cf041127d5cd44c2b18d0e9 to your computer and use it in GitHub Desktop.
Save AndrewRademacher/c3af049e0cf041127d5cd44c2b18d0e9 to your computer and use it in GitHub Desktop.
testing for proximity to end of iterator in all cases except input iterator.
#include <string>
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <unordered_set>
template<typename T>
void printElems(T elems) {
std::cout << "[ ";
for (auto itr = elems.begin(); itr != elems.end(); ++itr) {
std::cout << *itr;
// test proximity
auto next = itr;
++next;
if (next != elems.end()) std::cout << ", ";
}
std::cout << " ]\n";
}
int main() {
std::vector<uint64_t> vec{1, 2, 3, 4, 5, 6};
printElems(vec);
std::array<uint64_t, 5> ary{1, 2, 3, 4, 5};
printElems(ary);
std::set<uint64_t> set{1, 2, 3, 4};
printElems(set);
std::set<uint64_t> uset{1, 2, 3, 4};
printElems(uset);
return 0;
}
@AndrewRademacher
Copy link
Author

Output:

[ 1, 2, 3, 4, 5, 6 ]

[ 1, 2, 3, 4, 5 ]

[ 1, 2, 3, 4 ]

[ 1, 2, 3, 4 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment