By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

in-place set_intersection

P: n/a
Hi,

Can someone please explain to me WHY the standard does not allow the
input range and the output range to overlap for set_intersection?

I can't see why I can't do this (note sorted vectors, not sets or
anything)

vector<int> x = get_x();
vector<int> y = get_y();

x.erase(
set_intersection(x.begin(),x.end(),
y.begin(),y.end(),
x.begin()),
x.end());

?? I've looked at the g++ implementation, I see no reason why I can't
do this.

thanks
Paul

Aug 29 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
<ha*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Can someone please explain to me WHY the standard does not allow the
input range and the output range to overlap for set_intersection?


Because if the standard did allow the input and output ranges to overlap, it
would have to find a way of specifying the circumstances in which it would
be guaranteed to work and the circumstances in which it wouldn't.

Consider:

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

set_intersection(v.begin(), v.begin()+2, v.begin(), v.begin()+2,
v.begin()+1);

A straightforward implementation will look at v[0] and v[0], see that
they're the same, and copy v[0] to v[1]. Then it will look at (the newly
changed value of) v[1], and v[1], see that they're the same, and copy v[1]
to v[2]. The result will be that all three elements of v will be 1, which
is not correct.
Aug 29 '05 #2

P: n/a
So, one could reliably create a version of set_intersection that works
like unique(), and that would be ok? Where the two input ranges do
not overlap....

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

vector<int> v2;
v2.push_back(2);

v.erase( my_set_intersection(v.begin(),v.end(),v2.begin(),v 2.end()),
v.end());

where
template <class In1, class In2>
In1 my_set_intersection( In1 xb, In1 xe, In2 yb, In2 ye)
{
return set_intersection(xb,xe,yb,ye,xb);
}

?

Sep 2 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.