You don't show the definition of diffSet
, but if, as its name
indicates, it is an std::set
, you can't assign into it. The
type of the object returned by the iterator is always const.
There's also the question of whether the elements you are
assigning to exist. If they don't (diffSet.size()
less than
the number of elements in the results), and the assignment were
allowed, you'd have undefined behavior (but likely a crash); if
they do, and the assignment was allowed, modifying an entry in
the std::set
would likely inviolate internal invariants
regarding the order.
The usual use of the set functions is on sorted std::vector
,
with an insertion iterator for the output:
std::vector<std::string> tradedSymbols;
// fill tradedSymbols
std::sort( tradedSymbols.begin(), tradedSymbols.end() );
std::vector<std::string> symbols;
// fill symbols
std::sort( symbols.begin(), symbols.end() );
std::vector<std::string> diffs;
std::set_difference( tradedSymbols.begin(), tradedSymbols.end(),
symbols.begin(), symbols.end(),
std::back_inserter( diffs ) );
You can use std::set as well, but you'll have to change the
inserter to std::inserter( diffs, diffs.end() )
.
(Note that instead of sorting, you can keep the vectors sorted
during insertion, by using std::lower_bound
to find the point
of insertion. If you're filling them in one go, however, using
std::sort
afterwards is probably more efficient.)
diffSet
const
? – Peter WooddiffSet
anstd::set
? That's the most probably source of the problem. – James Kanze