Comparing two vectors sequential order

I have two vectors

`std::vector<int> numberSet1, numberSet2;`

numberSet1 contains 21,24

numberSet2 contains 20, 22, 23, 25 , 26 , 27

Is there anyway to determine if numberSet1 can sequentially fit into numberSet2 ?

Combined the numbers can go up at a count of 1? 20,21,22,23,24,25,26,27 I just want some kind of "return" to say , "numberSet1 fits the sequence"
Last edited on
show us your code. So we can know how to deal with it, you don't want someone elses complicated code you can't understand.
Last edited on
I dont have any code, as I said I have 2 vectors , numberSet2 is sorted by size. I do not know how to check if a vector can fit into the sequencing of another vector. So infact yes I would like to look at somebody else's code, complicated or not. That will at least give me the opportunity to break down whats happening and learn something new while accomplishing a task of mine.
Correct me if I misunderstand you ("fit sequentially" is not a well-known term), but you're asking whether two sorted sequences, such as {21,24} and {20,22,23,25,26,27} form a continuous integer range when merged?

You could use std::merge to do the merge, and then compare it for equality with the range of the same size and starting value:

 ``1234567891011121314151617181920212223`` ``````#include #include #include #include int main() { std::vector numberSet1 = {21,24}; std::vector numberSet2 = {20, 22, 23, 25 , 26 , 27}; std::vector merged; std::merge(numberSet1.begin(), numberSet1.end(), numberSet2.begin(), numberSet2.end(), back_inserter(merged)); std::vector range = merged; std::iota(range.begin(), range.end(), range.front()); if(merged == range) std::cout << "The merged vectors form a continuos integer range\n"; else std::cout << "The merged vectors do NOT form a continuos range\n"; }``````

(if you like boost, you can avoid constructing the second vector and just compare to a numeric range)
Boost, or a functor that has state.

There is also std::set_union that differs from std::merge a tiny bit: if same value occurs in both sets, then `result.size() < set1.size() + set2.size()`

Both algorithms require sorted input. The OP test will fail if either input set contains duplicate values.

Also note that on success, `result.front() + result.size() == result.back() + 1`.