Skip to content

Instantly share code, notes, and snippets.

@cjhanks
Last active July 28, 2016 19:34
Show Gist options
  • Save cjhanks/f4404d2db1f79733fc65ac32f596d9bc to your computer and use it in GitHub Desktop.
Save cjhanks/f4404d2db1f79733fc65ac32f596d9bc to your computer and use it in GitHub Desktop.
Overloading OSTREAM
#include <cassert>
#include <functional>
#include <iostream>
#include <vector>
std::vector<int> V;
template <typename Container1, typename Container2>
std::ostream&
operator<<(std::ostream& ostr, const std::pair<Container1&, Container2&> data)
{
auto head1 = data.first.begin();
auto tail1 = data.first.end();
auto head2 = data.second.begin();
auto tail2 = data.second.end();
assert(std::distance(head1, tail1) ==
std::distance(head2, tail2));
size_t distance = std::distance(head1, tail1);
while (head1 != tail1
&& head2 != tail2) {
ostr << *head1 << ", " << *head2 << "\n";
++head1;
++head2;
}
return ostr;
}
template <typename Container1, typename Container2>
std::ostream&
operator<<(std::ostream& ostr, const std::pair<Container1, Container2> data)
{
auto head1 = data.first.begin();
auto tail1 = data.first.end();
auto head2 = data.second.begin();
auto tail2 = data.second.end();
assert(std::distance(head1, tail1) ==
std::distance(head2, tail2));
size_t distance = std::distance(head1, tail1);
while (head1 != tail1
&& head2 != tail2) {
ostr << *head1 << ", " << *head2 << "\n";
++head1;
++head2;
}
return ostr;
}
int
main()
{
V.push_back(0);
V.push_back(2);
V.push_back(1);
std::cout << std::make_pair(std::ref(V), std::ref(V));
std::cout << std::make_pair(V, V);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment