someone12321
someone12321

Reputation: 755

How to do two pointers technique on two sets

Let's say we have given two sets in C++11, they are having different elements, and it always holds that the first set is having smaller elements then the second set.

To be more clear, we want to merge the two sets and then do the two pointer technique on this set. But I think that it is also possible to code it without merging the sets into one.

It is easy in theory, but I don't know how to write this in code. The problem is that the pointers will start in the first set, but when it will become bigger then the size of the first set it should move in the second set. Can you please give me some hints how to do this. Thanks in advance.

Upvotes: 0

Views: 63

Answers (1)

KonaeAkira
KonaeAkira

Reputation: 236

One way to do it is to check if the pointer has reached the end of the first set, then point it to the second set.

std::set<T> a, b;
std::set<T>::iterator p = a.begin();
while (p != b.end())
{
    // do something
    if (++p == a.end()) p = b.begin();
}

Upvotes: 1

Related Questions