Skip to content

Instantly share code, notes, and snippets.

@ilpropheta
Last active February 20, 2016 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilpropheta/bc6aac96dfea6e63e1b7 to your computer and use it in GitHub Desktop.
Save ilpropheta/bc6aac96dfea6e63e1b7 to your computer and use it in GitHub Desktop.
copy_if(adj_it(begin(v)), adj_it(end(v)), ostream_iterator<pair<int, int>>(cout, '\n') [&](const auto& p){
return p.second - p.first == minDiff;
});
@pmalek
Copy link

pmalek commented Feb 20, 2016

I believe this code doesn't compile (missing comma after ostream_iterator and some other minor problems) :)

I have tried to make it work but the closest I got was something like

#include <algorithm>
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>

using namespace std;

template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> p)
{
  return out << "(" << p.first << "," << p.second << ")";
}

template <typename T>
ostream& operator<<(ostream& out, vector<T> v)
{
  out << "{ ";
  for (const auto& i : v) {
   out << i << ' ';
  }
  out << '}';
  return out;
}

int main(int argc, char* argv[])
{
  vector<pair<int, int>> v{{1,2}, {1,1}, {4,4}}, result;
  copy_if(begin(v), end(v), std::back_inserter(result),//ostream_iterator<pair<int, int>>(cout, "\n"),
          [&](const auto& p) { return (p.second - p.first == 0); });
  cout << result;
}

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