1

I'm reviewing STL Algorithms. So I've written some algorithms among which this one merge_:

template <typename InIt1, typename InIt2, typename OutIt>
OutIt merge_(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt dest){

    while(first1 != last1){
        if(first2 == last2)
            return copy(first1, last1, dest);
        *dest++ = *first1 < *first2 ? *first1++ : *first2++;
    }

    return copy(first2, last2, dest);
}

  int main(){

    int a[]{1, 3, 5, 7};
    int b[]{2, 4, 10, 12};
    vector<int> v(10, 0);

    // 1 2 3 4 5 7 10 12
    //merge(begin(a), end(a), begin(b), end(b), v.begin());
    merge_(begin(a), end(a), begin(b), end(b), v.begin());

    for(int i : v)
        cout << i << ", ";
    cout << '\n';

    std::cout << "\ndone!\n";
}
  • It works just fine however I saw a possible implementation on cppreference: https://en.cppreference.com/w/cpp/algorithm/merge this way:

    First version
    template<class InputIt1, class InputIt2, class OutputIt>
    OutputIt merge(InputIt1 first1, InputIt1 last1,
                   InputIt2 first2, InputIt2 last2,
                   OutputIt d_first)
    {
        for (; first1 != last1; ++d_first) {
            if (first2 == last2) {
                return std::copy(first1, last1, d_first);
            }
            if (*first2 < *first1) {
                *d_first = *first2;
                ++first2;
            } else {
                *d_first = *first1;
                ++first1;
            }
        }
        return std::copy(first2, last2, d_first);
    }
    

I don't know why such complicated logic?

Itachi Uchiwa
  • 3,044
  • 12
  • 26
  • Does this answer your question? [Why are these constructs using pre and post-increment undefined behavior?](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) – Orace Nov 05 '21 at 21:18
  • Looks about the same to me except the one from cppreference is more readable because of braces and lack of ternary. – Retired Ninja Nov 05 '21 at 21:18
  • This probably belongs on https://codereview.stackexchange.com/ – Stephen Newell Nov 05 '21 at 21:19
  • @StephenNewell when suggesting users post on CR it would be great if there was also a suggestion like "_Please read the relevant help center pages like '[What topics can I ask about here?](https://codereview.stackexchange.com/help/on-topic)' and '[How do I ask a good question?](https://codereview.stackexchange.com/help/how-to-ask)_". The OP would need to ensure the code is written or maintained by them, otherwise [it wouldn't be reviewable](https://codereview.meta.stackexchange.com/questions/3649/my-question-was-closed-as-being-off-topic-what-are-my-options/3654#3654). – Sᴀᴍ Onᴇᴌᴀ Nov 05 '21 at 21:22
  • @SᴀᴍOnᴇᴌᴀ: Yes I've written it myself for practice purpose. I've written many algorithms mimicking STL ones for education purpose. – Itachi Uchiwa Nov 05 '21 at 21:24
  • @ItachiUchiwa I understand that- but I am referring to the other part you "_saw a possible implementation on cppreference_"... which you asked why it has such complicated logic... – Sᴀᴍ Onᴇᴌᴀ Nov 05 '21 at 21:25
  • @SᴀᴍOnᴇᴌᴀ: OK thank you. – Itachi Uchiwa Nov 05 '21 at 21:27
  • 1
    @Orace: The ternary operator has a sequence point, so OP's example is not undefined behavior, as far as I can tell. – Nate Eldredge Nov 05 '21 at 21:37
  • @NateEldredge, You said 'as far as I can tell', and that's my point it's to much work to estimate the actual side effects that the STL doesn't even bother to try to use the pre/post increment operators in a bigger expression – Orace Nov 05 '21 at 21:45
  • @Orace: `x++ * x++ ` or `x++ + ++x` is really UB and no sane person do it. The increment operator `++` is not sequenced. But the conditional operator `?` is really sequenced. `int i = 5; int j = x % 2 ? x++ : y++;` is legal. The first expr (condition) `x % 2` is evaluated first then if succeeded the second one is evaluated `++x` other wise the third one is evaluated. (2 and 3 are exclusive). `&& || ? ,` are sequenced operators and the new standard adds some like the shift operator `<< >>`. – Itachi Uchiwa Nov 05 '21 at 22:04

0 Answers0