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

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

Anonymous said…
The error I get is:

fulld@clubv tmp $ make setmap
g++ setmap.cpp -o setmap
setmap.cpp: In function `int main()':
setmap.cpp:24: error: parse error before `,' token
make: *** [setmap] Error 1
#include < iostream>
#include < map>
#include < set>
#include < algorithm>
#include < ostream>
#include < iterator>

using namespace std;

struct ltpairchar
{
bool operator ()(pair< const char, int> p1, pair< const char, int> p2)
{
return p1.first < p2.first;
}
};

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(), ltpairchar, inserter(C, C.begin()) );
/*cout << "Set C (difference of A and B): ";
copy(C.begin(), C.end(), ostream_iterator< const char*>(cout, " "));
cout << endl;*/

}

I get this error now:
fulld@clubv tmp $ make setmap
g++ setmap.cpp -o setmap
setmap.cpp: In function `int main()':
setmap.cpp:32: error: parse error before `,' token
make: *** [setmap] Error 1
Changing ltpairchar to ltpairchar() yeilds:

fulld@clubv tmp $ make setmap 2>&1 | replace "<" "< "
g++ setmap.cpp -o setmap
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algo.h: In
function `_OutputIter std::set_difference(_InputIter1, _InputIter1,
_InputIter2, _InputIter2, _OutputIter, _Compare) [with _InputIter1 =
std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char,
int>&, std::pair< const char, int>*>, _InputIter2 =
std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char,
int>&, std::pair< const char, int>*>, _OutputIter = ltpairchar, _Compare =
std::insert_iterator< std::map< const char, int, std::less< const char>,
std::allocator< std::pair< const char, int> > > >]':
setmap.cpp:32: instantiated from here
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algo.h:3840: error: no
match for call to `(std::insert_iterator< std::map< const char, int,
std::less< const char>, std::allocator< std::pair< const char, int> > > >) (
std::pair< const char, int>&, std::pair< const char, int>&)'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algo.h:3841: error: no
match for 'operator*' in '*__result'
setmap.cpp:32: instantiated from here
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algo.h:3843: error: no
match for 'operator++' in '++__result'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algo.h:3845: error: no
match for call to `(std::insert_iterator< std::map< const char, int,
std::less< const char>, std::allocator< std::pair< const char, int> > > >) (
std::pair< const char, int>&, std::pair< const char, int>&)'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algobase.h: In
function `_OutputIter std::__copy(_InputIter, _InputIter, _OutputIter,
std::input_iterator_tag) [with _InputIter =
std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char,
int>&, std::pair< const char, int>*>, _OutputIter = ltpairchar]':
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algobase.h:260: instantiated from `_OutputIter std::__copy_aux2(_InputIter, _InputIter, _OutputIter, __false_type) [with _InputIter = std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char, int>&, std::pair< const char, int>*>, _OutputIter = ltpairchar]'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algobase.h:303: instantiated from `_OutputIter std::__copy_ni2(_InputIter, _InputIter, _OutputIter, __false_type) [with _InputIter = std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char, int>&, std::pair< const char, int>*>, _OutputIter = ltpairchar]'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algobase.h:323: instantiated from `_OutputIter std::__copy_ni1(_InputIter, _InputIter, _OutputIter, __false_type) [with _InputIter = std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char, int>&, std::pair< const char, int>*>, _OutputIter = ltpairchar]'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algobase.h:349: instantiated from `_OutputIter std::copy(_InputIter, _InputIter, _OutputIter) [with _InputIter = std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char, int>&, std::pair< const char, int>*>, _OutputIter = ltpairchar]'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algo.h:3851: instantiated from `_OutputIter std::set_difference(_InputIter1, _InputIter1, _InputIter2, _InputIter2, _OutputIter, _Compare) [with _InputIter1 = std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char, int>&, std::pair< const char, int>*>, _InputIter2 = std::_Rb_tree_iterator< std::pair< const char, int>, std::pair< const char, int>&, std::pair< const char, int>*>, _OutputIter = ltpairchar, _Compare = std::insert_iterator< std::map< const char, int, std::less< const char>, std::allocator< std::pair< const char, int> > > >]'
setmap.cpp:32: instantiated from here
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algobase.h:227: error: no
match for 'operator++' in '++__result'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_algobase.h:228: error: no
match for 'operator*' in '*__result'
make: *** [setmap] Error 1
Anonymous said…
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
(Like hell I'm registering a blogger account just to say this)

Popular posts from this blog

Nontechnical: What is ERC-721?

There are no NFT contracts before Terra Nullius

Testing deployed Ethereum contracts with ESTIMATEGAS