Skip to content

Page 590: The description of "search" from <algorithm> is inconsistent with the example and incorrectly worded. #126

@WraithGlade

Description

@WraithGlade

In this text from the page:

The algorithm locates sequence 2 within sequence 1. In other words, it returns the first iterator i in sequence 1 such that for each non-negative integer n, *(=i + n) equals (ipt_begin2 + n), or if you provide a predicate pred((i + n), *(ipt_begin2 + n)) is true. The search algorithm returns ipt_begin1 if sequence 2 is empty or ipt_begin2 if no subsequence is found. This is different from find because it locates a subsequence rather than a single element.

ForwardIterator search([ ep], fwd_begin1, fwd_end1, fwd_begin2, fwd_end2, [pred]);

There are two main problems here:

(1) Notice that the variables "ipt_begin1" and "ipt_begin2" are different from what's actually used in the function declaration given later, which instead uses "fwd_" variants of the names.

(2) Most importantly, the phrase "or ipt_begin2 if no subsequence is found" appears to be wrong and misleading. The test case code shows that it appears to actually return an end iterator (which is far more reasonable), not a begin iterator. This was very confusing when I first read it. I'm sure it will confuse others too.

In contrast though, returning a begin iterator for searching for an empty subseq makes sense, since an empty subsequence is a subsequence of any sequence, but that's a different case.

Consider a general rewrite of the paragraph in general too, perhaps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions