Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1092127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:38:09+00:00 2026-05-16T23:38:09+00:00

I am studying the book Accelerated C++ from Koenig & Moo. Exercise 8-2 ask

  • 0

I am studying the book “Accelerated C++” from Koenig & Moo.

Exercise 8-2 ask me to implement on my own some templatized functions from <algorithm> and <numeric>, and to specify what kind of iterator does my implementation require.

When trying to implement std::search, I determined that I need only “input” iterators.

Here is my code so far:

template <class In1, class In2>
In1 search(In1 b, In1 e, In2 b2, In2 e2)
{
    if (b2 != e2) {
        while (b != e) {
            if (*b == *b2) {
                In1 bc = b;
                In2 b2c = b2;
                while (bc != e && b2c != e2 && *bc == *b2c) {
                    ++bc;
                    ++b2c;
                }
                if (b2c == e2)
                    return b;
            }
            ++b;
        }
    }
    return e;
}

However, looking at the implementation of std::search installed with my compiler, I can see that they use “forward” iterators, but I cannot understand why, because there is no need to write, only to read, and input iterators meet the requirement.

Can anybody here help me to understand this, please? Why would I need to use “forward” iterators to implement std::search?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:38:10+00:00Added an answer on May 16, 2026 at 11:38 pm

    With an input iterator, you are only allowed to pass through the range once. That is, once you’ve dereferenced and incremented the iterator, you can’t go back and dereference it again.

    This class of iterators is quite useful for many things. For example, if you are reading from a stream of some kind, once you’ve read something from the stream you can’t go back and read that thing again; you can’t store off a copy of the iterator, continue iterating with the original iterator, then go back and start iterating with the copy and assume you’ll get the same results.

    In your algorithm, you pass through the range more than once (see the comments in your source here):

    template <class In1, class In2>
    In1 search(In1 b, In1 e, In2 b2, In2 e2)
    {
        if (b2 != e2) {
            while (b != e) {
                if (*b == *b2) {
                    In1 bc = b;            // copy iterator b
                    In2 b2c = b2;
                    while (bc != e && b2c != e2 && *bc == *b2c) {
                        ++bc;              // increment the copy of iterator b
                        ++b2c;
                    }
                    if (b2c == e2)
                        return b;
                }
                ++b;                       // increment the original b
            }
        }
        return e;
    }
    

    ForwardIterator is the most basic type of iterator that can be used for multi-pass algorithms because it guarantees that you can iterate over a range multiple times.

    Whether an iterator is mutable or immutable (that is, whether you can modify the element to which an iterator of its type refers) is independent of the category of the iterator: a ForwardIterator may be immutable. For example, const iterators of any iterator category are immutable (or, for a concrete example, std::forward_list<int>::const_iterator in C++0x is an immutable forward iterator).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm almost finished with the book Head First Java. The reason I'm studying this
Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
Studying C#, my books are showing me classes for readin files. I've found 2
I've been studying the Google authentication API (AuthSub)... My question is, how do I
I am a student studying software development, and I feel programming, in general, is
I am studying for cryptography and I somehow stuck on understanding how DES works.
I am studying the content of Programming Ruby- The Pragmatic Programmer's Guide but the
Recently I have been studying recursion; how to write it, analyze it, etc. I
I've been studying OOP for quite a while now and I have a good
I am quantitatively studying various metrics associated with automated tests. Chrome seems to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.