mardi 15 décembre 2020

std::search to find a rightmost subsequence

I am trying to find a rightmost subsequence in a range with std::search and std::make_reverse_iterator.

However the iterator returned always point to the start of a range. What am I doing wrong?

TEST(basic_test, find_from_right)
{
    std::vector<uint8_t> array{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::array<uint8_t, 2> subSeq{3, 4};

    auto found = std::search(std::make_reverse_iterator(array.cend()),
                             std::make_reverse_iterator(array.cbegin()),
                             subSeq.cbegin(),
                             subSeq.cend());
// makes no difference
//                             std::make_reverse_iterator(subSeq.cend()),
//                             std::make_reverse_iterator(subSeq.cbegin()));


    auto distance = std::distance(found.base(), array.cbegin());

    EXPECT_EQ(distance, 3);
}

Output:

Failure
Expected equality of these values:
  distance
    Which is: 0
  3

I have a function that is provided with 2 template RandomIterators, so I have to call std::make_reverse_iterator. These containers are just for the sake of reproducing the problem and compiling the example.


I can't quiet understand how does this question fail to meet the community requirements. So if you downvote, please, consider explaining the reason.

Aucun commentaire:

Enregistrer un commentaire