C++ STL set functions… and liquids that can write code

By William Entriken

1 minutes

Are you badass with C++ STL? Then fix this…

#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <ostream>
#include <iterator>

using namespace std;

int main()
{
  map<const char, int> A;
  A['a'] = 0;
  A['b'] = 5;
  A['c'] = 10;

  map<const char, int> B;
  B['b'] = 105;
  B['c'] = 110;
  B['d'] = 115;

  map<const char, int> C;

  set_difference(A.begin(), A.end(), B.begin(), B.end(), less<char>, inserter(C, C.begin()) );
  /*cout << "Set C (difference of A and B): ";
  copy(C.begin(), C.end(), ostream_iterator<const char*>(cout, " "));
  cout << endl;*/
}

<aeacides> lol
<aeacides> fulld: you reversed the order of the comparison functor with the insert iterator
<aeacides> set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.begin()), ltpairchar());
<fulld> aeacides: so basically… what you’re saying is I need more coffee :-)
<fulld> thanks
<aeacides> fulld: programmers are essentially machines which transform coffee into code
<fulld> I’m gonna blog that

Comments

The official X thread

@fulldecent

You were very close to getting the solution.
From the version where you have the ltpairchar struct, change this line:
set_difference(A.begin(), A.end(), B.begin(), B.end(), ltpairchar, inserter(C, C.begin()) );
to:
set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.begin()), ltpairchar());
You needed the () operator after your struct, and you had the last two arguments out of order.

Tyler

Please discuss this topic anywhere and let me know any great comments or media coverage I should link here.